|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Methods; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Rules\Rule; |
| 8 | +use PHPStan\Rules\RuleErrorBuilder; |
| 9 | +use function array_key_exists; |
| 10 | +use function array_map; |
| 11 | +use function in_array; |
| 12 | +use function sprintf; |
| 13 | +use function strtolower; |
| 14 | + |
| 15 | +/** |
| 16 | + * @implements Rule<Node\Expr\StaticCall> |
| 17 | + */ |
| 18 | +final class IllegalConstructorStaticCallRule implements Rule |
| 19 | +{ |
| 20 | + |
| 21 | +public function getNodeType(): string |
| 22 | +{ |
| 23 | +return Node\Expr\StaticCall::class; |
| 24 | +} |
| 25 | + |
| 26 | +public function processNode(Node $node, Scope $scope): array |
| 27 | +{ |
| 28 | +if (!$node->name instanceof Node\Identifier || $node->name->toLowerString() !== '__construct') { |
| 29 | +return []; |
| 30 | +} |
| 31 | + |
| 32 | +if ($this->isCollectCallingConstructor($node, $scope)) { |
| 33 | +return []; |
| 34 | +} |
| 35 | + |
| 36 | +return [ |
| 37 | +RuleErrorBuilder::message('Static call to __construct() is only allowed on a parent class in the constructor.') |
| 38 | +->identifier('constructor.call') |
| 39 | +->build(), |
| 40 | +]; |
| 41 | +} |
| 42 | + |
| 43 | +private function isCollectCallingConstructor(Node\Expr\StaticCall $node, Scope $scope): bool |
| 44 | +{ |
| 45 | +// __construct should be called from inside constructor |
| 46 | +if ($scope->getFunction() === null) { |
| 47 | +return false; |
| 48 | +} |
| 49 | + |
| 50 | +if ($scope->getFunction()->getName() !== '__construct') { |
| 51 | +if (!$this->isInRenamedTraitConstructor($scope)) { |
| 52 | +return false; |
| 53 | +} |
| 54 | +} |
| 55 | + |
| 56 | +if (!$scope->isInClass()) { |
| 57 | +return false; |
| 58 | +} |
| 59 | + |
| 60 | +if (!$node->class instanceof Node\Name) { |
| 61 | +return false; |
| 62 | +} |
| 63 | + |
| 64 | +$parentClasses = array_map(static fn (string $name) => strtolower($name), $scope->getClassReflection()->getParentClassesNames()); |
| 65 | + |
| 66 | +return in_array(strtolower($scope->resolveName($node->class)), $parentClasses, true); |
| 67 | +} |
| 68 | + |
| 69 | +private function isInRenamedTraitConstructor(Scope $scope): bool |
| 70 | +{ |
| 71 | +if (!$scope->isInClass()) { |
| 72 | +return false; |
| 73 | +} |
| 74 | + |
| 75 | +if (!$scope->isInTrait()) { |
| 76 | +return false; |
| 77 | +} |
| 78 | + |
| 79 | +if ($scope->getFunction() === null) { |
| 80 | +return false; |
| 81 | +} |
| 82 | + |
| 83 | +$traitAliases = $scope->getClassReflection()->getNativeReflection()->getTraitAliases(); |
| 84 | +$functionName = $scope->getFunction()->getName(); |
| 85 | +if (!array_key_exists($functionName, $traitAliases)) { |
| 86 | +return false; |
| 87 | +} |
| 88 | + |
| 89 | +return $traitAliases[$functionName] === sprintf('%s::%s', $scope->getTraitReflection()->getName(), '__construct'); |
| 90 | +} |
| 91 | + |
| 92 | +} |
0 commit comments