From 877e89f5f1db8d43f4044d6da32aad33e7c06b39 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 1 Aug 2020 01:51:35 +0200 Subject: [PATCH] PHP 8.0 | PSR12/ClassInstantiation: allow for nullsafe object operator Includes unit tests. --- .../Sniffs/Classes/ClassInstantiationSniff.php | 17 +++++++++-------- .../Classes/ClassInstantiationUnitTest.inc | 4 ++++ .../ClassInstantiationUnitTest.inc.fixed | 4 ++++ .../Classes/ClassInstantiationUnitTest.php | 2 ++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php b/src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php index f474a26b8d..804ecfe1c7 100644 --- a/src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php +++ b/src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php @@ -44,14 +44,15 @@ public function process(File $phpcsFile, $stackPtr) // Find the class name. $allowed = [ - T_STRING => T_STRING, - T_NS_SEPARATOR => T_NS_SEPARATOR, - T_SELF => T_SELF, - T_STATIC => T_STATIC, - T_VARIABLE => T_VARIABLE, - T_DOLLAR => T_DOLLAR, - T_OBJECT_OPERATOR => T_OBJECT_OPERATOR, - T_DOUBLE_COLON => T_DOUBLE_COLON, + T_STRING => T_STRING, + T_NS_SEPARATOR => T_NS_SEPARATOR, + T_SELF => T_SELF, + T_STATIC => T_STATIC, + T_VARIABLE => T_VARIABLE, + T_DOLLAR => T_DOLLAR, + T_OBJECT_OPERATOR => T_OBJECT_OPERATOR, + T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR, + T_DOUBLE_COLON => T_DOUBLE_COLON, ]; $allowed += Tokens::$emptyTokens; diff --git a/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc b/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc index dcd849e6bd..ca0dc3614a 100644 --- a/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc @@ -32,3 +32,7 @@ $foo = new $bar['a'] [$baz['a']/* comment */ ['b']]['b']; $a = new self::$transport[$cap_string]; $renderer = new $this->inline_diff_renderer; $a = new ${$varHoldingClassName}; + +$class = new $obj?->classname(); +$class = new $obj?->classname; +$class = new ${$obj?->classname}; diff --git a/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed index d8438dac13..a4d209cc16 100644 --- a/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed @@ -32,3 +32,7 @@ $foo = new $bar['a'] [$baz['a']/* comment */ ['b']]['b'](); $a = new self::$transport[$cap_string](); $renderer = new $this->inline_diff_renderer(); $a = new ${$varHoldingClassName}(); + +$class = new $obj?->classname(); +$class = new $obj?->classname(); +$class = new ${$obj?->classname}(); diff --git a/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.php b/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.php index 40c90c8306..3fb1ab992b 100644 --- a/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.php +++ b/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.php @@ -41,6 +41,8 @@ public function getErrorList() 32 => 1, 33 => 1, 34 => 1, + 37 => 1, + 38 => 1, ]; }//end getErrorList()