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 12327b0
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 @@ -39,7 +39,11 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope,
): Type {
): Type|null {
if ($methodReflection->getDeclaringClass()->getName() !== Request::class) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

$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 12327b0

Please sign in to comment.