Skip to content

Commit

Permalink
Fix not respecting return type of overridden Request::user() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller committed May 20, 2024
1 parent f934f35 commit 4e9e12b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ReturnTypes/RequestUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Illuminate\Http\Request;
use Larastan\Larastan\Concerns\HasContainer;
use Larastan\Larastan\Concerns\LoadsAuthModel;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PhpParser\Node\Expr\MethodCall;

use function array_map;
use function count;
Expand All @@ -40,6 +40,10 @@ public function getTypeFromMethodCall(
MethodCall $methodCall,
Scope $scope,
): Type {
if ($methodReflection->getDeclaringClass()->getName() !== Request::class) {
return null;

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - L^9.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - L^9.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - L^11.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - L^11.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - L^9.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 - L^9.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - L^10.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 - L^9.0 ↓

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - L^10.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.

Check failure on line 44 in src/ReturnTypes/RequestUserExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - L^10.0 ↑

Method Larastan\Larastan\ReturnTypes\RequestUserExtension::getTypeFromMethodCall() should return PHPStan\Type\Type but returns null.
}

$config = $this->getContainer()->get('config');
$authModels = [];

Expand Down
1 change: 1 addition & 0 deletions tests/Type/GeneralTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function dataFileAsserts(): iterable
yield from self::gatherAssertTypes(__DIR__ . '/data/benchmark.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-1346.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-1565.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-1718.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-1760.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-1830.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/carbon.php');
Expand Down
31 changes: 31 additions & 0 deletions tests/Type/data/bug-1718.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bug1718;

use Illuminate\Foundation\Http\FormRequest;
use App\User;
use function PHPStan\Testing\assertType;

class AuthedRequest extends FormRequest
{
public function authorize(): bool
{
return parent::user() instanceof User;
}

public function user($guard = null): User
{
$user = parent::user($guard);
if (!$user instanceof User) {
abort(403);
}

return $user;
}
}

function test(AuthedRequest $request, FormRequest $formRequest): void
{
assertType('App\User', $request->user());
assertType('App\User|null', $formRequest->user());
}

0 comments on commit 4e9e12b

Please sign in to comment.