|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Salient\PHPStan\Core\Rules; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PhpParser\Node\Identifier; |
| 7 | +use PhpParser\Node; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\Php\PhpMethodReflection; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use PHPStan\Type\NeverType; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\VerbosityLevel; |
| 15 | +use Salient\Core\Concern\HasMutator; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements Rule<MethodCall> |
| 19 | + */ |
| 20 | +class TypesAssignedByHasMutatorRule implements Rule |
| 21 | +{ |
| 22 | + public function getNodeType(): string |
| 23 | + { |
| 24 | + return MethodCall::class; |
| 25 | + } |
| 26 | + |
| 27 | + public function processNode(Node $node, Scope $scope): array |
| 28 | + { |
| 29 | + /** @var MethodCall $node */ |
| 30 | + $methodName = $node->name; |
| 31 | + if (!$methodName instanceof Identifier) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + $calledOnType = $scope->getType($node->var); |
| 35 | + $methodReflection = $scope->getMethodReflection($calledOnType, $methodName->toString()); |
| 36 | + if (!$methodReflection) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + $prototypeReflection = $methodReflection->getPrototype(); |
| 40 | + if (!$prototypeReflection instanceof PhpMethodReflection) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + $traitReflection = $prototypeReflection->getDeclaringTrait(); |
| 44 | + if (!$traitReflection || $traitReflection->getName() !== HasMutator::class) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + $classReflection = $prototypeReflection->getDeclaringClass(); |
| 48 | + $aliases = array_change_key_case($classReflection->getNativeReflection()->getTraitAliases()); |
| 49 | + $name = $methodName->toLowerString(); |
| 50 | + if (isset($aliases[$name])) { |
| 51 | + $name = explode('::', $aliases[$name])[1]; |
| 52 | + } |
| 53 | + if ($name !== 'with' && $name !== 'without') { |
| 54 | + return []; |
| 55 | + } |
| 56 | + $args = $node->getArgs(); |
| 57 | + if (!$args) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + $propertyName = $scope->getType($args[0]->value); |
| 61 | + if ( |
| 62 | + !$propertyName->isConstantScalarValue()->yes() |
| 63 | + || !$propertyName->isString()->yes() |
| 64 | + ) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + /** @var string */ |
| 68 | + $propertyName = $propertyName->getConstantScalarValues()[0]; |
| 69 | + $has = $calledOnType->hasProperty($propertyName); |
| 70 | + if (!$has->yes() && !($has->maybe() && $this->allowsDynamicProperties($calledOnType))) { |
| 71 | + return [ |
| 72 | + RuleErrorBuilder::message(sprintf( |
| 73 | + 'Access to an undefined property %s::$%s.', |
| 74 | + $calledOnType->describe(VerbosityLevel::typeOnly()), |
| 75 | + $propertyName, |
| 76 | + )) |
| 77 | + ->identifier('salient.property.notFound') |
| 78 | + ->build(), |
| 79 | + ]; |
| 80 | + } |
| 81 | + if ($name !== 'with' || count($args) < 2) { |
| 82 | + return []; |
| 83 | + } |
| 84 | + $propertyReflection = $calledOnType->getProperty($propertyName, $scope); |
| 85 | + $propertyType = $propertyReflection->getWritableType(); |
| 86 | + if ($propertyType instanceof NeverType) { |
| 87 | + $propertyType = $propertyReflection->getReadableType(); |
| 88 | + } |
| 89 | + $valueType = $scope->getType($args[1]->value); |
| 90 | + $accepts = $propertyType->isSuperTypeOf($valueType); |
| 91 | + if (!$accepts->yes()) { |
| 92 | + return [ |
| 93 | + RuleErrorBuilder::message(sprintf( |
| 94 | + 'Property %s::$%s (%s) does not accept %s.', |
| 95 | + $calledOnType->describe(VerbosityLevel::typeOnly()), |
| 96 | + $propertyName, |
| 97 | + $propertyType->describe(VerbosityLevel::precise()), |
| 98 | + $valueType->describe(VerbosityLevel::precise()), |
| 99 | + )) |
| 100 | + ->identifier('salient.property.type') |
| 101 | + ->build(), |
| 102 | + ]; |
| 103 | + } |
| 104 | + return []; |
| 105 | + } |
| 106 | + |
| 107 | + private function allowsDynamicProperties(Type $type): bool |
| 108 | + { |
| 109 | + foreach ($type->getObjectClassReflections() as $reflection) { |
| 110 | + if ($reflection->allowsDynamicProperties()) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | +} |
0 commit comments