Skip to content

Commit 6367e71

Browse files
committed
add nested_attributes property on tokens inside an attribute
1 parent 868be7e commit 6367e71

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
@@ -2147,6 +2147,8 @@ protected function processAdditional()
21472147
echo "\t*** START ADDITIONAL PHP PROCESSING ***".PHP_EOL;
21482148
}
21492149

2150+
$this->createAttributesNestingMap();
2151+
21502152
$numTokens = count($this->tokens);
21512153
for ($i = ($numTokens - 1); $i >= 0; $i--) {
21522154
// Check for any unset scope conditions due to alternate IF/ENDIF syntax.
@@ -3209,4 +3211,41 @@ private function parsePhpAttribute(array &$tokens, $stackPtr)
32093211
}//end parsePhpAttribute()
32103212

32113213

3214+
/**
3215+
* Creates a map for the attributes tokens that surround other tokens.
3216+
*
3217+
* @return void
3218+
*/
3219+
private function createAttributesNestingMap()
3220+
{
3221+
$map = [];
3222+
for ($i = 0; $i < $this->numTokens; $i++) {
3223+
if (isset($this->tokens[$i]['attribute_opener']) === true
3224+
&& $i === $this->tokens[$i]['attribute_opener']
3225+
) {
3226+
if (empty($map) === false) {
3227+
$this->tokens[$i]['nested_attributes'] = $map;
3228+
}
3229+
3230+
if (isset($this->tokens[$i]['attribute_closer']) === true) {
3231+
$map[$this->tokens[$i]['attribute_opener']]
3232+
= $this->tokens[$i]['attribute_closer'];
3233+
}
3234+
} else if (isset($this->tokens[$i]['attribute_closer']) === true
3235+
&& $i === $this->tokens[$i]['attribute_closer']
3236+
) {
3237+
array_pop($map);
3238+
if (empty($map) === false) {
3239+
$this->tokens[$i]['nested_attributes'] = $map;
3240+
}
3241+
} else {
3242+
if (empty($map) === false) {
3243+
$this->tokens[$i]['nested_attributes'] = $map;
3244+
}
3245+
}//end if
3246+
}//end for
3247+
3248+
}//end createAttributesNestingMap()
3249+
3250+
32123251
}//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)