Skip to content

Commit 7a21f7a

Browse files
committed
Set Lexer as a dependency of Parser and Parser as a dependency of StringMatcher
1 parent 6bf8a60 commit 7a21f7a

File tree

9 files changed

+99
-70
lines changed

9 files changed

+99
-70
lines changed

src/Coduo/PHPMatcher/Factory/SimpleFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Coduo\PHPMatcher\Factory;
44

55
use Coduo\PHPMatcher\Factory;
6+
use Coduo\PHPMatcher\Lexer;
67
use Coduo\PHPMatcher\Matcher;
8+
use Coduo\PHPMatcher\Parser;
79

810
class SimpleFactory implements Factory
911
{
@@ -35,11 +37,13 @@ protected function buildMatchers()
3537
*/
3638
protected function buildScalarMatchers()
3739
{
40+
$parser = new Parser(new Lexer());
41+
3842
return new Matcher\ChainMatcher(array(
3943
new Matcher\CallbackMatcher(),
4044
new Matcher\ExpressionMatcher(),
4145
new Matcher\NullMatcher(),
42-
new Matcher\StringMatcher(),
46+
new Matcher\StringMatcher($parser),
4347
new Matcher\IntegerMatcher(),
4448
new Matcher\BooleanMatcher(),
4549
new Matcher\DoubleMatcher(),

src/Coduo/PHPMatcher/Matcher/Pattern/Expander/StartsWith.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class StartsWith implements PatternExpander
1616
* @var null|string
1717
*/
1818
private $error;
19+
1920
/**
2021
* @var bool
2122
*/

src/Coduo/PHPMatcher/Matcher/StringMatcher.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88
class StringMatcher extends Matcher
99
{
10+
/**
11+
* @var Parser
12+
*/
13+
private $parser;
14+
15+
/**
16+
* @param Parser $parser
17+
*/
18+
public function __construct(Parser $parser)
19+
{
20+
$this->parser = $parser;
21+
}
22+
1023
/**
1124
* {@inheritDoc}
1225
*/
@@ -17,8 +30,7 @@ public function match($value, $pattern)
1730
return false;
1831
}
1932

20-
$parser = new Parser($pattern);
21-
$typePattern = $parser->parse();
33+
$typePattern = $this->parser->parse($pattern);
2234
if (!$typePattern->matchExpanders($value)) {
2335
$this->error = $typePattern->getError();
2436
return false;
@@ -36,8 +48,6 @@ public function canMatch($pattern)
3648
return false;
3749
}
3850

39-
$parser = new Parser($pattern);
40-
41-
return $parser->patternSyntaxIsValid() && $parser->parse()->is('string');
51+
return $this->parser->hasValidSyntax($pattern) && $this->parser->parse($pattern)->is('string');
4252
}
4353
}

src/Coduo/PHPMatcher/Parser.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ class Parser
1818
*/
1919
private $lexer;
2020

21-
/**
22-
* @var AST\Pattern|null
23-
*/
24-
private $AST;
25-
2621
/**
2722
* @var array
2823
*/
@@ -31,34 +26,35 @@ class Parser
3126
);
3227

3328
/**
34-
* @param string $pattern
29+
* @param Lexer $lexer
3530
*/
36-
public function __construct($pattern)
31+
public function __construct(Lexer $lexer)
3732
{
38-
$this->lexer = new Lexer();
39-
$this->lexer->setInput($pattern);
33+
$this->lexer = $lexer;
4034
}
4135

4236
/**
37+
* @param string $pattern
4338
* @return bool
4439
*/
45-
public function patternSyntaxIsValid()
40+
public function hasValidSyntax($pattern)
4641
{
4742
try {
48-
$this->getAST();
43+
$this->getAST($pattern);
4944
return true;
5045
} catch (Exception $e) {
5146
return false;
5247
}
5348
}
5449

5550
/**
51+
* @param string $pattern
5652
* @throws UnknownExpanderException
5753
* @return Pattern\TypePattern
5854
*/
59-
public function parse()
55+
public function parse($pattern)
6056
{
61-
$AST = $this->getAST();
57+
$AST = $this->getAST($pattern);
6258
$pattern = new Pattern\TypePattern((string) $AST->getType());
6359
foreach ($AST->getExpanders() as $expander) {
6460
if (!array_key_exists($expander->getName(), $this->expanderDefinitions)) {
@@ -72,15 +68,13 @@ public function parse()
7268
}
7369

7470
/**
71+
* @param $pattern
7572
* @return AST\Pattern
7673
*/
77-
public function getAST()
74+
public function getAST($pattern)
7875
{
79-
if (!isset($this->AST)) {
80-
$this->AST = $this->getPattern();
81-
}
82-
83-
return $this->AST;
76+
$this->lexer->setInput($pattern);
77+
return $this->getPattern();
8478
}
8579

8680
/**

tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Coduo\PHPMatcher\Tests\Matcher;
44

5+
use Coduo\PHPMatcher\Lexer;
56
use Coduo\PHPMatcher\Matcher;
7+
use Coduo\PHPMatcher\Parser;
68

79
class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
810
{
@@ -13,12 +15,13 @@ class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
1315

1416
public function setUp()
1517
{
18+
$parser = new Parser(new Lexer());
1619
$this->matcher = new Matcher\ArrayMatcher(
1720
new Matcher\ChainMatcher(array(
1821
new Matcher\CallbackMatcher(),
1922
new Matcher\ExpressionMatcher(),
2023
new Matcher\NullMatcher(),
21-
new Matcher\StringMatcher(),
24+
new Matcher\StringMatcher($parser),
2225
new Matcher\IntegerMatcher(),
2326
new Matcher\BooleanMatcher(),
2427
new Matcher\DoubleMatcher(),

tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
2+
23
namespace Coduo\PHPMatcher\Tests\Matcher;
34

5+
use Coduo\PHPMatcher\Lexer;
46
use Coduo\PHPMatcher\Matcher;
7+
use Coduo\PHPMatcher\Parser;
58

69
class JsonMatcherTest extends \PHPUnit_Framework_TestCase
710
{
@@ -12,11 +15,12 @@ class JsonMatcherTest extends \PHPUnit_Framework_TestCase
1215

1316
public function setUp()
1417
{
18+
$parser = new Parser(new Lexer());
1519
$scalarMatchers = new Matcher\ChainMatcher(array(
1620
new Matcher\CallbackMatcher(),
1721
new Matcher\ExpressionMatcher(),
1822
new Matcher\NullMatcher(),
19-
new Matcher\StringMatcher(),
23+
new Matcher\StringMatcher($parser),
2024
new Matcher\IntegerMatcher(),
2125
new Matcher\BooleanMatcher(),
2226
new Matcher\DoubleMatcher(),

tests/Coduo/PHPMatcher/Matcher/StringMatcherTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
11
<?php
2+
23
namespace Coduo\PHPMatcher\Tests\Matcher;
34

5+
use Coduo\PHPMatcher\Lexer;
46
use Coduo\PHPMatcher\Matcher\StringMatcher;
7+
use Coduo\PHPMatcher\Parser;
58

69
class StringMatcherTest extends \PHPUnit_Framework_TestCase
710
{
11+
/**
12+
* @var StringMatcher
13+
*/
14+
private $matcher;
15+
16+
public function setUp()
17+
{
18+
$this->matcher = new StringMatcher(new Parser(new Lexer()));
19+
}
820
/**
921
* @dataProvider positiveCanMatchData
1022
*/
1123
public function test_positive_can_matches($pattern)
1224
{
13-
$matcher = new StringMatcher();
14-
$this->assertTrue($matcher->canMatch($pattern));
25+
$this->assertTrue($this->matcher->canMatch($pattern));
1526
}
1627

1728
/**
1829
* @dataProvider negativeCanMatchData
1930
*/
2031
public function test_negative_can_matches($pattern)
2132
{
22-
$matcher = new StringMatcher();
23-
$this->assertFalse($matcher->canMatch($pattern));
33+
$this->assertFalse($this->matcher->canMatch($pattern));
2434
}
2535

2636
/**
2737
* @dataProvider positiveMatchData
2838
*/
2939
public function test_positive_match($value, $pattern)
3040
{
31-
$matcher = new StringMatcher();
32-
$this->assertTrue($matcher->match($value, $pattern));
41+
$this->assertTrue($this->matcher->match($value, $pattern));
3342
}
3443

3544
/**
3645
* @dataProvider negativeMatchData
3746
*/
3847
public function test_negative_match($value, $pattern)
3948
{
40-
$matcher = new StringMatcher();
41-
$this->assertFalse($matcher->match($value, $pattern));
49+
$this->assertFalse($this->matcher->match($value, $pattern));
4250
}
4351

4452
/**
4553
* @dataProvider negativeMatchDescription
4654
*/
4755
public function test_negative_match_description($value, $pattern, $error)
4856
{
49-
$matcher = new StringMatcher();
50-
$matcher->match($value, $pattern);
51-
$this->assertEquals($error, $matcher->getError());
57+
$this->matcher->match($value, $pattern);
58+
$this->assertEquals($error, $this->matcher->getError());
5259
}
5360

5461
public static function positiveCanMatchData()

tests/Coduo/PHPMatcher/MatcherTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Coduo\PHPMatcher\Tests;
33

4+
use Coduo\PHPMatcher\Lexer;
45
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
56
use Coduo\PHPMatcher\Matcher\CaptureMatcher;
67
use Coduo\PHPMatcher\Matcher\CallbackMatcher;
@@ -11,6 +12,7 @@
1112
use Coduo\PHPMatcher\Matcher\TypeMatcher;
1213
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
1314
use Coduo\PHPMatcher\Matcher;
15+
use Coduo\PHPMatcher\Parser;
1416

1517
class MatcherTest extends \PHPUnit_Framework_TestCase
1618
{
@@ -26,13 +28,13 @@ class MatcherTest extends \PHPUnit_Framework_TestCase
2628
public function setUp()
2729
{
2830
$this->captureMatcher = new CaptureMatcher();
29-
31+
$parser = new Parser(new Lexer());
3032
$scalarMatchers = new ChainMatcher(array(
3133
$this->captureMatcher,
3234
new Matcher\CallbackMatcher(),
3335
new Matcher\ExpressionMatcher(),
3436
new Matcher\NullMatcher(),
35-
new Matcher\StringMatcher(),
37+
new Matcher\StringMatcher($parser),
3638
new Matcher\IntegerMatcher(),
3739
new Matcher\BooleanMatcher(),
3840
new Matcher\DoubleMatcher(),

0 commit comments

Comments
 (0)