Skip to content

Commit

Permalink
Merge pull request #13 from gietos/remove-static-regex
Browse files Browse the repository at this point in the history
Extract static variable $regex into property
  • Loading branch information
alcaeus authored Jul 30, 2019
2 parents 4ec60e1 + 8360b65 commit e17f069
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/Doctrine/Common/Lexer/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ abstract class AbstractLexer
*/
public $token;

/**
* Composed regex for input parsing.
*
* @var string
*/
private $regex;

/**
* Sets the input data to be tokenized.
*
Expand Down Expand Up @@ -235,10 +242,8 @@ public function glimpse()
*/
protected function scan($input)
{
static $regex;

if (! isset($regex)) {
$regex = sprintf(
if (! isset($this->regex)) {
$this->regex = sprintf(
'/(%s)|%s/%s',
implode(')|(', $this->getCatchablePatterns()),
implode('|', $this->getNonCatchablePatterns()),
Expand All @@ -247,7 +252,7 @@ protected function scan($input)
}

$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($regex, $input, -1, $flags);
$matches = preg_split($this->regex, $input, -1, $flags);

if ($matches === false) {
// Work around https://bugs.php.net/78122
Expand Down
17 changes: 17 additions & 0 deletions tests/Doctrine/Common/Lexer/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,21 @@ public function testIsA()
$this->assertTrue($this->concreteLexer->isA('<', 'operator'));
$this->assertTrue($this->concreteLexer->isA('fake_text', 'string'));
}

public function testAddCatchablePatternsToMutableLexer()
{
$mutableLexer = new MutableLexer();
$mutableLexer->addCatchablePattern('[a-z]');
$mutableLexer->setInput('one');
$token = $mutableLexer->glimpse();

$this->assertEquals('o', $token['value']);

$mutableLexer = new MutableLexer();
$mutableLexer->addCatchablePattern('[a-z]+');
$mutableLexer->setInput('one');
$token = $mutableLexer->glimpse();

$this->assertEquals('one', $token['value']);
}
}
33 changes: 33 additions & 0 deletions tests/Doctrine/Common/Lexer/MutableLexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Lexer;

use Doctrine\Common\Lexer\AbstractLexer;

class MutableLexer extends AbstractLexer
{
/** @var string[] */
private $catchablePatterns = [];

public function addCatchablePattern($pattern)
{
$this->catchablePatterns[] = $pattern;
}

protected function getCatchablePatterns()
{
return $this->catchablePatterns;
}

protected function getNonCatchablePatterns()
{
return ['[\s,]+'];
}

protected function getType(&$value)
{
return 1;
}
}

0 comments on commit e17f069

Please sign in to comment.