From ef289d92a54f45b29ff858d669487603642e6309 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 1 Aug 2020 01:53:07 +0200 Subject: [PATCH] PHP 8.0 | Squiz/ValidVariableName: allow for nullsafe object operator Includes unit test. --- .../Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php | 4 +++- .../Tests/NamingConventions/ValidVariableNameUnitTest.inc | 4 ++++ .../Tests/NamingConventions/ValidVariableNameUnitTest.php | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php index ed2505817b..5f17a08b53 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php @@ -38,7 +38,9 @@ protected function processVariable(File $phpcsFile, $stackPtr) } $objOperator = $phpcsFile->findNext([T_WHITESPACE], ($stackPtr + 1), null, true); - if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) { + if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR + || $tokens[$objOperator]['code'] === T_NULLSAFE_OBJECT_OPERATOR + ) { // Check to see if we are using a variable from an object. $var = $phpcsFile->findNext([T_WHITESPACE], ($objOperator + 1), null, true); if ($tokens[$var]['code'] === T_STRING) { diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc index 5692d6b33b..c7b8a2b6b2 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc @@ -142,3 +142,7 @@ $anonClass = new class() { } }; +echo $obj?->varName; +echo $obj?->var_name; +echo $obj?->varname; +echo $obj?->_varName; diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php index 4ec65e5e1a..aaa9d099a7 100644 --- a/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.php @@ -60,6 +60,7 @@ public function getErrorList() 123 => 1, 138 => 1, 141 => 1, + 146 => 1, ]; return $errors;