|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SaschaEgerer\PhpstanTypo3\Rule; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Type\ObjectType; |
| 10 | +use SaschaEgerer\PhpstanTypo3\Service\NullServiceDefinitionChecker; |
| 11 | +use SaschaEgerer\PhpstanTypo3\Service\PrivateServiceAnalyzer; |
| 12 | + |
| 13 | +/** |
| 14 | + * @implements Rule<MethodCall> |
| 15 | + */ |
| 16 | +final class ContainerInterfacePrivateServiceRule implements Rule |
| 17 | +{ |
| 18 | + |
| 19 | + private PrivateServiceAnalyzer $privateServiceAnalyzer; |
| 20 | + |
| 21 | + public function __construct(PrivateServiceAnalyzer $privateServiceAnalyzer) |
| 22 | + { |
| 23 | + $this->privateServiceAnalyzer = $privateServiceAnalyzer; |
| 24 | + } |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return MethodCall::class; |
| 29 | + } |
| 30 | + |
| 31 | + public function processNode(Node $node, Scope $scope): array |
| 32 | + { |
| 33 | + if ($this->shouldSkip($node, $scope)) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + return $this->privateServiceAnalyzer->analyze($node, $scope, new NullServiceDefinitionChecker()); |
| 38 | + } |
| 39 | + |
| 40 | + private function shouldSkip(MethodCall $node, Scope $scope): bool |
| 41 | + { |
| 42 | + if (!$node->name instanceof Node\Identifier) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + $methodCallArguments = $node->getArgs(); |
| 47 | + |
| 48 | + if (!isset($methodCallArguments[0])) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + $methodCallName = $node->name->name; |
| 53 | + |
| 54 | + if ($methodCallName !== 'get') { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + $argType = $scope->getType($node->var); |
| 59 | + |
| 60 | + $isPsrContainerType = (new ObjectType('Psr\Container\ContainerInterface'))->isSuperTypeOf($argType); |
| 61 | + $isTestCaseType = (new ObjectType('TYPO3\TestingFramework\Core\Functional\FunctionalTestCase'))->isSuperTypeOf($argType); |
| 62 | + |
| 63 | + if ($isTestCaseType->yes()) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + return !$isPsrContainerType->yes(); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments