Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not respecting return type of overridden Request::user() method. #1861

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ReturnTypes/RequestUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
return $methodReflection->getName() === 'user';
}

public function getTypeFromMethodCall(

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.

Check failure on line 38 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() never returns null so it can be removed from the return type.
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope,
): Type {
): Type|null {
if ($methodReflection->getDeclaringClass()->getName() !== Request::class) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
mad-briller marked this conversation as resolved.
Show resolved Hide resolved
}

$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());
}