From 5b71a74b293170aa29c3b86a03e646e95931adca Mon Sep 17 00:00:00 2001 From: Olga Moyseyenko Date: Fri, 26 Apr 2024 17:27:15 -0500 Subject: [PATCH 1/6] ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files --- .../Phtml/PhtmlTemplateObjectManagerSniff.php | 56 +++++++++++++++++++ .../PhtmlTemplateObjectManager.1.phtml.inc | 25 +++++++++ .../PhtmlTemplateObjectManagerUnitTest.php | 45 +++++++++++++++ Magento2/ruleset.xml | 5 ++ 4 files changed, 131 insertions(+) create mode 100644 Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php create mode 100644 Magento2/Tests/Phtml/PhtmlTemplateObjectManager.1.phtml.inc create mode 100644 Magento2/Tests/Phtml/PhtmlTemplateObjectManagerUnitTest.php diff --git a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php b/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php new file mode 100644 index 00000000..ea2a1b2e --- /dev/null +++ b/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php @@ -0,0 +1,56 @@ +getTokens(); + + if ($tokens[$stackPtr - 1]['content'] !== 'ObjectManager' + && $tokens[$stackPtr + 1]['content'] !== 'getInstance' + ) { + return; + } + + $phpcsFile->addWarning( + 'ObjectManager should not be used in .phtml template ' . + 'as it’s not a template’s responsibility to create objects.', + $stackPtr, + self::WARNING_CODE_OBJECT_MANAGER_USAGE + ); + } +} diff --git a/Magento2/Tests/Phtml/PhtmlTemplateObjectManager.1.phtml.inc b/Magento2/Tests/Phtml/PhtmlTemplateObjectManager.1.phtml.inc new file mode 100644 index 00000000..badbf38c --- /dev/null +++ b/Magento2/Tests/Phtml/PhtmlTemplateObjectManager.1.phtml.inc @@ -0,0 +1,25 @@ + + +
+
+ + diff --git a/Magento2/Tests/Phtml/PhtmlTemplateObjectManagerUnitTest.php b/Magento2/Tests/Phtml/PhtmlTemplateObjectManagerUnitTest.php new file mode 100644 index 00000000..be871dff --- /dev/null +++ b/Magento2/Tests/Phtml/PhtmlTemplateObjectManagerUnitTest.php @@ -0,0 +1,45 @@ + 1 + ]; + } + return []; + } + + /** + * @inheritdoc + */ + public function getErrorList($filename = '') + { + return []; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 4ac058c0..6e4ff7ad 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -350,6 +350,11 @@ 8 warning + + *\.phtml$ + 8 + warning + 8 warning From 81676df23dd25beda75a28d395c1ef854b5f13bc Mon Sep 17 00:00:00 2001 From: Olga Moyseyenko Date: Tue, 30 Apr 2024 12:21:18 -0500 Subject: [PATCH 2/6] ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files --- .../Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php b/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php index ea2a1b2e..e2446a10 100644 --- a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php +++ b/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php @@ -21,6 +21,14 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +/** + * Class PhtmlTemplateObjectManagerSniff + * + * Templates must not instantiate new objects within their code. + * All objects must be passed from the Block object. + * + * @link https://developer.adobe.com/commerce/frontend-core/guide/templates/override/#getting-argument-values-from-layout + */ class PhtmlTemplateObjectManagerSniff implements Sniff { private const WARNING_CODE_OBJECT_MANAGER_USAGE = 'ObjectManagerUsageFound'; @@ -48,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->addWarning( 'ObjectManager should not be used in .phtml template ' . - 'as it’s not a template’s responsibility to create objects.', + 'as it\'s not a template\'s responsibility to create objects.', $stackPtr, self::WARNING_CODE_OBJECT_MANAGER_USAGE ); From 06a96f8fd1fe2d316b10e055c74154d6eeb457b4 Mon Sep 17 00:00:00 2001 From: Olga Moyseyenko Date: Tue, 30 Apr 2024 13:52:59 -0500 Subject: [PATCH 3/6] ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files --- Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php b/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php index e2446a10..a8371d47 100644 --- a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php +++ b/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php @@ -55,8 +55,9 @@ public function process(File $phpcsFile, $stackPtr) } $phpcsFile->addWarning( - 'ObjectManager should not be used in .phtml template ' . - 'as it\'s not a template\'s responsibility to create objects.', + 'ObjectManager should not be used in .phtml template. ' . + 'Templates must not instantiate new objects within their code. ' . + 'All objects must be passed from the Block object.', $stackPtr, self::WARNING_CODE_OBJECT_MANAGER_USAGE ); From 8850ff7a03a0de4e506d810a045156b2fb7a0c7c Mon Sep 17 00:00:00 2001 From: Olga Moyseyenko Date: Thu, 27 Jun 2024 19:52:54 -0500 Subject: [PATCH 4/6] ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files --- .../ObjectManagerSniff.php} | 6 ++--- .../ObjectManager.1.phtml} | 0 .../Tests/Templates/ObjectManager.1.phtml.inc | 25 +++++++++++++++++++ .../ObjectManagerUnitTest.php} | 0 Magento2/ruleset.xml | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) rename Magento2/Sniffs/{Phtml/PhtmlTemplateObjectManagerSniff.php => Templates/ObjectManagerSniff.php} (87%) rename Magento2/Tests/{Phtml/PhtmlTemplateObjectManager.1.phtml.inc => Templates/ObjectManager.1.phtml} (100%) create mode 100644 Magento2/Tests/Templates/ObjectManager.1.phtml.inc rename Magento2/Tests/{Phtml/PhtmlTemplateObjectManagerUnitTest.php => Templates/ObjectManagerUnitTest.php} (100%) diff --git a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php b/Magento2/Sniffs/Templates/ObjectManagerSniff.php similarity index 87% rename from Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php rename to Magento2/Sniffs/Templates/ObjectManagerSniff.php index a8371d47..3bb2ab98 100644 --- a/Magento2/Sniffs/Phtml/PhtmlTemplateObjectManagerSniff.php +++ b/Magento2/Sniffs/Templates/ObjectManagerSniff.php @@ -16,17 +16,17 @@ */ declare(strict_types = 1); -namespace Magento2\Sniffs\Phtml; +namespace Magento2\Sniffs\Templates; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; /** - * Class PhtmlTemplateObjectManagerSniff - * * Templates must not instantiate new objects within their code. * All objects must be passed from the Block object. * + * @see https://developer.adobe.com/commerce/php/coding-standards/technical-guidelines/#62-presentation-layer 6.2.6 + * @link https://developer.adobe.com/commerce/frontend-core/guide/layouts/xml-instructions/#obtain-arguments-examples-in-template * @link https://developer.adobe.com/commerce/frontend-core/guide/templates/override/#getting-argument-values-from-layout */ class PhtmlTemplateObjectManagerSniff implements Sniff diff --git a/Magento2/Tests/Phtml/PhtmlTemplateObjectManager.1.phtml.inc b/Magento2/Tests/Templates/ObjectManager.1.phtml similarity index 100% rename from Magento2/Tests/Phtml/PhtmlTemplateObjectManager.1.phtml.inc rename to Magento2/Tests/Templates/ObjectManager.1.phtml diff --git a/Magento2/Tests/Templates/ObjectManager.1.phtml.inc b/Magento2/Tests/Templates/ObjectManager.1.phtml.inc new file mode 100644 index 00000000..badbf38c --- /dev/null +++ b/Magento2/Tests/Templates/ObjectManager.1.phtml.inc @@ -0,0 +1,25 @@ + + +
+
+ + diff --git a/Magento2/Tests/Phtml/PhtmlTemplateObjectManagerUnitTest.php b/Magento2/Tests/Templates/ObjectManagerUnitTest.php similarity index 100% rename from Magento2/Tests/Phtml/PhtmlTemplateObjectManagerUnitTest.php rename to Magento2/Tests/Templates/ObjectManagerUnitTest.php diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 6e4ff7ad..c425aea7 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -350,7 +350,7 @@ 8 warning
- + *\.phtml$ 8 warning From b421a21f3b779216606299eebd0e821905ca3f8a Mon Sep 17 00:00:00 2001 From: Olga Moyseyenko Date: Thu, 27 Jun 2024 20:02:02 -0500 Subject: [PATCH 5/6] ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files --- .../Sniffs/Templates/ObjectManagerSniff.php | 2 +- .../Tests/Templates/ObjectManager.1.phtml | 25 ------------------- .../Tests/Templates/ObjectManagerUnitTest.php | 8 +++--- 3 files changed, 5 insertions(+), 30 deletions(-) delete mode 100644 Magento2/Tests/Templates/ObjectManager.1.phtml diff --git a/Magento2/Sniffs/Templates/ObjectManagerSniff.php b/Magento2/Sniffs/Templates/ObjectManagerSniff.php index 3bb2ab98..24d4c19e 100644 --- a/Magento2/Sniffs/Templates/ObjectManagerSniff.php +++ b/Magento2/Sniffs/Templates/ObjectManagerSniff.php @@ -29,7 +29,7 @@ * @link https://developer.adobe.com/commerce/frontend-core/guide/layouts/xml-instructions/#obtain-arguments-examples-in-template * @link https://developer.adobe.com/commerce/frontend-core/guide/templates/override/#getting-argument-values-from-layout */ -class PhtmlTemplateObjectManagerSniff implements Sniff +class ObjectManagerSniff implements Sniff { private const WARNING_CODE_OBJECT_MANAGER_USAGE = 'ObjectManagerUsageFound'; diff --git a/Magento2/Tests/Templates/ObjectManager.1.phtml b/Magento2/Tests/Templates/ObjectManager.1.phtml deleted file mode 100644 index badbf38c..00000000 --- a/Magento2/Tests/Templates/ObjectManager.1.phtml +++ /dev/null @@ -1,25 +0,0 @@ - - -
-
- - diff --git a/Magento2/Tests/Templates/ObjectManagerUnitTest.php b/Magento2/Tests/Templates/ObjectManagerUnitTest.php index be871dff..9a21f5ec 100644 --- a/Magento2/Tests/Templates/ObjectManagerUnitTest.php +++ b/Magento2/Tests/Templates/ObjectManagerUnitTest.php @@ -16,20 +16,20 @@ */ declare(strict_types = 1); -namespace Magento2\Tests\Phtml; +namespace Magento2\Tests\Templates; use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; -class PhtmlTemplateObjectManagerUnitTest extends AbstractSniffUnitTest +class ObjectManagerUnitTest extends AbstractSniffUnitTest { /** * @inheritdoc */ public function getWarningList($filename = '') { - if ($filename === 'PhtmlTemplateObjectManager.1.phtml.inc') { + if ($filename === 'ObjectManager.1.phtml.inc') { return [ - 7 => 1 + 18 => 1 ]; } return []; From a539f9633222cffbc8e6b596dbc3ee328504fcda Mon Sep 17 00:00:00 2001 From: Olga Moyseyenko Date: Thu, 27 Jun 2024 21:51:15 -0500 Subject: [PATCH 6/6] ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files --- ...ectManager.1.phtml.inc => ObjectManagerUnitTest.1.phtml.inc} | 0 Magento2/Tests/Templates/ObjectManagerUnitTest.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename Magento2/Tests/Templates/{ObjectManager.1.phtml.inc => ObjectManagerUnitTest.1.phtml.inc} (100%) diff --git a/Magento2/Tests/Templates/ObjectManager.1.phtml.inc b/Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc similarity index 100% rename from Magento2/Tests/Templates/ObjectManager.1.phtml.inc rename to Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc diff --git a/Magento2/Tests/Templates/ObjectManagerUnitTest.php b/Magento2/Tests/Templates/ObjectManagerUnitTest.php index 9a21f5ec..755b18bb 100644 --- a/Magento2/Tests/Templates/ObjectManagerUnitTest.php +++ b/Magento2/Tests/Templates/ObjectManagerUnitTest.php @@ -27,7 +27,7 @@ class ObjectManagerUnitTest extends AbstractSniffUnitTest */ public function getWarningList($filename = '') { - if ($filename === 'ObjectManager.1.phtml.inc') { + if ($filename === 'ObjectManagerUnitTest.1.phtml.inc') { return [ 18 => 1 ];