Skip to content

Commit 3a09117

Browse files
committed
add call static support, set and get
1 parent 971356b commit 3a09117

4 files changed

Lines changed: 95 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector\Fixture;
4+
5+
final class CallStaticMethod
6+
{
7+
public function __callStatic($method, $args)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector\Fixture;
17+
18+
final class CallStaticMethod
19+
{
20+
public function __callStatic(string $method, array $args)
21+
{
22+
}
23+
}
24+
25+
?>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector\Fixture;
4+
5+
final class GetAndSet
6+
{
7+
public function __get($method)
8+
{
9+
}
10+
11+
public function __set($method, $args)
12+
{
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector\Fixture;
21+
22+
final class GetAndSet
23+
{
24+
public function __get(string $method)
25+
{
26+
}
27+
28+
public function __set(string $method, mixed $args)
29+
{
30+
}
31+
}
32+
33+
?>

rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Name;
1010
use PhpParser\Node\Stmt\Class_;
11+
use Rector\Php\PhpVersionProvider;
1112
use Rector\Rector\AbstractRector;
1213
use Rector\ValueObject\MethodName;
1314
use Rector\ValueObject\PhpVersionFeature;
@@ -22,6 +23,12 @@
2223
*/
2324
final class KnownMagicClassMethodTypeRector extends AbstractRector implements MinPhpVersionInterface
2425
{
26+
public function __construct(
27+
private readonly PhpVersionProvider $phpVersionProvider
28+
) {
29+
30+
}
31+
2532
public function getRuleDefinition(): RuleDefinition
2633
{
2734
return new RuleDefinition(
@@ -70,7 +77,7 @@ public function refactor(Node $node): ?Node
7077
continue;
7178
}
7279

73-
if ($this->isName($classMethod, MethodName::CALL)) {
80+
if ($this->isNames($classMethod, [MethodName::CALL, MethodName::CALL_STATIC])) {
7481
$firstParam = $classMethod->getParams()[0];
7582
if (! $firstParam->type instanceof Node) {
7683
$firstParam->type = new Identifier('string');
@@ -83,6 +90,30 @@ public function refactor(Node $node): ?Node
8390
$hasChanged = true;
8491
}
8592
}
93+
94+
if ($this->isName($classMethod, MethodName::__GET)) {
95+
$firstParam = $classMethod->getParams()[0];
96+
if (! $firstParam->type instanceof Node) {
97+
$firstParam->type = new Identifier('string');
98+
$hasChanged = true;
99+
}
100+
}
101+
102+
if ($this->isName($classMethod, MethodName::__SET)) {
103+
$firstParam = $classMethod->getParams()[0];
104+
if (! $firstParam->type instanceof Node) {
105+
$firstParam->type = new Identifier('string');
106+
$hasChanged = true;
107+
}
108+
109+
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
110+
$secondParam = $classMethod->getParams()[1];
111+
if (! $secondParam->type instanceof Node) {
112+
$secondParam->type = new Identifier('mixed');
113+
$hasChanged = true;
114+
}
115+
}
116+
}
86117
}
87118

88119
if ($hasChanged) {

src/ValueObject/MethodName.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ final class MethodName
5858
*/
5959
public const CALL = '__call';
6060

61+
/**
62+
* @var string
63+
*/
64+
public const CALL_STATIC = '__callStatic';
65+
6166
/**
6267
* @var string
6368
*/

0 commit comments

Comments
 (0)