|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symplify\PHPStanRules\Rules\Symfony\ConfigClosure; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\ArrayItem; |
| 9 | +use PhpParser\Node\Expr\Array_; |
| 10 | +use PhpParser\Node\Expr\BinaryOp; |
| 11 | +use PhpParser\Node\Expr\BinaryOp\Concat; |
| 12 | +use PhpParser\Node\Expr\Closure; |
| 13 | +use PhpParser\Node\Expr\MethodCall; |
| 14 | +use PhpParser\Node\Identifier; |
| 15 | +use PhpParser\Node\Scalar\MagicConst\Dir; |
| 16 | +use PhpParser\Node\Scalar\String_; |
| 17 | +use PhpParser\NodeFinder; |
| 18 | +use PHPStan\Analyser\Scope; |
| 19 | +use PHPStan\Rules\IdentifierRuleError; |
| 20 | +use PHPStan\Rules\Rule; |
| 21 | +use PHPStan\Rules\RuleErrorBuilder; |
| 22 | +use Symplify\PHPStanRules\Enum\SymfonyRuleIdentifier; |
| 23 | +use Symplify\PHPStanRules\Symfony\NodeAnalyzer\SymfonyClosureDetector; |
| 24 | + |
| 25 | +/** |
| 26 | + * @implements Rule<Closure> |
| 27 | + */ |
| 28 | +final class ServicesExcludedDirectoryMustExistRule implements Rule |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + public const ERROR_MESSAGE = 'Services excluded path "%s" does not exists. You can remove it'; |
| 34 | + |
| 35 | + public function getNodeType(): string |
| 36 | + { |
| 37 | + return Closure::class; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param Closure $node |
| 42 | + * @return IdentifierRuleError[] |
| 43 | + */ |
| 44 | + public function processNode(Node $node, Scope $scope): array |
| 45 | + { |
| 46 | + if (! SymfonyClosureDetector::detect($node)) { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + $excludeMethodCalls = $this->resolveExcludeMethodCalls($node); |
| 51 | + if ($excludeMethodCalls === []) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + $ruleErrors = []; |
| 56 | + |
| 57 | + foreach ($excludeMethodCalls as $excludeMethodCall) { |
| 58 | + // check all array args |
| 59 | + $firstArgValue = $excludeMethodCall->getArgs()[0]->value; |
| 60 | + if (! $firstArgValue instanceof Array_) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($firstArgValue->items as $arrayItem) { |
| 65 | + $directoryPath = $this->resolveDirectoryPath($arrayItem, $scope); |
| 66 | + if (! is_string($directoryPath)) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + // path exists, all good |
| 71 | + if (file_exists($directoryPath)) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + /** @var BinaryOp $binarOp */ |
| 76 | + $binarOp = $arrayItem->value; |
| 77 | + |
| 78 | + /** @var String_ $pathValue */ |
| 79 | + $pathValue = $binarOp->right; |
| 80 | + |
| 81 | + $errorMessage = sprintf(self::ERROR_MESSAGE, $pathValue->value); |
| 82 | + |
| 83 | + $ruleErrors[] = RuleErrorBuilder::message($errorMessage) |
| 84 | + ->line($arrayItem->getStartLine()) |
| 85 | + ->identifier(SymfonyRuleIdentifier::SERVICES_EXCLUDED_DIRECTORY_MUST_EXIST) |
| 86 | + ->build(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $ruleErrors; |
| 91 | + } |
| 92 | + |
| 93 | + private function resolveDirectoryPath(ArrayItem $arrayItem, Scope $scope): ?string |
| 94 | + { |
| 95 | + if (! $arrayItem->value instanceof Concat) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + $concat = $arrayItem->value; |
| 100 | + if (! $concat->left instanceof Dir) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (! $concat->right instanceof String_) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $stringPart = $concat->right->value; |
| 109 | + |
| 110 | + // uses magic mask, nothing to validate |
| 111 | + if (str_contains($stringPart, '*') || str_contains($stringPart, '{')) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + // check full path |
| 116 | + $directory = dirname($scope->getFile()); |
| 117 | + return $directory . '/' . $stringPart; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @return array<MethodCall> |
| 122 | + */ |
| 123 | + private function resolveExcludeMethodCalls(Closure $closure): array |
| 124 | + { |
| 125 | + $nodeFinder = new NodeFinder(); |
| 126 | + |
| 127 | + $methodCalls = $nodeFinder->find($closure, function (Node $node): bool { |
| 128 | + if (! $node instanceof MethodCall) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + if ($node->isFirstClassCallable()) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + // must have exactly 1 arg |
| 137 | + if (count($node->getArgs()) !== 1) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + if (! $node->name instanceof Identifier) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + return $node->name->toString() === 'exclude'; |
| 146 | + }); |
| 147 | + |
| 148 | + /** @var MethodCall[] $methodCalls */ |
| 149 | + return $methodCalls; |
| 150 | + } |
| 151 | +} |
0 commit comments