/
AppMakeHelper.php
50 lines (41 loc) · 1.33 KB
/
AppMakeHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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();
}
}