Skip to content

Commit 3ffe801

Browse files
committed
add call static support, set and get
1 parent dac05e6 commit 3ffe801

4 files changed

Lines changed: 105 additions & 0 deletions

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: 42 additions & 0 deletions
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
<<<<<<< HEAD
@@ -27,9 +28,17 @@
2728
final class KnownMagicClassMethodTypeRector extends AbstractRector implements MinPhpVersionInterface
2829
{
2930
public function __construct(
31+
<<<<<<< HEAD
3032
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
3133
){
3234
}
35+
=======
36+
private readonly PhpVersionProvider $phpVersionProvider
37+
) {
38+
39+
}
40+
41+
>>>>>>> 3a091179f1 (add call static support, set and get)
3342
public function getRuleDefinition(): RuleDefinition
3443
{
3544
return new RuleDefinition(
@@ -78,9 +87,18 @@ public function refactor(Node $node): ?Node
7887
continue;
7988
}
8089

90+
<<<<<<< HEAD
8191
if (! $this->isName($classMethod, MethodName::CALL)) {
8292
continue;
8393
}
94+
=======
95+
if ($this->isNames($classMethod, [MethodName::CALL, MethodName::CALL_STATIC])) {
96+
$firstParam = $classMethod->getParams()[0];
97+
if (! $firstParam->type instanceof Node) {
98+
$firstParam->type = new Identifier('string');
99+
$hasChanged = true;
100+
}
101+
>>>>>>> 3a091179f1 (add call static support, set and get)
84102

85103
if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) {
86104
return null;
@@ -97,6 +115,30 @@ public function refactor(Node $node): ?Node
97115
$secondParam->type = new Name('array');
98116
$hasChanged = true;
99117
}
118+
119+
if ($this->isName($classMethod, MethodName::__GET)) {
120+
$firstParam = $classMethod->getParams()[0];
121+
if (! $firstParam->type instanceof Node) {
122+
$firstParam->type = new Identifier('string');
123+
$hasChanged = true;
124+
}
125+
}
126+
127+
if ($this->isName($classMethod, MethodName::__SET)) {
128+
$firstParam = $classMethod->getParams()[0];
129+
if (! $firstParam->type instanceof Node) {
130+
$firstParam->type = new Identifier('string');
131+
$hasChanged = true;
132+
}
133+
134+
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
135+
$secondParam = $classMethod->getParams()[1];
136+
if (! $secondParam->type instanceof Node) {
137+
$secondParam->type = new Identifier('mixed');
138+
$hasChanged = true;
139+
}
140+
}
141+
}
100142
}
101143

102144
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)