Skip to content

Commit

Permalink
first()/findFirst() don't accept consumer #133
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Jun 15, 2022
1 parent 4249ea6 commit 8bf22cb
Show file tree
Hide file tree
Showing 35 changed files with 510 additions and 801 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Incoming
* Breaking changes
* Removed previously deprecated `Pattern.match().group()`.
* Removed `Pattern.match().asInt()`. Use `stream().asInt()` or `Detail.toInt()`.
* `Pattern.match().first()` no longer accepts `callable` as its argument
* `Pattern.match().findFirst()` no longer accepts `callable` as its argument
* `Stream.first()` no longer accepts `callable` as its argument
* `Stream.findFirst()` no longer accepts `callable` as its argument

Added in 0.32.0
---------------
Expand Down
26 changes: 7 additions & 19 deletions src/CleanRegex/Match/MatchPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,37 +89,25 @@ public function all(): array
return $this->base->matchAll()->getTexts();
}

/**
* @param null|callable $consumer
* @return string|mixed
*/
public function first(callable $consumer = null)
{
if ($consumer === null) {
return $this->matchDetail()->text();
}
return $consumer($this->matchDetail());
}

public function findFirst(callable $consumer): Optional
public function first(): Detail
{
$match = $this->base->matchOffset();
if ($match->matched()) {
return new PresentOptional($consumer($this->findFirstDetail($match)));
return $this->firstDetail($match);
}
return new EmptyOptional();
throw new SubjectNotMatchedException(new FirstMatchMessage(), $this->subject);
}

private function matchDetail(): Detail
public function findFirst(): Optional
{
$match = $this->base->matchOffset();
if ($match->matched()) {
return $this->findFirstDetail($match);
return new PresentOptional($this->firstDetail($match));
}
throw new SubjectNotMatchedException(new FirstMatchMessage(), $this->subject);
return new EmptyOptional();
}

private function findFirstDetail(RawMatchOffset $match): Detail
private function firstDetail(RawMatchOffset $match): Detail
{
$polyfill = new GroupPolyfillDecorator(new FalseNegative($match), $this->allFactory, 0, $this->groupAware);
return DeprecatedMatchDetail::create($this->subject, 0, $polyfill, $this->allFactory, new MatchPrime($match));
Expand Down
22 changes: 4 additions & 18 deletions src/CleanRegex/Match/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,19 @@ public function reduce(callable $reducer, $accumulator)
return $this->terminal->reduce($reducer, $accumulator);
}

public function first(callable $consumer = null)
{
if ($consumer === null) {
return $this->firstValue();
}
return $consumer($this->firstValue());
}

private function firstValue()
public function first()
{
try {
[$key, $value] = $this->upstream->first();
return $value;
} catch (EmptyStreamException $exception) {
$message = new FromFirstStreamMessage();
throw new NoSuchStreamElementException(new FromFirstStreamMessage());
} catch (UnmatchedStreamException $exception) {
$message = new FirstMatchMessage();
throw new NoSuchStreamElementException(new FirstMatchMessage());
}
throw new NoSuchStreamElementException($message);
}

public function findFirst(callable $consumer): Optional
{
return $this->firstOptional()->map($consumer);
}

