Navigation Menu

Skip to content

Commit

Permalink
Remove duplicationMappers from AlternationQuotable
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Jun 18, 2021
1 parent c17e6fb commit 43aa2ee
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 153 deletions.
Expand Up @@ -35,6 +35,6 @@ public function buildPattern(string $delimiterable, Quotable $pattern): string

public function getAlternationFactory(): AlterationFactory
{
return new AlterationFactory('');
return new AlterationFactory();
}
}
Expand Up @@ -26,6 +26,6 @@ public function buildPattern(string $delimiterable, Quotable $pattern): string

public function getAlternationFactory(): AlterationFactory
{
return new AlterationFactory($this->flags);
return new AlterationFactory();
}
}
15 changes: 2 additions & 13 deletions src/CleanRegex/Internal/Prepared/Quotable/AlternationQuotable.php
Expand Up @@ -9,13 +9,10 @@ class AlternationQuotable implements Quotable
{
/** @var array */
private $userInputs;
/** @var callable|null */
private $duplicateMapper;

public function __construct(array $userInputs, ?callable $duplicateMapper)
public function __construct(array $userInputs)
{
$this->userInputs = $userInputs;
$this->duplicateMapper = $duplicateMapper;
}

public function quote(string $delimiter): string
Expand All @@ -28,7 +25,7 @@ private function normalizedUserInput(): array
foreach ($this->userInputs as $input) {
$this->validateQuotable($input);
}
return $this->removeDuplicates($this->userInputEmptyLast());
return $this->userInputEmptyLast();
}

private function validateQuotable($quoteable): void
Expand All @@ -49,12 +46,4 @@ private function userInputEmptyLast(): array
$result[] = '';
return $result;
}

private function removeDuplicates(array $values): array
{
if ($this->duplicateMapper) {
return \array_intersect_key($values, \array_unique(\array_map($this->duplicateMapper, $values)));
}
return \array_unique($values);
}
}
Expand Up @@ -9,34 +9,15 @@

class AlterationFactory implements QuotableFactory
{
/** @var string */
private $flags;

public function __construct(string $flags)
{
$this->flags = $flags;
}

public function quotable($value): Quotable
{
if (\is_string($value)) {
return new UserInputQuotable($value);
}
if (\is_array($value)) {
return new AlternationQuotable($value, $this->duplicateMapper());
return new AlternationQuotable($value);
}
$type = Type::asString($value);
throw new InvalidArgumentException("Invalid bound value. Expected string, but $type given");
}

private function duplicateMapper(): ?callable
{
if (\strpos($this->flags, 'i') !== false) {
if (\strpos($this->flags, 'u') !== false) {
return 'mb_strToLower';
}
return 'strToLower';
}
return null;
}
}

This file was deleted.

Expand Up @@ -63,7 +63,7 @@ public function shouldNotThrow_trailingSlash_lastArray(): void
$parser = new PreparedParser(['first\\', ['last\\']]);

// when
$parsed = $parser->parse('/', new AlterationFactory(''));
$parsed = $parser->parse('/', new AlterationFactory());

// then
$this->assertSame('first\\last\\\\', $parsed->quote('/'));
Expand Down
Expand Up @@ -19,7 +19,7 @@ public function shouldParse(): void
$parser = new TemplateParser('foo:&', [new RawToken('W', '#')]);

// when
$result = $parser->parse('#', new AlterationFactory(''));
$result = $parser->parse('#', new AlterationFactory());

// then
$this->assertSame('foo:W', $result->quote('#'));
Expand All @@ -37,7 +37,7 @@ public function shouldThrow_onTrailingBackslash(): void
$this->expectException(TrailingBackslashException::class);

// when
$parser->parse('#', new AlterationFactory(''));
$parser->parse('#', new AlterationFactory());
}

/**
Expand Down
Expand Up @@ -3,7 +3,6 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Test\Utils\Functions;
use TRegx\CleanRegex\Internal\Prepared\Quotable\AlternationQuotable;

class AlternationQuotableTest extends TestCase
Expand All @@ -29,7 +28,7 @@ public function shouldQuote()
public function shouldQuoteDelimiter()
{
// given
$quotable = new AlternationQuotable(['a', '%b'], null);
$quotable = new AlternationQuotable(['a', '%b']);

// when
$result = $quotable->quote('%');
Expand All @@ -38,43 +37,13 @@ public function shouldQuoteDelimiter()
$this->assertSame('(?:a|\%b)', $result);
}

/**
* @test
*/
public function shouldRemoveDuplicates_caseSensitive()
{
// given
$quotable = new AlternationQuotable(['a', 'FOO', 'a', 'c', 'foo'], Functions::identity());

// when
$result = $quotable->quote('/');

// then
$this->assertSame('(?:a|FOO|c|foo)', $result);
}

