Skip to content

Commit

Permalink
PHP 8.0 | PSR1/SideEffects: allow for nullsafe object operator
Browse files Browse the repository at this point in the history
Includes unit tests.

Note: adding the token to the second condition (test 15/16) seems redundant and the original `T_OBJECT_OPERATOR` could be removed here too as, if an instantiated object is seen before the `defined()`, a side-effect would already have registered for the variable anyway.
  • Loading branch information
jrfnl committed Sep 1, 2020
1 parent 85f3521 commit 7e6daaf
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.10.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.11.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.12.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.13.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.14.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.15.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.16.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SideEffectsUnitTest.php" role="test" />
</dir>
<dir name="Methods">
Expand Down
5 changes: 4 additions & 1 deletion src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_DOUBLE_COLON
&& $tokens[$prev]['code'] !== T_FUNCTION
) {
Expand All @@ -216,11 +217,13 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
&& strtolower($tokens[$i]['content']) === 'defined'
) {
$openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
if ($tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS
if ($openBracket !== false
&& $tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS
&& isset($tokens[$openBracket]['parenthesis_closer']) === true
) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
if ($tokens[$prev]['code'] !== T_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_NULLSAFE_OBJECT_OPERATOR
&& $tokens[$prev]['code'] !== T_DOUBLE_COLON
&& $tokens[$prev]['code'] !== T_FUNCTION
) {
Expand Down
2 changes: 2 additions & 0 deletions src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.13.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
defined('MINSIZE') === false OR $my->define("MAXSIZE", 100);
2 changes: 2 additions & 0 deletions src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.14.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
defined('MINSIZE') === false OR $my?->define("MAXSIZE", 100);
2 changes: 2 additions & 0 deletions src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.15.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$my->defined('MINSIZE') or define("MAXSIZE", 100);
2 changes: 2 additions & 0 deletions src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.16.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$my?->defined('MINSIZE') or define("MAXSIZE", 100);
2 changes: 2 additions & 0 deletions src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function getWarningList($testFile='')
case 'SideEffectsUnitTest.5.inc':
case 'SideEffectsUnitTest.10.inc':
case 'SideEffectsUnitTest.12.inc':
case 'SideEffectsUnitTest.15.inc':
case 'SideEffectsUnitTest.16.inc':
return [1 => 1];
default:
return [];
Expand Down

0 comments on commit 7e6daaf

Please sign in to comment.