-
Notifications
You must be signed in to change notification settings - Fork 41
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 #235 from carlosas/has-attribute
Add hasAttribute() selector
- Loading branch information
Showing
24 changed files
with
96 additions
and
64 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,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; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace PHPat\Test\Builder; | ||
|
||
class BuildStep extends AbstractStep | ||
{ | ||
} | ||
class BuildStep extends AbstractStep {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
class SimpleClass | ||
{ | ||
} | ||
class SimpleClass {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
class SimpleClassFive | ||
{ | ||
} | ||
class SimpleClassFive {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
class SimpleClassFour | ||
{ | ||
} | ||
class SimpleClassFour {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
class SimpleClassSix | ||
{ | ||
} | ||
class SimpleClassSix {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
class SimpleClassThree | ||
{ | ||
} | ||
class SimpleClassThree {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
class SimpleClassTwo | ||
{ | ||
} | ||
class SimpleClassTwo {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
final class SimpleFinalClass | ||
{ | ||
} | ||
final class SimpleFinalClass {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
interface SimpleInterface | ||
{ | ||
} | ||
interface SimpleInterface {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
interface SimpleInterfaceTwo | ||
{ | ||
} | ||
interface SimpleInterfaceTwo {} |
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ | |
|
||
namespace Tests\PHPat\fixtures\Simple; | ||
|
||
trait SimpleTrait | ||
{ | ||
} | ||
trait SimpleTrait {} |
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