Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix make(), makeWith() and resolve() methods not resolving to correct…
… type on Application and Container.
- Loading branch information
1 parent
d8d0141
commit 416ab65
Showing
10 changed files
with
178 additions
and
66 deletions.
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)); |