Skip to content

Commit

Permalink
Merge pull request #235 from carlosas/has-attribute
Browse files Browse the repository at this point in the history
Add hasAttribute() selector
  • Loading branch information
carlosas authored Sep 27, 2023
2 parents 367a4c9 + 3858462 commit 4d7db7c
Show file tree
Hide file tree
Showing 24 changed files with 96 additions and 64 deletions.
5 changes: 5 additions & 0 deletions ci/psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@
<ArgumentTypeCoercion>
<errorLevel type="suppress"><directory name="../src/Selector/" /></errorLevel>
</ArgumentTypeCoercion>

<UndefinedDocblockClass>
<errorLevel type="suppress"><file name="../src/Selector/HasAttribute.php" /></errorLevel>
<errorLevel type="suppress"><file name="../src/Rule/Extractor/Relation/ClassAttributeExtractor.php" /></errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
</psalm>
7 changes: 5 additions & 2 deletions docs/documentation/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ The first selector will select all classes in the `App\User\Domain` namespace.
The second one will select all classes in a namespace matching the regular expression.

## Selector::implements()
Select classes that implement a given interface.
Select classes that implement the given interface.

## Selector::extends()
Select classes that extend a given class.
Select classes that extend the given class.

## Selector::interface()
Select all interfaces.

## Selector::hasAttribute()
Select classes that has the given attribute.

## Selector::enum()
Select all enums.

Expand Down
4 changes: 1 addition & 3 deletions src/Rule/Assertion/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@
/**
* @extends PHPStanRule<Node>
*/
interface Assertion extends PHPStanRule
{
}
interface Assertion extends PHPStanRule {}
58 changes: 58 additions & 0 deletions src/Selector/HasAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);

namespace PHPat\Selector;

use PHPStan\BetterReflection\Reflection\ReflectionAttribute;
use PHPStan\Reflection\ClassReflection;

final class HasAttribute implements SelectorInterface
{
private string $classname;
private bool $isRegex;

/**
* @param class-string|string $classname
*/
public function __construct(string $classname, bool $isRegex)
{
$this->classname = $classname;
$this->isRegex = $isRegex;
}

public function getName(): string
{
return $this->classname;
}

public function matches(ClassReflection $classReflection): bool
{
/** @var list<ReflectionAttribute> $attributes */
$attributes = $classReflection->getNativeReflection()->getAttributes();

if ($this->isRegex) {
return $this->matchesRegex($attributes);
}

foreach ($attributes as $attribute) {
if ($attribute->getName() === $this->classname) {
return true;
}
}

return false;
}

/**
* @param list<ReflectionAttribute> $attributes
*/
private function matchesRegex(array $attributes): bool
{
foreach ($attributes as $attribute) {
if (preg_match($this->classname, $attribute->getName()) === 1) {
return true;
}
}

return false;
}
}
8 changes: 8 additions & 0 deletions src/Selector/SelectorPrimitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ public static function extends(string $fqcn, bool $regex = false): ClassExtends
{
return new ClassExtends($fqcn, $regex);
}

/**
* @param class-string|non-empty-string $fqcn
*/
public static function hasAttribute(string $fqcn, bool $regex = false): HasAttribute
{
return new HasAttribute($fqcn, $regex);
}
}
4 changes: 1 addition & 3 deletions src/Test/Builder/BuildStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace PHPat\Test\Builder;

class BuildStep extends AbstractStep
{
}
class BuildStep extends AbstractStep {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleAbstractClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

abstract class SimpleAbstractClass
{
}
abstract class SimpleAbstractClass {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleAbstractClassTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

abstract class SimpleAbstractClassTwo
{
}
abstract class SimpleAbstractClassTwo {}
6 changes: 1 addition & 5 deletions tests/fixtures/Simple/SimpleAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@

namespace Tests\PHPat\fixtures\Simple;

use Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
class SimpleAttribute
{
}
class SimpleAttribute {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleClass
{
}
class SimpleClass {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleClassFive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleClassFive
{
}
class SimpleClassFive {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleClassFour.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleClassFour
{
}
class SimpleClassFour {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleClassSix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleClassSix
{
}
class SimpleClassSix {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleClassThree.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleClassThree
{
}
class SimpleClassThree {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleClassTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleClassTwo
{
}
class SimpleClassTwo {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleException extends \Exception
{
}
class SimpleException extends \Exception {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleExceptionTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

class SimpleExceptionTwo extends \Exception
{
}
class SimpleExceptionTwo extends \Exception {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleFinalClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

final class SimpleFinalClass
{
}
final class SimpleFinalClass {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

interface SimpleInterface
{
}
interface SimpleInterface {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleInterfaceTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

interface SimpleInterfaceTwo
{
}
interface SimpleInterfaceTwo {}
4 changes: 1 addition & 3 deletions tests/fixtures/Simple/SimpleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Tests\PHPat\fixtures\Simple;

trait SimpleTrait
{
}
trait SimpleTrait {}
4 changes: 1 addition & 3 deletions tests/fixtures/Special/ClassImplementing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Tests\PHPat\fixtures\Simple\SimpleInterface;

class ClassImplementing implements SimpleInterface
{
}
class ClassImplementing implements SimpleInterface {}
4 changes: 1 addition & 3 deletions tests/fixtures/Special/ClassWithStaticMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

class ClassWithStaticMethod
{
public static function staticMethod(): void
{
}
public static function staticMethod(): void {}
}
4 changes: 1 addition & 3 deletions tests/fixtures/Special/ClassWithStaticMethodTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

class ClassWithStaticMethodTwo
{
public static function staticMethod(): void
{
}
public static function staticMethod(): void {}
}

0 comments on commit 4d7db7c

Please sign in to comment.