-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
WpThemeGetDynamicMethodReturnTypeExtension.php
82 lines (69 loc) · 2.09 KB
/
WpThemeGetDynamicMethodReturnTypeExtension.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
// phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
/**
* Set return type of WP_Theme::get().
*/
declare(strict_types=1);
namespace SzepeViktor\PHPStan\WordPress;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
class WpThemeGetDynamicMethodReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{
/**
* File headers.
*
* @var list<string>
*/
private static $headers = [
'Name',
'ThemeURI',
'Description',
'Author',
'AuthorURI',
'Version',
'Template',
'Status',
'Tags',
'TextDomain',
'DomainPath',
'RequiresWP',
'RequiresPHP',
];
public function getClass(): string
{
return 'WP_Theme';
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'get';
}
/**
* @see https://developer.wordpress.org/reference/classes/wp_theme/get/
*/
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$argumentType = $scope->getType($methodCall->getArgs()[0]->value);
if (!$argumentType instanceof ConstantStringType) {
return TypeCombinator::union(
new StringType(),
new ArrayType(new IntegerType(), new StringType()),
new ConstantBooleanType(false)
);
}
if ($argumentType->getValue() === 'Tags') {
return new ArrayType(new IntegerType(), new StringType());
}
if (in_array($argumentType->getValue(), self::$headers, true)) {
return new StringType();
}
return new ConstantBooleanType(false);
}
}