Skip to content

Commit e464df8

Browse files
committed
add nested_attributes property on tokens inside an attribute
1 parent 1b065bb commit e464df8

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/Tokenizers/PHP.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,8 @@ protected function processAdditional()
20022002
echo "\t*** START ADDITIONAL PHP PROCESSING ***".PHP_EOL;
20032003
}
20042004

2005+
$this->createAttributesNestingMap();
2006+
20052007
$numTokens = count($this->tokens);
20062008
for ($i = ($numTokens - 1); $i >= 0; $i--) {
20072009
// Check for any unset scope conditions due to alternate IF/ENDIF syntax.
@@ -2969,4 +2971,41 @@ private function parsePhpAttribute(array &$tokens, $stackPtr)
29692971
}//end parsePhpAttribute()
29702972

29712973

2974+
/**
2975+
* Creates a map for the attributes tokens that surround other tokens.
2976+
*
2977+
* @return void
2978+
*/
2979+
private function createAttributesNestingMap()
2980+
{
2981+
$map = [];
2982+
for ($i = 0; $i < $this->numTokens; $i++) {
2983+
if (isset($this->tokens[$i]['attribute_opener']) === true
2984+
&& $i === $this->tokens[$i]['attribute_opener']
2985+
) {
2986+
if (empty($map) === false) {
2987+
$this->tokens[$i]['nested_attributes'] = $map;
2988+
}
2989+
2990+
if (isset($this->tokens[$i]['attribute_closer']) === true) {
2991+
$map[$this->tokens[$i]['attribute_opener']]
2992+
= $this->tokens[$i]['attribute_closer'];
2993+
}
2994+
} else if (isset($this->tokens[$i]['attribute_closer']) === true
2995+
&& $i === $this->tokens[$i]['attribute_closer']
2996+
) {
2997+
array_pop($map);
2998+
if (empty($map) === false) {
2999+
$this->tokens[$i]['nested_attributes'] = $map;
3000+
}
3001+
} else {
3002+
if (empty($map) === false) {
3003+
$this->tokens[$i]['nested_attributes'] = $map;
3004+
}
3005+
}//end if
3006+
}//end for
3007+
3008+
}//end createAttributesNestingMap()
3009+
3010+
29723011
}//end class

tests/Core/Tokenizer/AttributesTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,19 +465,33 @@ public function testNestedAttributes()
465465
$this->assertSame($tokens[$attribute]['attribute_opener'], $tokens[$closer]['attribute_opener']);
466466
$this->assertSame($tokens[$attribute]['attribute_closer'], $tokens[$closer]['attribute_closer']);
467467

468-
$test = function (array $tokens, $length) use ($attribute) {
468+
$this->assertArrayNotHasKey('nested_attributes', $tokens[$attribute]);
469+
$this->assertArrayHasKey('nested_attributes', $tokens[($attribute + 8)]);
470+
$this->assertSame([$attribute => ($attribute + 24)], $tokens[($attribute + 8)]['nested_attributes']);
471+
472+
$test = function (array $tokens, $length, $nestedMap) use ($attribute) {
469473
foreach ($tokens as $token) {
470474
$this->assertArrayHasKey('attribute_closer', $token);
471475
$this->assertSame(($attribute + $length), $token['attribute_closer']);
476+
$this->assertSame($nestedMap, $token['nested_attributes']);
472477
}
473478
};
474479

475-
$test(array_slice($tokens, ($attribute + 1), 7), 24);
480+
$test(array_slice($tokens, ($attribute + 1), 7), 24, [$attribute => $attribute + 24]);
481+
$test(array_slice($tokens, ($attribute + 8), 1), 8 + 5, [$attribute => $attribute + 24]);
476482

477483
// Length here is 8 (nested attribute offset) + 5 (real length).
478-
$test(array_slice($tokens, ($attribute + 8), 6), 8 + 5);
484+
$test(
485+
array_slice($tokens, ($attribute + 9), 4),
486+
8 + 5,
487+
[
488+
$attribute => $attribute + 24,
489+
$attribute + 8 => $attribute + 13,
490+
]
491+
);
479492

480-
$test(array_slice($tokens, ($attribute + 14), 11), 24);
493+
$test(array_slice($tokens, ($attribute + 13), 1), 8 + 5, [$attribute => $attribute + 24]);
494+
$test(array_slice($tokens, ($attribute + 14), 10), 24, [$attribute => $attribute + 24]);
481495

482496
$map = array_map(
483497
static function ($token) {

0 commit comments

Comments
 (0)