diff --git a/Magento2/Sniffs/PHP/LiteralNamespacesSniff.php b/Magento2/Sniffs/PHP/LiteralNamespacesSniff.php index d76dae3e..b40b94b4 100644 --- a/Magento2/Sniffs/PHP/LiteralNamespacesSniff.php +++ b/Magento2/Sniffs/PHP/LiteralNamespacesSniff.php @@ -53,11 +53,15 @@ public function process(File $phpcsFile, $stackPtr) } if (preg_match($this->literalNamespacePattern, $content) === 1) { - $phpcsFile->addWarning( + $fix = $phpcsFile->addFixableWarning( "Use ::class notation instead.", $stackPtr, 'LiteralClassUsage' ); + + if ($fix) { + $phpcsFile->fixer->replaceToken($stackPtr, '\\' . trim($content, '\\') . '::class'); + } } } } diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc index d6880b37..c0d7057d 100644 --- a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc @@ -10,5 +10,17 @@ class Fixture public function notOk() { $this->create('Magento\CustomerSegment\Model\Segment\Condition\Customer\Address'); + $this->create('Magento\\CustomerSegment\\Model\\Segment\\Condition\\Customer\\Address'); + $this->create("Magento\\CustomerSegment\\Model\\Segment\\Condition\\Customer\\Address"); + $this->create('\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address'); + $this->create('\Magento\\CustomerSegment\\Model\\Segment\\Condition\\Customer\\Address'); + $this->create("\Magento\\CustomerSegment\\Model\\Segment\\Condition\\Customer\\Address"); + } + + public function thisIsFine(string $class) + { + $this->create('Magento\\Module\\' . $class); + $this->create("Magento\\Module\\" . $class); + $this->create("Magento\\Module\\$class"); } } diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed new file mode 100644 index 00000000..122e134e --- /dev/null +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed @@ -0,0 +1,26 @@ +create(Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + } + + public function notOk() + { + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + } + + public function thisIsFine(string $class) + { + $this->create('Magento\\Module\\' . $class); + $this->create("Magento\\Module\\" . $class); + $this->create("Magento\\Module\\$class"); + } +} diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.php b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.php index bebdf4ed..c8789b64 100644 --- a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.php +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.php @@ -24,6 +24,11 @@ public function getWarningList() { return [ 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 16 => 1, + 17 => 1, ]; } }