|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bug11857; |
| 4 | + |
| 5 | +use PHPStan\Analyser\Scope; |
| 6 | +use PHPStan\Reflection\MethodReflection; |
| 7 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 8 | +use PHPStan\Type\Generic\GenericObjectType; |
| 9 | +use PHPStan\Type\ObjectType; |
| 10 | +use PHPStan\Type\Type; |
| 11 | +use PhpParser\Node\Expr\MethodCall; |
| 12 | + |
| 13 | +use function PHPStan\Testing\assertType; |
| 14 | + |
| 15 | +class RelationDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 16 | +{ |
| 17 | + public function getClass(): string |
| 18 | + { |
| 19 | + return Model::class; |
| 20 | + } |
| 21 | + |
| 22 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 23 | + { |
| 24 | + return $methodReflection->getName() === 'belongsTo'; |
| 25 | + } |
| 26 | + |
| 27 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type { |
| 28 | + $returnType = $methodReflection->getVariants()[0]->getReturnType(); |
| 29 | + $argType = $scope->getType($methodCall->getArgs()[0]->value); |
| 30 | + $modelClass = $argType->getClassStringObjectType()->getObjectClassNames()[0]; |
| 31 | + |
| 32 | + return new GenericObjectType($returnType->getObjectClassNames()[0], [ |
| 33 | + new ObjectType($modelClass), |
| 34 | + $scope->getType($methodCall->var), |
| 35 | + ]); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +abstract class Model |
| 40 | +{ |
| 41 | + /** @return BelongsTo<*, *> */ |
| 42 | + public function belongsTo(string $related): BelongsTo |
| 43 | + { |
| 44 | + return new BelongsTo(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @template TRelatedModel of Model |
| 50 | + * @template TDeclaringModel of Model |
| 51 | + */ |
| 52 | +class BelongsTo {} |
| 53 | + |
| 54 | +class User extends Model {} |
| 55 | + |
| 56 | +class Post extends Model |
| 57 | +{ |
| 58 | + /** @return BelongsTo<User, $this> */ |
| 59 | + public function user(): BelongsTo |
| 60 | + { |
| 61 | + return $this->belongsTo(User::class); |
| 62 | + } |
| 63 | + |
| 64 | + /** @return BelongsTo<User, self> */ |
| 65 | + public function userSelf(): BelongsTo |
| 66 | + { |
| 67 | + /** @phpstan-ignore return.type */ |
| 68 | + return $this->belongsTo(User::class); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class ChildPost extends Post {} |
| 73 | + |
| 74 | +final class Comment extends Model |
| 75 | +{ |
| 76 | + // This model is final, so either of these |
| 77 | + // two methods would work. It seems that |
| 78 | + // PHPStan is automatically converting the |
| 79 | + // `$this` to a `self` type in the user docblock, |
| 80 | + // but it is not doing so likewise for the `$this` |
| 81 | + // that is returned by the dynamic return extension. |
| 82 | + |
| 83 | + /** @return BelongsTo<User, $this> */ |
| 84 | + public function user(): BelongsTo |
| 85 | + { |
| 86 | + return $this->belongsTo(User::class); |
| 87 | + } |
| 88 | + |
| 89 | + /** @return BelongsTo<User, self> */ |
| 90 | + public function user2(): BelongsTo |
| 91 | + { |
| 92 | + return $this->belongsTo(User::class); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function test(ChildPost $child): void |
| 97 | +{ |
| 98 | + assertType('Bug11857\BelongsTo<Bug11857\User, Bug11857\ChildPost>', $child->user()); |
| 99 | + // This demonstrates why `$this` is needed in non-final models |
| 100 | + assertType('Bug11857\BelongsTo<Bug11857\User, Bug11857\ChildPost>', $child->userSelf()); |
| 101 | +} |
0 commit comments