private function firstOptional(): Optional
public function findFirst(): Optional
{
try {
[$key, $value] = $this->upstream->first();
Expand Down
9 changes: 2 additions & 7 deletions test/Feature/CleanRegex/GroupIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Test\Feature\CleanRegex;

use PHPUnit\Framework\TestCase;
use Test\Utils\DetailFunctions;
use TRegx\CleanRegex\Pattern;

/**
Expand All @@ -18,11 +17,9 @@ class GroupIdentifierTest extends TestCase
public function shouldBeValidGroup(string $pattern, $groupIdentifier)
{
// given
Pattern::of($pattern)->match('Foo')->first(DetailFunctions::out($detail));

$detail = Pattern::of($pattern)->match('Foo')->first();
// when
$identifier = $detail->group($groupIdentifier)->usedIdentifier();

// then
$this->assertSame($identifier, $groupIdentifier);
}
Expand Down Expand Up @@ -51,10 +48,8 @@ public function shouldThrowForOtherTypes($invalidGroupIdentifier, string $messag
// then
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($message);

// given
Pattern::of('Foo')->match('Foo')->first(DetailFunctions::out($detail));

$detail = Pattern::of('Foo')->match('Foo')->first();
// when
$detail->group($invalidGroupIdentifier);
}
Expand Down
68 changes: 27 additions & 41 deletions test/Feature/CleanRegex/Match/Details/J/MatchDetailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use PHPUnit\Framework\TestCase;
use Test\Utils\Assertion\AssertsGroup;
use Test\Utils\DetailFunctions;
use TRegx\CleanRegex\Exception\GroupNotMatchedException;
use TRegx\CleanRegex\Match\Details\Detail;
use TRegx\CleanRegex\Pattern;
Expand All @@ -17,11 +16,9 @@ class MatchDetailTest extends TestCase
*/
public function shouldGroupGetRightValue()
{
// when
pattern('(?<group>Foo)(?<group>Bar)', 'J')
->match('FooBar')
->first(DetailFunctions::out($detail));
// given
$detail = pattern('(?<group>Foo)(?<group>Bar)', 'J')->match('FooBar')->first();
// when
$group = $detail->group('group');
// when, then
$this->assertSame(['group', null], $detail->groupNames());
Expand All @@ -37,9 +34,9 @@ public function shouldGroupGetRightValue()
public function shouldLastGroupNotBeMatched()
{
// given
pattern('(?:(?<group>Foo)|(?<group>Bar)|(?<group>Lorem))', 'J')
->match('Lorem')
->first(DetailFunctions::out($detail));
$pattern = pattern('(?:(?<group>Foo)|(?<group>Bar)|(?<group>Lorem))', 'J');
$detail = $pattern->match('Lorem')->first();
// when
$group = $detail->group('group');
// when, then
$this->assertSame(['group', null, null], $detail->groupNames());
Expand All @@ -53,9 +50,8 @@ public function shouldLastGroupNotBeMatched()
public function shouldGetThrow_forUnmatchedGroup()
{
// given
pattern('(?:(?<group>Foo)|(?<group>Bar)|(?<group>Lorem))', 'J')
->match('Lorem')
->first(DetailFunctions::out($detail));
$pattern = pattern('(?:(?<group>Foo)|(?<group>Bar)|(?<group>Lorem))', 'J');
$detail = $pattern->match('Lorem')->first();
// then
$this->expectException(GroupNotMatchedException::class);
$this->expectExceptionMessage("Expected to get group 'group', but the group was not matched");
Expand All @@ -69,9 +65,8 @@ public function shouldGetThrow_forUnmatchedGroup()
public function shouldGetGroups()
{
// given
pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J')
->match('Lorem')
->first(DetailFunctions::out($detail));
$pattern = pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J');
$detail = $pattern->match('Lorem')->first();
// when
$groups = $detail->groups();
// then
Expand All @@ -84,9 +79,8 @@ public function shouldGetGroups()
public function shouldGetGroupNames()
{
// given
pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J')
->match('Lorem')
->first(DetailFunctions::out($detail));
$pattern = pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J');
$detail = $pattern->match('Lorem')->first();
// when
$groups = $detail->groups();
// then
Expand All @@ -100,9 +94,8 @@ public function shouldGetGroupNames()
public function shouldGetNamedGroups()
{
// given
pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J')
->match('Lorem')
->first(DetailFunctions::out($detail));
$pattern = pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J');
$detail = $pattern->match('Lorem')->first();
// when
$groups = $detail->namedGroups();
// then
Expand All @@ -116,9 +109,8 @@ public function shouldGetNamedGroups()
public function shouldGetNamedGroupNames()
{
// given
pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J')
->match('Lorem')
->first(DetailFunctions::out($detail));
$pattern = pattern('(?<group>Foo)|(?<group>Bar)|(?<group>Lorem)', 'J');
$detail = $pattern->match('Lorem')->first();
// when
$groups = $detail->namedGroups();
// then
Expand Down Expand Up @@ -158,9 +150,8 @@ public function shouldGet_text_or()
public function shouldGetOr_forUnmatchedGroup()
{
// given
Pattern::of('(?<group>Plane)?(?<group>Bird)?Superman', 'J')
->match('Superman')
->first(DetailFunctions::out($detail));
$pattern = Pattern::of('(?<group>Plane)?(?<group>Bird)?Superman', 'J');
$detail = $pattern->match('Superman')->first();
// when
$declared = $detail->group('group');
// then
Expand Down Expand Up @@ -233,9 +224,8 @@ public function detail(): Detail
public function shouldGetAsInt()
{
// given
Pattern::of('(?<group>123),(?<group>456)', 'J')
->match('123,456')
->first(DetailFunctions::out($detail));
$pattern = Pattern::of('(?<group>123),(?<group>456)', 'J');
$detail = $pattern->match('123,456')->first();
// when
$declared = $detail->group('group');
// then
Expand All @@ -248,9 +238,8 @@ public function shouldGetAsInt()
public function shouldGetAsIntBase16()
{
// given
Pattern::of('(?<group>123a),(?<group>456a)', 'J')
->match('123a,456a')
->first(DetailFunctions::out($detail));
$pattern = Pattern::of('(?<group>123a),(?<group>456a)', 'J');
$detail = $pattern->match('123a,456a')->first();
// when
$declared = $detail->group('group');
// then
Expand All @@ -263,9 +252,8 @@ public function shouldGetAsIntBase16()
public function shouldBeInt()
{
// given
Pattern::of('(?<group>___),(?<group>123)', 'J')
->match('___,123')
->first(DetailFunctions::out($detail));
$pattern = Pattern::of('(?<group>___),(?<group>123)', 'J');
$detail = $pattern->match('___,123')->first();
// when
$declared = $detail->group('group');
// then
Expand All @@ -278,9 +266,8 @@ public function shouldBeInt()
public function shouldNotBeInt()
{
// given
Pattern::of('(?<group>123),(?<group>___)', 'J')
->match('123,___')
->first(DetailFunctions::out($detail));
$pattern = Pattern::of('(?<group>123),(?<group>___)', 'J');
$detail = $pattern->match('123,___')->first();
// when
$declared = $detail->group('group');
// then
Expand All @@ -294,9 +281,8 @@ public function shouldNotBeInt()
public function shouldSubstitute()
{
// given
Pattern::of('<(?<group>Old):(?<group>Old)>', 'J')
->match('Subject <Old:Old>.')
->first(DetailFunctions::out($detail));
$pattern = Pattern::of('<(?<group>Old):(?<group>Old)>', 'J');
$detail = $pattern->match('Subject <Old:Old>.')->first();
// when
$declared = $detail->group('group')->substitute('New');
// then
Expand Down
Loading

0 comments on commit 8bf22cb

Please sign in to comment.