|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Nette\PHPStan\Database; |
| 4 | + |
| 5 | +use Nette\Database\Table\ActiveRow; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\MethodReflection; |
| 10 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\NullType; |
| 12 | +use PHPStan\Type\Type; |
| 13 | +use PHPStan\Type\TypeCombinator; |
| 14 | +use function count; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Narrows return type of ActiveRow::ref() from ?self |
| 19 | + * to ?EntityRow based on table-to-entity-class mapping. |
| 20 | + * |
| 21 | + * When the foreign-key column (2nd argument) is declared as non-nullable on the |
| 22 | + * calling row class, the referenced row is guaranteed to exist, so the result is |
| 23 | + * narrowed to a non-nullable EntityRow. |
| 24 | + */ |
| 25 | +class ActiveRowRefReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private readonly TableRowTypeResolver $resolver, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public function getClass(): string |
| 34 | + { |
| 35 | + return ActiveRow::class; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 40 | + { |
| 41 | + return $methodReflection->getName() === 'ref'; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public function getTypeFromMethodCall( |
| 46 | + MethodReflection $methodReflection, |
| 47 | + MethodCall $methodCall, |
| 48 | + Scope $scope, |
| 49 | + ): ?Type |
| 50 | + { |
| 51 | + if ($methodCall->isFirstClassCallable()) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + $args = $methodCall->getArgs(); |
| 56 | + if ($args === []) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + $keyType = $scope->getType($args[0]->value); |
| 61 | + $constantStrings = $keyType->getConstantStrings(); |
| 62 | + if (count($constantStrings) !== 1) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $key = $constantStrings[0]->getValue(); |
| 67 | + $tableName = $this->resolver->extractTableName($key); |
| 68 | + $rowType = $this->resolver->resolve($tableName); |
| 69 | + if ($rowType === null) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + if (isset($args[1]) && $this->isColumnNonNullable($args[1]->value, $methodCall->var, $scope)) { |
| 74 | + return $rowType; |
| 75 | + } |
| 76 | + |
| 77 | + return TypeCombinator::addNull($rowType); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * Tells whether the FK column is declared as non-nullable on the calling row class. |
| 83 | + * Tries both camelCase (Explorer convention) and the raw column name. |
| 84 | + */ |
| 85 | + private function isColumnNonNullable(Expr $columnExpr, Expr $callerExpr, Scope $scope): bool |
| 86 | + { |
| 87 | + $columnStrings = $scope->getType($columnExpr)->getConstantStrings(); |
| 88 | + if (count($columnStrings) !== 1) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $column = $columnStrings[0]->getValue(); |
| 93 | + $callerType = $scope->getType($callerExpr); |
| 94 | + |
| 95 | + foreach ([$this->resolver->snakeToCamelCase($column), $column] as $property) { |
| 96 | + if (!$callerType->hasProperty($property)->yes()) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + $propertyType = $callerType->getProperty($property, $scope)->getReadableType(); |
| 100 | + return $propertyType->isSuperTypeOf(new NullType)->no(); |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | +} |
0 commit comments