Skip to content

Commit c06c135

Browse files
committed
skip get/set magic
1 parent 9449769 commit c06c135

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Class_\PropertyHookRector\Fixture;
4+
5+
final class SkipGetSetMagic
6+
{
7+
private string $name;
8+
9+
public function getName(): string
10+
{
11+
return $this->name;
12+
}
13+
14+
public function setName(string $name): void
15+
{
16+
$this->name = ucfirst($name);
17+
}
18+
19+
public function __get($name)
20+
{
21+
}
22+
23+
public function __set($name, $value)
24+
{
25+
}
26+
}

rules/Php84/Rector/Class_/PropertyHookRector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\Php84\NodeFactory\PropertyHookFactory;
1414
use Rector\Php84\NodeFinder\SetterAndGetterFinder;
1515
use Rector\Rector\AbstractRector;
16+
use Rector\ValueObject\MethodName;
1617
use Rector\ValueObject\PhpVersionFeature;
1718
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
1819
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
@@ -82,6 +83,10 @@ public function refactor(Node $node): ?Node
8283
return null;
8384
}
8485

86+
if ($this->hasMagicGetSetMethod($node)) {
87+
return null;
88+
}
89+
8590
// nothing to hook to
8691
if ($node->getProperties() === []) {
8792
return null;
@@ -149,4 +154,15 @@ public function provideMinPhpVersion(): int
149154
{
150155
return PhpVersionFeature::PROPERTY_HOOKS;
151156
}
157+
158+
private function hasMagicGetSetMethod(Class_ $class): bool
159+
{
160+
$magicGetMethod = $class->getMethod(MethodName::__GET);
161+
if ($magicGetMethod instanceof ClassMethod) {
162+
return true;
163+
}
164+
165+
$magicSetMethod = $class->getMethod(MethodName::__SET);
166+
return $magicSetMethod instanceof ClassMethod;
167+
}
152168
}

0 commit comments

Comments
 (0)