Skip to content

Commit

Permalink
Add OneOfSelectorModifier.php
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Sep 1, 2024
1 parent f85bf01 commit 83e97d6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Selector/Modifier/OneOfSelectorModifier.php
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;
}
}

0 comments on commit 83e97d6

Please sign in to comment.