-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
- Loading branch information
1 parent
f85bf01
commit 83e97d6
Showing
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Selector\Modifier; | ||
|
||
use PHPat\Selector\SelectorInterface; | ||
use PHPStan\Reflection\ClassReflection; | ||
|
||
final class OneOfSelectorModifier implements SelectorInterface | ||
{ | ||
/** @var array<SelectorInterface> */ | ||
private array $selectors; | ||
|
||
public function __construct(SelectorInterface ...$selectors) | ||
{ | ||
$this->selectors = $selectors; | ||
} | ||
|
||
#[\Override] | ||
public function getName(): string | ||
{ | ||
return \sprintf( | ||
'one of: [ %s ]', | ||
\implode(' and ', \array_map(static fn ($selector) => $selector->getName(), $this->selectors)), | ||
); | ||
} | ||
|
||
#[\Override] | ||
public function matches(ClassReflection $classReflection): bool | ||
{ | ||
$matches = 0; | ||
foreach ($this->selectors as $selector) { | ||
if ($selector->matches($classReflection)) { | ||
++$matches; | ||
} | ||
|
||
if ($matches > 1) { | ||
return false; | ||
} | ||
} | ||
|
||
return $matches === 1; | ||
} | ||
} |