Skip to content

Commit

Permalink
bug #5688 ArrayIndentationFixer - fix for really long arrays (kubawer…
Browse files Browse the repository at this point in the history
…los)

This PR was merged into the 2.19 branch.

Discussion
----------

ArrayIndentationFixer - fix for really long arrays

Reference: https://github.com/aws/aws-sdk-php/blob/3.180.2/src/data/ec2/2016-11-15/api-2.json.php

Code in test case has 100k tokens, code from reference file has 174k tokens.

Without the fix the newly added tests was taking around 1700 seconds (for each `[` it was searching for new line before it - token by token).

Commits
-------

e17cdb2 ArrayIndentationFixer - fix for really long arrays
  • Loading branch information
keradus committed Jun 4, 2021
2 parents 98a5861 + e17cdb2 commit 001d134
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/Fixer/Whitespace/ArrayIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

final class ArrayIndentationFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
{
/** @var int */
private $newlineTokenIndexCache;

/** @var int */
private $newlineTokenPositionCache;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -57,6 +63,8 @@ public function getPriority()

protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$this->returnWithUpdateCache(0, null);

$scopes = [];
$previousLineInitialIndent = '';
$previousLineNewIndent = '';
Expand Down Expand Up @@ -213,21 +221,30 @@ private function extractIndent($content)
return '';
}

private function getPreviousNewlineTokenIndex(Tokens $tokens, $index)
private function getPreviousNewlineTokenIndex(Tokens $tokens, $startIndex)
{
$index = $startIndex;
while ($index > 0) {
$index = $tokens->getPrevTokenOfKind($index, [[T_WHITESPACE], [T_INLINE_HTML]]);

if ($this->newlineTokenIndexCache > $index) {
return $this->returnWithUpdateCache($startIndex, $this->newlineTokenPositionCache);
}

if (null === $index) {
break;
}

if ($this->isNewLineToken($tokens, $index)) {
return $index;
return $this->returnWithUpdateCache($startIndex, $index);
}

if ($this->isNewLineToken($tokens, $index)) {
return $this->returnWithUpdateCache($startIndex, $index);
}
}

return null;
return $this->returnWithUpdateCache($startIndex, null);
}

private function isNewLineToken(Tokens $tokens, $index)
Expand All @@ -249,4 +266,16 @@ private function computeNewLineContent(Tokens $tokens, $index)

return $content;
}

/**
* @param int $index
* @param null|int $position
*/
private function returnWithUpdateCache($index, $position)
{
$this->newlineTokenIndexCache = $index;
$this->newlineTokenPositionCache = $position;

return $position;
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Integration/misc/huge_array.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Test if array_indentation can handle huge arrays.
--RULESET--
{
"array_indentation": true
}
--REQUIREMENTS--
{"php": 70000}
3 changes: 3 additions & 0 deletions tests/Fixtures/Integration/misc/huge_array.test-out.php

Large diffs are not rendered by default.

0 comments on commit 001d134

Please sign in to comment.