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: use Scope to get type of arg in AppMakeHelper #1506

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ReturnTypes/AppMakeDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
return $this->appMakeHelper->resolveTypeFromCall($methodCall);
return $this->appMakeHelper->resolveTypeFromCall($methodCall, $scope);
}
}
44 changes: 24 additions & 20 deletions src/ReturnTypes/AppMakeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,52 @@
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\Analyser\Scope;
use PHPStan\Type\ErrorType;
use PHPStan\Type\NeverType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Throwable;

final class AppMakeHelper
{
use HasContainer;

public function resolveTypeFromCall(FuncCall|MethodCall|StaticCall $call): Type
public function resolveTypeFromCall(FuncCall|MethodCall|StaticCall $call, Scope $scope): Type
{
if (count($call->getArgs()) === 0) {
$args = $call->getArgs();
if (count($args) === 0) {
return new ErrorType();
}

$expr = $call->getArgs()[0]->value;
if ($expr instanceof String_) {
try {
/** @var object|null $resolved */
$resolved = $this->resolve($expr->value);
$argType = $scope->getType($args[0]->value);

if ($resolved === null) {
$constantStrings = $argType->getConstantStrings();

if (count($constantStrings) > 0) {
$types = [];
foreach ($constantStrings as $constantString) {
try {
/** @var object|null $resolved */
$resolved = $this->resolve($constantString->getValue());

if ($resolved === null) {
return new ErrorType();
}

$types[] = new ObjectType(get_class($resolved));
} catch (Throwable $exception) {
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 count($types) === 1 ? $types[0] : TypeCombinator::union(...$types);
}

return new NeverType();
return new MixedType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
return $this->appMakeHelper->resolveTypeFromCall($methodCall);
return $this->appMakeHelper->resolveTypeFromCall($methodCall, $scope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
return $this->appMakeHelper->resolveTypeFromCall($methodCall);
return $this->appMakeHelper->resolveTypeFromCall($methodCall, $scope);
}
}
2 changes: 1 addition & 1 deletion src/ReturnTypes/Helpers/AppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function getTypeFromFunctionCall(
return new ObjectType(Application::class);
}

return $this->appMakeHelper->resolveTypeFromCall($functionCall);
return $this->appMakeHelper->resolveTypeFromCall($functionCall, $scope);
}
}
26 changes: 18 additions & 8 deletions tests/Type/data/application-make.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
namespace ApplicationMake;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Model;
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));
function doFoo(\Illuminate\Foundation\Application $app, \Illuminate\Contracts\Foundation\Application $app2)
{
assertType('Illuminate\Config\Repository', $app->make(Repository::class));
assertType('Illuminate\Config\Repository', $app->makeWith(Repository::class));
assertType('Illuminate\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));
assertType('Illuminate\Config\Repository', $app2->make(Repository::class));
assertType('Illuminate\Config\Repository', $app2->makeWith(Repository::class));
assertType('Illuminate\Config\Repository', $app2->resolve(Repository::class));
}

/** @param class-string<Model> $foo */
function doBar(string $foo, \Illuminate\Foundation\Application $app)
{
assertType('mixed', $app->make($foo));
assertType('mixed', $app->makeWith($foo));
assertType('mixed', $app->resolve($foo));
}
26 changes: 18 additions & 8 deletions tests/Type/data/container-make.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
namespace ContainerMake;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Model;
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));
function doFoo(\Illuminate\Container\Container $container, \Illuminate\Contracts\Container\Container $container2)
{
assertType('Illuminate\Config\Repository', $container->make(Repository::class));
assertType('Illuminate\Config\Repository', $container->makeWith(Repository::class));
assertType('Illuminate\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));
assertType('Illuminate\Config\Repository', $container2->make(Repository::class));
assertType('Illuminate\Config\Repository', $container2->makeWith(Repository::class));
assertType('Illuminate\Config\Repository', $container2->resolve(Repository::class));
}

/** @param class-string<Model> $foo */
function doBar(string $foo, \Illuminate\Contracts\Container\Container $container)
{
assertType('mixed', $container->make($foo));
assertType('mixed', $container->makeWith($foo));
assertType('mixed', $container->resolve($foo));
}