Skip to content

Commit

Permalink
Create XorModifier.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 Aug 23, 2024
1 parent 4c29e33 commit 358508e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Selector/Modifier/XorModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace PHPat\Selector\Modifier;

use PHPat\Selector\SelectorInterface;
use PHPStan\Reflection\ClassReflection;

final class XorModifier implements SelectorInterface
{
/** @var array<SelectorInterface> */
private array $selectors;

public function __construct(SelectorInterface ...$selector)
{
$this->selectors = array_values($selector);
}

public function matches(ClassReflection $classReflection): bool
{
$matches = 0;

foreach ($this->selectors as $selector) {
if ($selector->matches($classReflection)) {
++$matches;
}
}

return $matches === 1;
}

public function getName(): string
{
return implode(
':xor:',
array_map(static fn (SelectorInterface $selector) => $selector->getName(), $this->selectors),
);
}
}

0 comments on commit 358508e

Please sign in to comment.