generated from ibexa/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-8534 and #11 Key changes: * Implemented the rule for replacing magic properties with getters * Configured the magic getters replacement rule for `Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest`
- Loading branch information
Showing
7 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rector\Rule; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class PropertyToGetterRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** @var array<string, array<string, string>> */ | ||
private array $classPropertyToGetterMap = []; | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change direct property access to getter method for specified classes and properties', [new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
private $property; | ||
public function getProperty() {} | ||
} | ||
$instance = new SomeClass(); | ||
$instance->property; | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
$instance = new SomeClass(); | ||
$instance->getProperty(); | ||
CODE_SAMPLE, | ||
['var_dump'] | ||
)]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [PropertyFetch::class]; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\PropertyFetch $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$className = $this->resolveClassName($node); | ||
if ($className === null || !isset($this->classPropertyToGetterMap[$className])) { | ||
return null; | ||
} | ||
|
||
$propertyName = $this->getName($node->name); | ||
|
||
if (!isset($this->classPropertyToGetterMap[$className][$propertyName])) { | ||
return null; | ||
} | ||
|
||
$getterMethodName = $this->classPropertyToGetterMap[$className][$propertyName]; | ||
|
||
return $this->nodeFactory->createMethodCall($node->var, $getterMethodName); | ||
} | ||
|
||
private function resolveClassName(PropertyFetch $propertyFetch): ?string | ||
{ | ||
$type = $this->nodeTypeResolver->getType($propertyFetch->var); | ||
|
||
if ($type instanceof ObjectType) { | ||
return $type->getClassName(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function configure(array $configuration): void | ||
{ | ||
$this->classPropertyToGetterMap = $configuration; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/lib/Rule/PropertyToGetterRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\PropertyToGetterRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public ?int $foo; | ||
|
||
public function getFoo(): ?int | ||
{ | ||
return $this->foo; | ||
} | ||
} | ||
|
||
$some = new SomeClass(); | ||
$some->foo; | ||
|
||
?> | ||
----- | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\PropertyToGetterRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public ?int $foo; | ||
|
||
public function getFoo(): ?int | ||
{ | ||
return $this->foo; | ||
} | ||
} | ||
|
||
$some = new SomeClass(); | ||
$some->getFoo(); | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
tests/lib/Rule/PropertyToGetterRector/PropertyToGetterRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rector\Tests\Rule\PropertyToGetterRector; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class PropertyToGetterRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @throws \Rector\Exception\ShouldNotHappenException | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): \Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/lib/Rule/PropertyToGetterRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
use Ibexa\Rector\Rule\PropertyToGetterRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->ruleWithConfiguration( | ||
PropertyToGetterRector::class, | ||
[ | ||
'Ibexa\Rector\Tests\Rule\PropertyToGetterRector\Fixture\SomeClass' => [ | ||
'foo' => 'getFoo', | ||
], | ||
] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/lib/Sets/Ibexa50/Fixture/simplified_request_properties.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Ibexa\Rector\Tests\Sets\Ibexa50\Fixture; | ||
|
||
use Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest; | ||
|
||
$request = new SimplifiedRequest(); | ||
$pathInfo = $request->pathinfo; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ibexa\Rector\Tests\Sets\Ibexa50\Fixture; | ||
|
||
use Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest; | ||
|
||
$request = new SimplifiedRequest(); | ||
$pathInfo = $request->getPathInfo(); | ||
|
||
?> |