Skip to content

Commit

Permalink
Merge 8b08c4e into 5fdb06f
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Apr 18, 2023
2 parents 5fdb06f + 8b08c4e commit 7d00cad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
17 changes: 11 additions & 6 deletions PHPCompatibility/Sniffs/Lists/NewListReferenceAssignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPCompatibility\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\Lists;

/**
Expand Down Expand Up @@ -40,11 +41,7 @@ class NewListReferenceAssignmentSniff extends Sniff
*/
public function register()
{
return [
\T_LIST => \T_LIST,
\T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY,
\T_OPEN_SQUARE_BRACKET => \T_OPEN_SQUARE_BRACKET,
];
return Collections::listOpenTokensBC();
}

/**
Expand All @@ -64,10 +61,18 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$tokens = $phpcsFile->getTokens();
if (isset(Collections::shortArrayListOpenTokensBC()[$tokens[$stackPtr]['code']]) === true
&& Lists::isShortList($phpcsFile, $stackPtr) === false
) {
// Real square brackets or short array, not short list.
return;
}

try {
$assignments = Lists::getAssignments($phpcsFile, $stackPtr);
} catch (RuntimeException $e) {
// Parse error, live coding or short array, not short list.
// Parse error/live coding.
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ foreach ($array as [&$a, $b]) {}
// Test handling of tokenizer issue in older PHPCS versions.
if (true) {}
[$id1, &$name1] = $data;

// Safeguard that the sniff doesn't trigger on short arrays.
$a = [&$x1, $y1];

// Safeguard handling of PHP 7.1+ keyed lists.
list("id" => &$id1, "name" => $name1) = $data[0];
["id" => $id1, "name" => & /*comment*/ $name1] = $data[0];

// Don't trigger on unfinished code during live code review.
// This has to be the last test in the file!
list(
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function dataNewListReferenceAssignment()
[36], // x2.
[37],
[41],
[47],
[48],
];
}

Expand Down Expand Up @@ -100,6 +102,8 @@ public function dataNoFalsePositives()
[29],
[31],
[32],
[44],
[52],
];
}

Expand Down

0 comments on commit 7d00cad

Please sign in to comment.