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: make
, makeWith
and resolve
methods not resolving to correct type on Application and Container.
#1451
Merged
Merged
fix: make
, makeWith
and resolve
methods not resolving to correct type on Application and Container.
#1451
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace NunoMaduro\Larastan\ReturnTypes; | ||
|
||
use NunoMaduro\Larastan\Concerns\HasContainer; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Scalar\String_; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use Throwable; | ||
|
||
final class AppMakeHelper | ||
{ | ||
use HasContainer; | ||
|
||
public function resolveTypeFromCall(FuncCall|MethodCall|StaticCall $call): Type | ||
{ | ||
if (count($call->getArgs()) === 0) { | ||
return new ErrorType(); | ||
} | ||
|
||
$expr = $call->getArgs()[0]->value; | ||
if ($expr instanceof String_) { | ||
try { | ||
/** @var object|null $resolved */ | ||
$resolved = $this->resolve($expr->value); | ||
|
||
if ($resolved === null) { | ||
return new ErrorType(); | ||
} | ||
|
||
return new ObjectType(get_class($resolved)); | ||
} catch (Throwable $exception) { | ||
return new ErrorType(); | ||
} | ||
} | ||
|
||
if ($expr instanceof ClassConstFetch && $expr->class instanceof FullyQualified) { | ||
return new ObjectType($expr->class->toString()); | ||
} | ||
|
||
return new NeverType(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/ReturnTypes/ApplicationMakeDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\ReturnTypes; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
|
||
final class ApplicationMakeDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function __construct( | ||
private AppMakeHelper $appMakeHelper | ||
) { | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return Application::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return in_array($methodReflection->getName(), ['make', 'makeWith', 'resolve'], true); | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
return $this->appMakeHelper->resolveTypeFromCall($methodCall); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/ReturnTypes/ContainerMakeDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\ReturnTypes; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
|
||
final class ContainerMakeDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function __construct( | ||
private AppMakeHelper $appMakeHelper | ||
) { | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return Container::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return in_array($methodReflection->getName(), ['make', 'makeWith', 'resolve'], true); | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
return $this->appMakeHelper->resolveTypeFromCall($methodCall); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace ApplicationMake; | ||
|
||
use Illuminate\Contracts\Config\Repository; | ||
use function PHPStan\Testing\assertType; | ||
|
||
/** @var \Illuminate\Foundation\Application $app */ | ||
assertType('Illuminate\Contracts\Config\Repository', $app->make(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $app->makeWith(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $app->resolve(Repository::class)); | ||
|
||
/** @var \Illuminate\Contracts\Foundation\Application $app */ | ||
assertType('Illuminate\Contracts\Config\Repository', $app->make(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $app->makeWith(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $app->resolve(Repository::class)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace ContainerMake; | ||
|
||
use Illuminate\Contracts\Config\Repository; | ||
use function PHPStan\Testing\assertType; | ||
|
||
/** @var \Illuminate\Container\Container $container */ | ||
assertType('Illuminate\Contracts\Config\Repository', $container->make(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $container->makeWith(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $container->resolve(Repository::class)); | ||
|
||
/** @var \Illuminate\Contracts\Container\Container $container */ | ||
assertType('Illuminate\Contracts\Config\Repository', $container->make(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $container->makeWith(Repository::class)); | ||
assertType('Illuminate\Contracts\Config\Repository', $container->resolve(Repository::class)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not really guaranteed that
resolve
will return an object, scalars can be bound into the container using constant string names too and i've seen that in the wild, so it might be worth expanding this code to anticipate more types to be returned in the future