|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Xact\PHPStan\Collectors; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\Trait_; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Collectors\Collector; |
| 11 | +use PHPStan\File\FileHelper; |
| 12 | +use Xact\PHPStan\Configuration; |
| 13 | +use Xact\PHPStan\Exception\InvalidNodeTypeException; |
| 14 | + |
| 15 | +class DeclareTraitCollector implements Collector |
| 16 | +{ |
| 17 | + private Configuration $configuration; |
| 18 | + private FileHelper $fileHelper; |
| 19 | + |
| 20 | + public function __construct(Configuration $configuration, FileHelper $fileHelper) |
| 21 | + { |
| 22 | + $this->configuration = $configuration; |
| 23 | + $this->fileHelper = $fileHelper; |
| 24 | + } |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return Trait_::class; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter |
| 35 | + public function processNode(Node $node, Scope $scope) |
| 36 | + { |
| 37 | + if (!$node instanceof Trait_) { |
| 38 | + throw InvalidNodeTypeException::create($node, Trait_::class); |
| 39 | + } |
| 40 | + |
| 41 | + if ($node->namespacedName === null) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + if ($this->isFileExcluded($scope)) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + // returns an array with class name and line - array{string, int} |
| 50 | + return [$node->namespacedName->toString(), $node->getLine()]; |
| 51 | + } |
| 52 | + |
| 53 | + private function isFileExcluded(Scope $scope): bool |
| 54 | + { |
| 55 | + if ($this->configuration->isUnusedTraitsEnabled() === false) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + $excludePaths = $this->configuration->getExcludePaths(); |
| 60 | + foreach ($excludePaths as $path) { |
| 61 | + $excludePath = $this->fileHelper->absolutizePath($path); |
| 62 | + if (str_starts_with($scope->getFile(), $excludePath)) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + return false; |
| 67 | + } |
| 68 | +} |
0 commit comments