/**
* @test
*/
public function shouldRemoveDuplicates_caseInsensitive()
{
// given
$quotable = new AlternationQuotable(['a', 'FOO', 'a', 'a', 'c', 'foo'], 'strToLower');

// when
$result = $quotable->quote('/');

// then
$this->assertSame('(?:a|FOO|c)', $result);
}

/**
* @test
*/
public function shouldAddAnEmptyProduct_toIndicateAnEmptyString()
{
// given
$quotable = new AlternationQuotable(['a', '', '', 'b'], null);
$quotable = new AlternationQuotable(['a', '', '', 'b']);

// when
$result = $quotable->quote('/');
Expand All @@ -86,10 +55,10 @@ public function shouldAddAnEmptyProduct_toIndicateAnEmptyString()
/**
* @test
*/
public function shouldIgnoreOtherCharacters()
public function shouldNotRemoveFalsyStrings()
{
// given
$quotable = new AlternationQuotable(['|', ' ', '0'], null);
$quotable = new AlternationQuotable(['|', ' ', '0']);

// when
$result = $quotable->quote('/');
Expand All @@ -104,7 +73,7 @@ public function shouldIgnoreOtherCharacters()
public function shouldThrowForArrayValues()
{
// given
$quotable = new AlternationQuotable(['|', []], null);
$quotable = new AlternationQuotable(['|', []]);

// then
$this->expectException(InvalidArgumentException::class);
Expand Down
Expand Up @@ -13,7 +13,7 @@ class AlterationFactoryTest extends TestCase
public function shouldCreateQuotableString()
{
// given
$factory = new AlterationFactory('');
$factory = new AlterationFactory();

// when
$quoteable = $factory->quotable('5% you (are|is) welcome');
Expand All @@ -28,7 +28,7 @@ public function shouldCreateQuotableString()
public function shouldQuoteArray()
{
// given
$factory = new AlterationFactory('');
$factory = new AlterationFactory();

// when
$quoteable = $factory->quotable(['first 1%', 'second 2%']);
Expand All @@ -37,41 +37,13 @@ public function shouldQuoteArray()
$this->assertSame('(?:first\ 1\%|second\ 2\%)', $quoteable->quote('%'));
}

/**
* @test
* @dataProvider arrayDuplicatesByFlags
* @param string $flags
* @param string $expected
*/
public function shouldRemoveDuplicates(string $flags, string $expected)
{
// given
$factory = new AlterationFactory($flags);

// when
$quoteable = $factory->quotable(['FOO', 'foo', 'PIęć', 'pięć', 'Żółć', 'ŻÓŁĆ']);

// then
$this->assertSame($expected, $quoteable->quote('%'), "Failed to assert that duplicates were removed with flags '$flags'.");
}

public function arrayDuplicatesByFlags(): array
{
return [
['UI', '(?:FOO|foo|PIęć|pięć|Żółć|ŻÓŁĆ)'],
['mu', '(?:FOO|foo|PIęć|pięć|Żółć|ŻÓŁĆ)'],
['im', '(?:FOO|PIęć|Żółć|ŻÓŁĆ)'],
['uim', '(?:FOO|PIęć|Żółć)'],
];
}

/**
* @test
*/
public function shouldThrowForInvalidType()
{
// given
$factory = new AlterationFactory('');
$factory = new AlterationFactory();

// then
$this->expectException(InvalidArgumentException::class);
Expand Down
4 changes: 1 addition & 3 deletions test/Utils/Impl/MappingAlternation.php
Expand Up @@ -12,13 +12,11 @@ class MappingAlternation extends AlterationFactory

public function __construct(callable $mapper)
{
parent::__construct('');
$this->mapper = $mapper;
}

public function quotable($value): Quotable
{
$value1 = ($this->mapper)($value);
return new RawQuotable($value1);
return new RawQuotable(($this->mapper)($value));
}
}
5 changes: 0 additions & 5 deletions test/Utils/Impl/NoAlternation.php
Expand Up @@ -6,11 +6,6 @@

class NoAlternation extends AlterationFactory
{
public function __construct()
{
parent::__construct('');
}

public function quotable($value): Quotable
{
throw new \AssertionError("Failed to assert that AlternationFactory wasn't used");
Expand Down

0 comments on commit 43aa2ee

Please sign in to comment.