Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
PHPCompatibility/Sniffs/Lists/NewListReferenceAssignmentSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* \PHPCompatibility\Sniffs\Lists\NewListReferenceAssignmentSniff. | ||
* | ||
* PHP version 7.3 | ||
* | ||
* @category PHP | ||
* @package PHPCompatibility | ||
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl> | ||
*/ | ||
|
||
namespace PHPCompatibility\Sniffs\Lists; | ||
|
||
use PHPCompatibility\Sniffs\Lists\NewKeyedListSniff; | ||
|
||
/** | ||
* Detect reference assignments in array destructuring using (short) list. | ||
* | ||
* PHP version 7.3 | ||
* | ||
* @category PHP | ||
* @package PHPCompatibility | ||
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl> | ||
*/ | ||
class NewListReferenceAssignmentSniff extends NewKeyedListSniff | ||
{ | ||
/** | ||
* The token(s) within the list construct which is being targeted. | ||
* | ||
* @var array | ||
*/ | ||
protected $targetsInList = array( | ||
T_BITWISE_AND => T_BITWISE_AND, | ||
); | ||
|
||
/** | ||
* Do a version check to determine if this sniff needs to run at all. | ||
* | ||
* @return bool | ||
*/ | ||
protected function bowOutEarly() | ||
{ | ||
return ($this->supportsBelow('7.2') === false); | ||
} | ||
|
||
/** | ||
* Examine the contents of a list construct to determine whether an error needs to be thrown. | ||
* | ||
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $opener The position of the list open token. | ||
* @param int $closer The position of the list close token. | ||
* | ||
* @return void | ||
*/ | ||
protected function examineList(\PHP_CodeSniffer_File $phpcsFile, $opener, $closer) | ||
{ | ||
$start = $opener; | ||
while (($start = $this->hasTargetInList($phpcsFile, $start, $closer)) !== false) { | ||
$phpcsFile->addError( | ||
'Reference assignments within list constructs are not supported in PHP 7.2 or earlier.', | ||
$start, | ||
'Found' | ||
); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
PHPCompatibility/Tests/Sniffs/Lists/NewListReferenceAssignmentUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* Valid prior to PHP 7.3. | ||
*/ | ||
list($id1, $name1) = $data[0]; | ||
[$id1, $name1] = $data[0]; | ||
|
||
foreach ($data as list($id, $name)) {} | ||
foreach ($data as [$id, $name]) {} | ||
|
||
|
||
/* | ||
* PHP 7.3: list() reference assignments. | ||
*/ | ||
list($a, &$b) = $array; | ||
[$a, &$b] = $array; | ||
|
||
list( | ||
&$a, | ||
$b, | ||
, | ||
list( | ||
&$c, | ||
$d | ||
) | ||
) = $array; | ||
|
||
[ | ||
&$a, | ||
$b, | ||
, | ||
[&$c, &$d] | ||
] = $array; | ||
|
||
foreach ($array as list(&$a, &$b)) {} | ||
foreach ($array as [&$a, $b]) {} |
117 changes: 117 additions & 0 deletions
117
PHPCompatibility/Tests/Sniffs/Lists/NewListReferenceAssignmentUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* PHP 7.3 list reference assignments sniff test file. | ||
* | ||
* @package PHPCompatibility | ||
*/ | ||
|
||
namespace PHPCompatibility\Tests\Sniffs\Lists; | ||
|
||
use PHPCompatibility\Tests\BaseSniffTest; | ||
|
||
/** | ||
* PHP 7.3 list reference assignments sniff test file. | ||
* | ||
* @group newListReferenceAssignment | ||
* @group lists | ||
* | ||
* @covers \PHPCompatibility\Sniffs\Lists\NewListReferenceAssignmentSniff | ||
* | ||
* @uses \PHPCompatibility\Tests\BaseSniffTest | ||
* @package PHPCompatibility | ||
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl> | ||
*/ | ||
class NewListReferenceAssignmentUnitTest extends BaseSniffTest | ||
{ | ||
const TEST_FILE = 'Sniffs/Lists/NewListReferenceAssignmentUnitTest.inc'; | ||
|
||
/** | ||
* testNewListReferenceAssignment | ||
* | ||
* @dataProvider dataNewListReferenceAssignment | ||
* | ||
* @param int $line Line number where the error should occur. | ||
* | ||
* @return void | ||
*/ | ||
public function testNewListReferenceAssignment($line) | ||
{ | ||
$file = $this->sniffFile(self::TEST_FILE, '7.2'); | ||
$this->assertError($file, $line, 'Reference assignments within list constructs are not supported in PHP 7.2 or earlier.'); | ||
} | ||
|
||
/** | ||
* dataNewListReferenceAssignment | ||
* | ||
* @see testNewListReferenceAssignment() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNewListReferenceAssignment() | ||
{ | ||
return array( | ||
array(16), | ||
array(17), | ||
array(20), | ||
array(24), | ||
array(30), | ||
array(33), // x2. | ||
array(36), // x2. | ||
array(37), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* testNoFalsePositives | ||
* | ||
* @dataProvider dataNoFalsePositives | ||
* | ||
* @param int $line Line number with a valid list assignment. | ||
* | ||
* @return void | ||
*/ | ||
public function testNoFalsePositives($line) | ||
{ | ||
$file = $this->sniffFile(self::TEST_FILE, '7.2'); | ||
$this->assertNoViolation($file, $line); | ||
} | ||
|
||
/** | ||
* dataNoFalsePositives | ||
* | ||
* @see testNoFalsePositives() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNoFalsePositives() | ||
{ | ||
return array( | ||
array(6), | ||
array(7), | ||
array(9), | ||
array(10), | ||
array(19), | ||
array(21), | ||
array(22), | ||
array(23), | ||
array(25), | ||
array(29), | ||
array(31), | ||
array(32), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Verify no notices are thrown at all. | ||
* | ||
* @return void | ||
*/ | ||
public function testNoViolationsInFileOnValidVersion() | ||
{ | ||
$file = $this->sniffFile(self::TEST_FILE, '7.3'); | ||
$this->assertNoViolation($file); | ||
} | ||
|
||
} |