Skip to content

Commit e17f069

Browse files
authored
Merge pull request #13 from gietos/remove-static-regex
Extract static variable $regex into property
2 parents 4ec60e1 + 8360b65 commit e17f069

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

lib/Doctrine/Common/Lexer/AbstractLexer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ abstract class AbstractLexer
6767
*/
6868
public $token;
6969

70+
/**
71+
* Composed regex for input parsing.
72+
*
73+
* @var string
74+
*/
75+
private $regex;
76+
7077
/**
7178
* Sets the input data to be tokenized.
7279
*
@@ -235,10 +242,8 @@ public function glimpse()
235242
*/
236243
protected function scan($input)
237244
{
238-
static $regex;
239-
240-
if (! isset($regex)) {
241-
$regex = sprintf(
245+
if (! isset($this->regex)) {
246+
$this->regex = sprintf(
242247
'/(%s)|%s/%s',
243248
implode(')|(', $this->getCatchablePatterns()),
244249
implode('|', $this->getNonCatchablePatterns()),
@@ -247,7 +252,7 @@ protected function scan($input)
247252
}
248253

249254
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
250-
$matches = preg_split($regex, $input, -1, $flags);
255+
$matches = preg_split($this->regex, $input, -1, $flags);
251256

252257
if ($matches === false) {
253258
// Work around https://bugs.php.net/78122

tests/Doctrine/Common/Lexer/AbstractLexerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,21 @@ public function testIsA()
269269
$this->assertTrue($this->concreteLexer->isA('<', 'operator'));
270270
$this->assertTrue($this->concreteLexer->isA('fake_text', 'string'));
271271
}
272+
273+
public function testAddCatchablePatternsToMutableLexer()
274+
{
275+
$mutableLexer = new MutableLexer();
276+
$mutableLexer->addCatchablePattern('[a-z]');
277+
$mutableLexer->setInput('one');
278+
$token = $mutableLexer->glimpse();
279+
280+
$this->assertEquals('o', $token['value']);
281+
282+
$mutableLexer = new MutableLexer();
283+
$mutableLexer->addCatchablePattern('[a-z]+');
284+
$mutableLexer->setInput('one');
285+
$token = $mutableLexer->glimpse();
286+
287+
$this->assertEquals('one', $token['value']);
288+
}
272289
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Common\Lexer;
6+
7+
use Doctrine\Common\Lexer\AbstractLexer;
8+
9+
class MutableLexer extends AbstractLexer
10+
{
11+
/** @var string[] */
12+
private $catchablePatterns = [];
13+
14+
public function addCatchablePattern($pattern)
15+
{
16+
$this->catchablePatterns[] = $pattern;
17+
}
18+
19+
protected function getCatchablePatterns()
20+
{
21+
return $this->catchablePatterns;
22+
}
23+
24+
protected function getNonCatchablePatterns()
25+
{
26+
return ['[\s,]+'];
27+
}
28+
29+
protected function getType(&$value)
30+
{
31+
return 1;
32+
}
33+
}

0 commit comments

Comments
 (0)