From c5fbbb3166de57b6acc90d664eb39c5988d1bd02 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 12:55:08 +0100 Subject: [PATCH 1/4] Add additional test cases --- Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc index d6880b37..13985c99 100644 --- a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc @@ -11,4 +11,11 @@ class Fixture { $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"); + } } From fcc76966d8af9a7853ed0ecb43a6d5426d696e04 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 12:55:52 +0100 Subject: [PATCH 2/4] Copy testcase ready for auto-fixing --- .../PHP/LiteralNamespacesUnitTest.inc.fixed | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed new file mode 100644 index 00000000..13985c99 --- /dev/null +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed @@ -0,0 +1,21 @@ +create(Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); + } + + public function notOk() + { + $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"); + } +} From 3c2e1c829dc748aa814619f30f52691aa2771a9a Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 13:01:55 +0100 Subject: [PATCH 3/4] Auto-fix `Magento2.PHP.LiteralNamespaces` --- Magento2/Sniffs/PHP/LiteralNamespacesSniff.php | 6 +++++- Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) 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.fixed b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed index 13985c99..31e0c532 100644 --- a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed @@ -9,7 +9,7 @@ class Fixture public function notOk() { - $this->create('Magento\CustomerSegment\Model\Segment\Condition\Customer\Address'); + $this->create(\Magento\CustomerSegment\Model\Segment\Condition\Customer\Address::class); } public function thisIsFine(string $class) From c6d9ffcc6f5a0f17c10ab11cf76b28269724d7e9 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 13:19:53 +0100 Subject: [PATCH 4/4] Add more test cases (already covered by the code) --- Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc | 5 +++++ Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed | 5 +++++ Magento2/Tests/PHP/LiteralNamespacesUnitTest.php | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc index 13985c99..c0d7057d 100644 --- a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc @@ -10,6 +10,11 @@ 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) diff --git a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed index 31e0c532..122e134e 100644 --- a/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed +++ b/Magento2/Tests/PHP/LiteralNamespacesUnitTest.inc.fixed @@ -10,6 +10,11 @@ class Fixture 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) 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, ]; } }