-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
ReturnTypeFromStrictNativeCallRector.php
105 lines (91 loc) · 2.9 KB
/
ReturnTypeFromStrictNativeCallRector.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\MixedType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictNativeFunctionReturnTypeAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector\ReturnTypeFromStrictNativeCallRectorTest
*/
final class ReturnTypeFromStrictNativeCallRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly StrictNativeFunctionReturnTypeAnalyzer $strictNativeFunctionReturnTypeAnalyzer,
private readonly TypeFactory $typeFactory,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add strict return type based native function or class method return', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
return strlen('value');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(): int
{
return strlen('value');
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Closure::class, Function_::class];
}
/**
* @param ClassMethod|Closure|Function_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->returnType !== null) {
return null;
}
$nativeCallLikes = $this->strictNativeFunctionReturnTypeAnalyzer->matchAlwaysReturnNativeCallLikes($node);
if ($nativeCallLikes === null) {
return null;
}
$callLikeTypes = [];
foreach ($nativeCallLikes as $nativeCallLike) {
$callLikeTypes[] = $this->getType($nativeCallLike);
}
$returnType = $this->typeFactory->createMixedPassedOrUnionType($callLikeTypes);
if ($returnType instanceof MixedType) {
return null;
}
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
return null;
}
$node->returnType = $returnTypeNode;
return $node;
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_70;
}
}