Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Apr 13, 2021
2 parents 81a5c17 + e1a2604 commit 3dff24b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/Tokenizer/Tokens.php
Expand Up @@ -48,7 +48,14 @@ class Tokens extends \SplFixedArray
private static $cache = [];

/**
* Cache of block edges. Any change in collection will invalidate it.
* Cache of block starts. Any change in collection will invalidate it.
*
* @var array<int, int>
*/
private $blockStartCache = [];

/**
* Cache of block ends. Any change in collection will invalidate it.
*
* @var array<int, int>
*/
Expand Down Expand Up @@ -327,6 +334,7 @@ public function offsetUnset($index)
*/
public function offsetSet($index, $newval)
{
$this->blockStartCache = [];
$this->blockEndCache = [];

if (!isset($this[$index]) || !$this[$index]->equals($newval)) {
Expand Down Expand Up @@ -938,6 +946,7 @@ public function insertSlices(array $slices)

$oldSize = \count($this);
$this->changed = true;
$this->blockStartCache = [];
$this->blockEndCache = [];
$this->setSize($oldSize + $itemsCount);

Expand Down Expand Up @@ -1436,8 +1445,13 @@ private function findOppositeBlockEdge($type, $searchIndex, $findEnd)
throw new \InvalidArgumentException(sprintf('Invalid param type: "%s".', $type));
}

if (!self::isLegacyMode() && isset($this->blockEndCache[$searchIndex])) {
return $this->blockEndCache[$searchIndex];
if (!self::isLegacyMode()) {
if ($findEnd && isset($this->blockStartCache[$searchIndex])) {
return $this->blockStartCache[$searchIndex];
}
if (!$findEnd && isset($this->blockEndCache[$searchIndex])) {
return $this->blockEndCache[$searchIndex];
}
}

$startEdge = $blockEdgeDefinitions[$type]['start'];
Expand Down Expand Up @@ -1482,8 +1496,13 @@ private function findOppositeBlockEdge($type, $searchIndex, $findEnd)
throw new \UnexpectedValueException(sprintf('Missing block "%s".', $findEnd ? 'end' : 'start'));
}

$this->blockEndCache[$startIndex] = $index;
$this->blockEndCache[$index] = $startIndex;
if ($startIndex < $index) {
$this->blockStartCache[$startIndex] = $index;
$this->blockEndCache[$index] = $startIndex;
} else {
$this->blockStartCache[$index] = $startIndex;
$this->blockEndCache[$startIndex] = $index;
}

return $index;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Tokenizer/TokensTest.php
Expand Up @@ -827,6 +827,32 @@ public function testFindBlockEndLastParameterTrueDeprecated()
static::assertSame(4, $tokens->findBlockEnd(Tokens::BLOCK_TYPE_DYNAMIC_VAR_BRACE, 2, true));
}

public function testFindBlockEndCalledMultipleTimes()
{
Tokens::clearCache();
$tokens = Tokens::fromCode('<?php foo(1, 2);');

static::assertSame(7, $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, 2));

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/^Invalid param \$startIndex - not a proper block "start"\.$/');

$tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, 7);
}

public function testFindBlockStartEdgeCalledMultipleTimes()
{
Tokens::clearCache();
$tokens = Tokens::fromCode('<?php foo(1, 2);');

static::assertSame(2, $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, 7));

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/^Invalid param \$startIndex - not a proper block "end"\.$/');

$tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, 2);
}

public function testEmptyTokens()
{
$code = '';
Expand Down

0 comments on commit 3dff24b

Please sign in to comment.