Skip to content

Commit

Permalink
POC
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Jan 9, 2019
1 parent 2fb0119 commit 1cfe4af
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private function fixFunctionCalls(Tokens $tokens, callable $functionFilter, $sta
{
$functionsAnalyzer = new FunctionsAnalyzer();

$insertAtIndexes = [];
$tokensToInsert = [];
for ($index = $start; $index < $end; ++$index) {
if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $index)) {
continue;
Expand All @@ -279,12 +279,10 @@ private function fixFunctionCalls(Tokens $tokens, callable $functionFilter, $sta
continue; // do not bother if previous token is already namespace separator
}

$insertAtIndexes[] = $index;
$tokensToInsert[$index] = new Token([T_NS_SEPARATOR, '\\']);
}

foreach (array_reverse($insertAtIndexes) as $index) {
$tokens->insertAt($index, new Token([T_NS_SEPARATOR, '\\']));
}
$tokens->insertSlices($tokensToInsert);
}

/**
Expand Down
53 changes: 43 additions & 10 deletions src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,27 @@ public function findSequence(array $sequence, $start = 0, $end = null, $caseSens
public function insertAt($index, $items)
{
$items = \is_array($items) || $items instanceof self ? $items : [$items];
$itemsCnt = \count($items);

$this->insertSlices([$index => $items]);
}

/**
* @TODO: docs
*
* @internal
*
* 5 => tokenA
* 7 => [tokenB, tokenC]
*
* @param mixed $slices
*/
public function insertSlices($slices)
{
$itemsCnt = 0;
foreach ($slices as $slice) {
$slice = \is_array($slice) || $slice instanceof self ? $slice : [$slice];
$itemsCnt += \count($slice);
}

if (0 === $itemsCnt) {
return;
Expand All @@ -880,20 +900,33 @@ public function insertAt($index, $items)
$this->blockEndCache = [];
$this->setSize($oldSize + $itemsCnt);

krsort($slices);

$insertBound = $oldSize - 1;

// since we only move already existing items around, we directly call into SplFixedArray::offset* methods.
// that way we get around additional overhead this class adds with overridden offset* methods.
for ($i = $oldSize + $itemsCnt - 1; $i >= $index; --$i) {
$oldItem = parent::offsetExists($i - $itemsCnt) ? parent::offsetGet($i - $itemsCnt) : new Token('');
parent::offsetSet($i, $oldItem);
}
foreach ($slices as $index => $slice) {
$slice = \is_array($slice) || $slice instanceof self ? $slice : [$slice];
$sliceCnt = \count($slice);

for ($i = 0; $i < $itemsCnt; ++$i) {
if ('' === $items[$i]->getContent()) {
throw new \InvalidArgumentException('Must not add empty token to collection.');
for ($i = $insertBound; $i >= $index; --$i) {
$oldItem = parent::offsetExists($i) ? parent::offsetGet($i) : new Token('');
parent::offsetSet($i + $itemsCnt, $oldItem);
}

$this->registerFoundToken($items[$i]);
parent::offsetSet($i + $index, $items[$i]);
$insertBound = $index - $sliceCnt;
$itemsCnt -= $sliceCnt;

foreach ($slice as $indexItem => $item) {
if ('' === $item->getContent()) {
throw new \InvalidArgumentException('Must not add empty token to collection.');
}

$this->registerFoundToken($item);
$newOffset = $index + $itemsCnt + $indexItem;
parent::offsetSet($newOffset, $item);
}
}
}

Expand Down
26 changes: 14 additions & 12 deletions tests/Fixer/FunctionNotation/NativeFunctionInvocationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,6 @@ public function provideFixWithDefaultConfigurationCases()
[
'<?php
\json_encode($foo);
\strlen($foo);
',
'<?php
json_encode($foo);
strlen($foo);
',
],
[
'<?php
class Foo
{
public function bar($foo)
Expand Down Expand Up @@ -269,6 +257,20 @@ public function bar($foo)
#
strlen($a);
echo strlen($a);
',
],
'fix multiple calls in single code' => [
'<?php
\json_encode($foo);
\strlen($foo);
\strlen($foo);
',
'<?php
json_encode($foo);
strlen($foo);
strlen($foo);
',
],
];
Expand Down

0 comments on commit 1cfe4af

Please sign in to comment.