Skip to content

Commit

Permalink
Merge pull request #10 from worksome/feat-remove-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Nybroe authored Feb 21, 2022
2 parents 5b8712f + 789f482 commit eb77321
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 10 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ jobs:
setup:
timeout-minutes: 30
runs-on: ubuntu-latest
name: Pest on PHP ${{ matrix.php }}
name: Pest ${{ matrix.test-suite }} on PHP ${{ matrix.php }}
strategy:
matrix:
php: ["8.0", "8.1"]
test-suite: ['phpstan', 'rector']

steps:
- name: Checkout code
Expand All @@ -25,7 +26,7 @@ jobs:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install Composer dependencies
uses: worksome/composer-install@fix-race-condition
uses: ramsey/composer-install@v2

- name: Test php code
run: vendor/bin/pest
run: vendor/bin/pest --testsuite ${{ matrix.test-suite }}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require-dev": {
"composer/composer": "^2.0",
"pestphp/pest": "^1.21",
"spatie/ray": "^1.32"
"spatie/ray": "^1.32",
"spatie/invade": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 6 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
colors="true"
>
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<testsuite name="phpstan">
<directory>tests/PHPStan</directory>
</testsuite>
<testsuite name="rector">
<directory>tests/Rector</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
57 changes: 57 additions & 0 deletions src/Rector/Generic/DisallowedAttributesRector.php
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));
}
}
3 changes: 2 additions & 1 deletion src/Rector/WorksomeSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
class WorksomeSetList implements SetListInterface
{
public const LARAVEL_CODE_QUALITY = __DIR__ . '/config/sets/laravel-code-quality.php';
}
public const GENERIC_CODE_QUALITY = __DIR__ . '/config/sets/generic-code-quality.php';
}
15 changes: 15 additions & 0 deletions src/Rector/config/sets/generic-code-quality.php
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,
]);
};
2 changes: 0 additions & 2 deletions src/Rector/config/sets/laravel-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Worksome\CodingStyle\Rector\Generic\Class_\NamespaceBasedSuffixRector;
use Worksome\CodingStyle\Rector\Generic\Class_\ParentBasedSuffixRector;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
Expand Down
1 change: 1 addition & 0 deletions src/WorksomeRectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static function setup(ContainerConfigurator $containerConfigurator)
// SetList::PHP_80,

$containerConfigurator->import(WorksomeSetList::LARAVEL_CODE_QUALITY);
$containerConfigurator->import(WorksomeSetList::GENERIC_CODE_QUALITY);

// get services (needed for register a single rule)
$services = $containerConfigurator->services();
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
*/

use Worksome\CodingStyle\Tests\PHPStan\BaseRuleTestCase;
use Worksome\CodingStyle\Tests\Rector\BaseRectorTestCase;

uses(BaseRuleTestCase::class)->in('PHPStan');
uses(BaseRectorTestCase::class)->in('Rector');

/*
|--------------------------------------------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions tests/Rector/BaseRectorTestCase.php
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';
}
}
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')
);
});
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;
}
}

?>
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;
}
}

?>
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 tests/Rector/DisallowedAttributesRector/config/configured_rule.php
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,
]);
};

0 comments on commit eb77321

Please sign in to comment.