Skip to content

Commit ac2f93c

Browse files
committed
add isset/unset keys
1 parent 3a09117 commit ac2f93c

3 files changed

Lines changed: 52 additions & 13 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: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,25 @@ public function refactor(Node $node): ?Node
7878
}
7979

8080
if ($this->isNames($classMethod, [MethodName::CALL, MethodName::CALL_STATIC])) {
81-
$firstParam = $classMethod->getParams()[0];
82-
if (! $firstParam->type instanceof Node) {
83-
$firstParam->type = new Identifier('string');
84-
$hasChanged = true;
85-
}
86-
8781
$secondParam = $classMethod->getParams()[1];
8882
if (! $secondParam->type instanceof Node) {
8983
$secondParam->type = new Name('array');
9084
$hasChanged = true;
9185
}
9286
}
9387

94-
if ($this->isName($classMethod, MethodName::__GET)) {
88+
// first arg string
89+
if ($this->isNames(
90+
$classMethod,
91+
[
92+
MethodName::CALL,
93+
MethodName::CALL_STATIC,
94+
MethodName::__SET,
95+
MethodName::__GET,
96+
MethodName::ISSET,
97+
MethodName::UNSET,
98+
]
99+
)) {
95100
$firstParam = $classMethod->getParams()[0];
96101
if (! $firstParam->type instanceof Node) {
97102
$firstParam->type = new Identifier('string');
@@ -100,12 +105,6 @@ public function refactor(Node $node): ?Node
100105
}
101106

102107
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-
109108
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
110109
$secondParam = $classMethod->getParams()[1];
111110
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)