Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ jobs:
echo "$OUTPUT"
../bashunit -a contains 'Child process error (exit code 255): PHP Fatal error' "$OUTPUT"
../bashunit -a contains 'Result is incomplete because of severe errors.' "$OUTPUT"
- script: |
cd e2e/bug-11857
composer install
../../bin/phpstan

steps:
- name: "Checkout"
Expand Down
1 change: 1 addition & 0 deletions e2e/bug-11857/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
5 changes: 5 additions & 0 deletions e2e/bug-11857/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"autoload-dev": {
"classmap": ["src/"]
}
}
18 changes: 18 additions & 0 deletions e2e/bug-11857/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions e2e/bug-11857/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 8
paths:
- src

services:
-
class: Bug11857\RelationDynamicMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
36 changes: 36 additions & 0 deletions e2e/bug-11857/src/extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Bug11857;

use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PhpParser\Node\Expr\MethodCall;

class RelationDynamicMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return Model::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'belongsTo';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type {
$returnType = $methodReflection->getVariants()[0]->getReturnType();
$argType = $scope->getType($methodCall->getArgs()[0]->value);
$modelClass = $argType->getClassStringObjectType()->getObjectClassNames()[0];

return new GenericObjectType($returnType->getObjectClassNames()[0], [
new ObjectType($modelClass),
$scope->getType($methodCall->var),
]);
}
}

70 changes: 70 additions & 0 deletions e2e/bug-11857/src/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Bug11857;

use function PHPStan\Testing\assertType;

abstract class Model
{
/** @return BelongsTo<*, *> */
public function belongsTo(string $related): BelongsTo
{
return new BelongsTo();
}
}

/**
* @template TRelatedModel of Model
* @template TDeclaringModel of Model
*/
class BelongsTo {}

class User extends Model {}

class Post extends Model
{
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/** @return BelongsTo<User, self> */
public function userSelf(): BelongsTo
{
/** @phpstan-ignore return.type */
return $this->belongsTo(User::class);
}
}

class ChildPost extends Post {}

final class Comment extends Model
{
// This model is final, so either of these
// two methods would work. It seems that
// PHPStan is automatically converting the
// `$this` to a `self` type in the user docblock,
// but it is not doing so likewise for the `$this`
// that is returned by the dynamic return extension.

/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/** @return BelongsTo<User, self> */
public function user2(): BelongsTo
{
/** @phpstan-ignore return.type */
return $this->belongsTo(User::class);
}
}

function test(ChildPost $child): void
{
assertType('Bug11857\BelongsTo<Bug11857\User, Bug11857\ChildPost>', $child->user());
// This demonstrates why `$this` is needed in non-final models
assertType('Bug11857\BelongsTo<Bug11857\User, Bug11857\Post>', $child->userSelf()); // should be: Bug11857\BelongsTo<Bug11857\User, Bug11857\ChildPost>
}
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3015,7 +3015,7 @@ private function transformStaticType(Type $type): Type
if ($type instanceof StaticType) {
$classReflection = $this->getClassReflection();
$changedType = $type->changeBaseClass($classReflection);
if ($classReflection->isFinal()) {
if ($classReflection->isFinal() && !$type instanceof ThisType) {
$changedType = $changedType->getStaticObjectType();
}
return $traverse($changedType);
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6159,7 +6159,7 @@ private function transformStaticType(ClassReflection $declaringClass, Type $type
return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($declaringClass): Type {
if ($type instanceof StaticType) {
$changedType = $type->changeBaseClass($declaringClass);
if ($declaringClass->isFinal()) {
if ($declaringClass->isFinal() && !$type instanceof ThisType) {
$changedType = $changedType->getStaticObjectType();
}
return $traverse($changedType);
Expand Down
2 changes: 1 addition & 1 deletion src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private function transformStaticType(Type $type, ClassMemberAccessAnswerer $scop
$isFinal = $classReflection->isFinal();
}
$type = $type->changeBaseClass($classReflection);
if (!$isFinal) {
if (!$isFinal || $type instanceof ThisType) {
return $type;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,4 +1059,9 @@ public function testBug11663(): void
$this->analyse([__DIR__ . '/data/bug-11663.php'], []);
}

public function testBug11857(): void
{
$this->analyse([__DIR__ . '/data/bug-11857-builder.php'], []);
}

}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-11857-builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php // lint >= 8.0

namespace Bug11857Builder;

class Foo
{

/**
* @param array<string, mixed> $attributes
* @return $this
*/
public function filter(array $attributes): static
{
return $this;
}

/**
* @param array<string, mixed> $attributes
* @return $this
*/
public function filterUsingRequest(array $attributes): static
{
return $this->filter($attributes);
}

}

final class FinalFoo
{

/**
* @param array<string, mixed> $attributes
* @return $this
*/
public function filter(array $attributes): static
{
return $this;
}

/**
* @param array<string, mixed> $attributes
* @return $this
*/
public function filterUsingRequest(array $attributes): static
{
return $this->filter($attributes);
}

}
Loading