Skip to content

Commit

Permalink
Merge branch 'feature/tokenizer-php-bug-fix-short-list-in-non-braced-…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Sep 15, 2022
2 parents 70aee78 + 382ad27 commit 5a85210
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
9 changes: 7 additions & 2 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2688,13 +2688,18 @@ protected function processAdditional()
}

if (isset(Util\Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) {
if (isset($allowed[$this->tokens[$x]['code']]) === false) {
// Allow for control structures without braces.
if (($this->tokens[$x]['code'] === T_CLOSE_PARENTHESIS
&& isset($this->tokens[$x]['parenthesis_owner']) === true
&& isset(Util\Tokens::$scopeOpeners[$this->tokens[$this->tokens[$x]['parenthesis_owner']]['code']]) === true)
|| isset($allowed[$this->tokens[$x]['code']]) === false
) {
$isShortArray = true;
}

break;
}
}
}//end for

if ($isShortArray === true) {
$this->tokens[$i]['code'] = T_OPEN_SHORT_ARRAY;
Expand Down
14 changes: 14 additions & 0 deletions tests/Core/Tokenizer/ShortArrayTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ echo [1, 2, 3][0];
/* testArrayWithinFunctionCall */
$var = functionCall([$x, $y]);

if ( true ) {
/* testShortListDeclarationAfterBracedControlStructure */
[ $a ] = [ 'hi' ];
}

if ( true )
/* testShortListDeclarationAfterNonBracedControlStructure */
[ $a ] = [ 'hi' ];

if ( true ) :
/* testShortListDeclarationAfterAlternativeControlStructure */
[ $a ] = [ 'hi' ];
endif;

/* testLiveCoding */
// Intentional parse error. This has to be the last test in the file.
$array = [
63 changes: 33 additions & 30 deletions tests/Core/Tokenizer/ShortArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,29 @@ public function testSquareBrackets($testMarker)
public function dataSquareBrackets()
{
return [
['/* testArrayAccess1 */'],
['/* testArrayAccess2 */'],
['/* testArrayAssignment */'],
['/* testFunctionCallDereferencing */'],
['/* testMethodCallDereferencing */'],
['/* testStaticMethodCallDereferencing */'],
['/* testPropertyDereferencing */'],
['/* testPropertyDereferencingWithInaccessibleName */'],
['/* testStaticPropertyDereferencing */'],
['/* testStringDereferencing */'],
['/* testStringDereferencingDoubleQuoted */'],
['/* testConstantDereferencing */'],
['/* testClassConstantDereferencing */'],
['/* testMagicConstantDereferencing */'],
['/* testArrayAccessCurlyBraces */'],
['/* testArrayLiteralDereferencing */'],
['/* testShortArrayLiteralDereferencing */'],
['/* testClassMemberDereferencingOnInstantiation1 */'],
['/* testClassMemberDereferencingOnInstantiation2 */'],
['/* testClassMemberDereferencingOnClone */'],
['/* testNullsafeMethodCallDereferencing */'],
['/* testInterpolatedStringDereferencing */'],
['/* testLiveCoding */'],
'array access 1' => ['/* testArrayAccess1 */'],
'array access 2' => ['/* testArrayAccess2 */'],
'array assignment' => ['/* testArrayAssignment */'],
'function call dereferencing' => ['/* testFunctionCallDereferencing */'],
'method call dereferencing' => ['/* testMethodCallDereferencing */'],
'static method call dereferencing' => ['/* testStaticMethodCallDereferencing */'],
'property dereferencing' => ['/* testPropertyDereferencing */'],
'property dereferencing with inaccessable name' => ['/* testPropertyDereferencingWithInaccessibleName */'],
'static property dereferencing' => ['/* testStaticPropertyDereferencing */'],
'string dereferencing single quotes' => ['/* testStringDereferencing */'],
'string dereferencing double quotes' => ['/* testStringDereferencingDoubleQuoted */'],
'global constant dereferencing' => ['/* testConstantDereferencing */'],
'class constant dereferencing' => ['/* testClassConstantDereferencing */'],
'magic constant dereferencing' => ['/* testMagicConstantDereferencing */'],
'array access with curly braces' => ['/* testArrayAccessCurlyBraces */'],
'array literal dereferencing' => ['/* testArrayLiteralDereferencing */'],
'short array literal dereferencing' => ['/* testShortArrayLiteralDereferencing */'],
'class member dereferencing on instantiation 1' => ['/* testClassMemberDereferencingOnInstantiation1 */'],
'class member dereferencing on instantiation 2' => ['/* testClassMemberDereferencingOnInstantiation2 */'],
'class member dereferencing on clone' => ['/* testClassMemberDereferencingOnClone */'],
'nullsafe method call dereferencing' => ['/* testNullsafeMethodCallDereferencing */'],
'interpolated string dereferencing' => ['/* testInterpolatedStringDereferencing */'],
'live coding' => ['/* testLiveCoding */'],
];

}//end dataSquareBrackets()
Expand Down Expand Up @@ -117,13 +117,16 @@ public function testShortArrays($testMarker)
public function dataShortArrays()
{
return [
['/* testShortArrayDeclarationEmpty */'],
['/* testShortArrayDeclarationWithOneValue */'],
['/* testShortArrayDeclarationWithMultipleValues */'],
['/* testShortArrayDeclarationWithDereferencing */'],
['/* testShortListDeclaration */'],
['/* testNestedListDeclaration */'],
['/* testArrayWithinFunctionCall */'],
'short array empty' => ['/* testShortArrayDeclarationEmpty */'],
'short array with value' => ['/* testShortArrayDeclarationWithOneValue */'],
'short array with values' => ['/* testShortArrayDeclarationWithMultipleValues */'],
'short array with dereferencing' => ['/* testShortArrayDeclarationWithDereferencing */'],
'short list' => ['/* testShortListDeclaration */'],
'short list nested' => ['/* testNestedListDeclaration */'],
'short array within function call' => ['/* testArrayWithinFunctionCall */'],
'short list after braced control structure' => ['/* testShortListDeclarationAfterBracedControlStructure */'],
'short list after non-braced control structure' => ['/* testShortListDeclarationAfterNonBracedControlStructure */'],
'short list after alternative control structure' => ['/* testShortListDeclarationAfterAlternativeControlStructure */'],
];

}//end dataShortArrays()
Expand Down

0 comments on commit 5a85210

Please sign in to comment.