-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from worksome/feat-remove-attributes
- Loading branch information
Showing
15 changed files
with
216 additions
and
10 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
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,57 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Rector\Generic; | ||
|
||
use Illuminate\Support\Collection; | ||
use PhpParser\Node; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
class DisallowedAttributesRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** @var array<class-string> */ | ||
private array $disallowedAttributes = []; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Remove attributes which are not allowed.', [ | ||
new CodeSample( | ||
<<<PHP | ||
#[NotAllowed] | ||
class MyClass {} | ||
PHP, | ||
"class MyClass {}", | ||
) | ||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Node\AttributeGroup::class]; | ||
} | ||
|
||
public function configure(array $configuration) : void | ||
{ | ||
$this->disallowedAttributes = $configuration; | ||
} | ||
|
||
/** | ||
* @param Node\AttributeGroup $node | ||
*/ | ||
public function refactor(Node $node) | ||
{ | ||
Collection::make($node->attrs)->reject(function (Node\Attribute $node) { | ||
foreach ($this->disallowedAttributes as $disallowedAttribute) { | ||
if ($this->isName($node, $disallowedAttribute)) { | ||
$this->removeNode($node); | ||
return true; | ||
} | ||
} | ||
return false; | ||
})->whenEmpty(fn() => $this->removeNode($node)); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Worksome\CodingStyle\Rector\Generic\DisallowedAttributesRector; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
|
||
$services->set(DisallowedAttributesRector::class) | ||
->configure([ | ||
JetBrains\PhpStorm\Pure::class, | ||
]); | ||
}; |
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
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,17 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\Rector; | ||
|
||
use Illuminate\Support\Str; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
/** | ||
* @property-read string $__filename | ||
*/ | ||
abstract class BaseRectorTestCase extends AbstractRectorTestCase | ||
{ | ||
public function provideConfigFilePath(): string | ||
{ | ||
return Str::beforeLast(invade($this)->__filename, "/") . '/config/configured_rule.php'; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Rector/DisallowedAttributesRector/DisallowedAttributesRectorTest.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 | ||
|
||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
it('can remove disallowed attributes', function () { | ||
$this->doTestFileInfo( | ||
new SmartFileInfo(__DIR__ . '/Fixture/class_with_disallowed_attribute.php.inc') | ||
); | ||
}); | ||
|
||
it('does not remove allowed attributes', function () { | ||
$this->doTestFileInfo( | ||
new SmartFileInfo(__DIR__ . '/Fixture/class_with_allowed_attribute.php.inc') | ||
); | ||
}); | ||
|
||
it('only deletes disallowed attribute from attribute group', function () { | ||
$this->doTestFileInfo( | ||
new SmartFileInfo(__DIR__ . '/Fixture/class_with_disallowed_attribute_in_group.php.inc') | ||
); | ||
}); |
13 changes: 13 additions & 0 deletions
13
tests/Rector/DisallowedAttributesRector/Fixture/class_with_allowed_attribute.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,13 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\Rector\DisallowedAttributesRector\Fixture; | ||
|
||
class ClassWithAllowedAttribute { | ||
#[Something] | ||
public function pureMethod() | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
30 changes: 30 additions & 0 deletions
30
tests/Rector/DisallowedAttributesRector/Fixture/class_with_disallowed_attribute.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,30 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\Rector\DisallowedAttributesRector\Fixture; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
|
||
class ClassWithDisallowedAttribute { | ||
#[Pure] | ||
public function pureMethod() | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\Rector\DisallowedAttributesRector\Fixture; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
|
||
class ClassWithDisallowedAttribute { | ||
public function pureMethod() | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...ector/DisallowedAttributesRector/Fixture/class_with_disallowed_attribute_in_group.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,31 @@ | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\Rector\DisallowedAttributesRector\Fixture; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
|
||
class ClassWithDisallowedAttributeInGroup { | ||
#[Pure, Other] | ||
public function pureMethod() | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Worksome\CodingStyle\Tests\Rector\DisallowedAttributesRector\Fixture; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
|
||
class ClassWithDisallowedAttributeInGroup { | ||
#[Other] | ||
public function pureMethod() | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
tests/Rector/DisallowedAttributesRector/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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Worksome\CodingStyle\Rector\Generic\DisallowedAttributesRector; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
|
||
$services->set(DisallowedAttributesRector::class) | ||
->configure([ | ||
JetBrains\PhpStorm\Pure::class, | ||
]); | ||
}; |