Skip to content

Commit fea5ed6

Browse files
committed
add isset/unset keys
1 parent 3ffe801 commit fea5ed6

3 files changed

Lines changed: 61 additions & 41 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector\Fixture;
4+
5+
final class IssetUnset
6+
{
7+
public function __isset($key): bool
8+
{
9+
return true;
10+
}
11+
12+
public function __unset($key): void
13+
{
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\KnownMagicClassMethodTypeRector\Fixture;
22+
23+
final class IssetUnset
24+
{
25+
public function __isset(string $key): bool
26+
{
27+
return true;
28+
}
29+
30+
public function __unset(string $key): void
31+
{
32+
}
33+
}
34+
35+
?>

rules/TypeDeclaration/Rector/ClassMethod/KnownMagicClassMethodTypeRector.php

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
use Rector\Php\PhpVersionProvider;
1212
use Rector\Rector\AbstractRector;
1313
use Rector\ValueObject\MethodName;
14-
<<<<<<< HEAD
15-
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
16-
=======
1714
use Rector\ValueObject\PhpVersionFeature;
15+
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
1816
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
19-
>>>>>>> 971356b355 (add scalar types condition)
2017
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2118
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2219

@@ -28,17 +25,12 @@
2825
final class KnownMagicClassMethodTypeRector extends AbstractRector implements MinPhpVersionInterface
2926
{
3027
public function __construct(
31-
<<<<<<< HEAD
32-
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
33-
){
34-
}
35-
=======
36-
private readonly PhpVersionProvider $phpVersionProvider
28+
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
29+
private readonly PhpVersionProvider $phpVersionProvider,
3730
) {
3831

3932
}
4033

41-
>>>>>>> 3a091179f1 (add call static support, set and get)
4234
public function getRuleDefinition(): RuleDefinition
4335
{
4436
return new RuleDefinition(
@@ -87,50 +79,38 @@ public function refactor(Node $node): ?Node
8779
continue;
8880
}
8981

90-
<<<<<<< HEAD
91-
if (! $this->isName($classMethod, MethodName::CALL)) {
92-
continue;
93-
}
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)
102-
10382
if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) {
10483
return null;
10584
}
10685

107-
$firstParam = $classMethod->getParams()[0];
108-
if (! $firstParam->type instanceof Node) {
109-
$firstParam->type = new Identifier('string');
110-
$hasChanged = true;
111-
}
112-
113-
$secondParam = $classMethod->getParams()[1];
114-
if (! $secondParam->type instanceof Node) {
115-
$secondParam->type = new Name('array');
116-
$hasChanged = true;
117-
}
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');
86+
if ($this->isNames($classMethod, [MethodName::CALL, MethodName::CALL_STATIC])) {
87+
$secondParam = $classMethod->getParams()[1];
88+
if (! $secondParam->type instanceof Node) {
89+
$secondParam->type = new Name('array');
12390
$hasChanged = true;
12491
}
12592
}
12693

127-
if ($this->isName($classMethod, MethodName::__SET)) {
94+
// first arg string
95+
if ($this->isNames(
96+
$classMethod,
97+
[
98+
MethodName::CALL,
99+
MethodName::CALL_STATIC,
100+
MethodName::__SET,
101+
MethodName::__GET,
102+
MethodName::ISSET,
103+
MethodName::UNSET,
104+
]
105+
)) {
128106
$firstParam = $classMethod->getParams()[0];
129107
if (! $firstParam->type instanceof Node) {
130108
$firstParam->type = new Identifier('string');
131109
$hasChanged = true;
132110
}
111+
}
133112

113+
if ($this->isName($classMethod, MethodName::__SET)) {
134114
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
135115
$secondParam = $classMethod->getParams()[1];
136116
if (! $secondParam->type instanceof Node) {

src/ValueObject/MethodName.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ final class MethodName
7777
* @var string
7878
*/
7979
public const ISSET = '__isset';
80+
81+
/**
82+
* @var string
83+
*/
84+
public const UNSET = '__unset';
8085
}

0 commit comments

Comments
 (0)