diff --git a/Magento2/Sniffs/Templates/ObjectManagerSniff.php b/Magento2/Sniffs/Templates/ObjectManagerSniff.php new file mode 100644 index 00000000..24d4c19e --- /dev/null +++ b/Magento2/Sniffs/Templates/ObjectManagerSniff.php @@ -0,0 +1,65 @@ +getTokens(); + + if ($tokens[$stackPtr - 1]['content'] !== 'ObjectManager' + && $tokens[$stackPtr + 1]['content'] !== 'getInstance' + ) { + return; + } + + $phpcsFile->addWarning( + '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 + ); + } +} diff --git a/Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc b/Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc new file mode 100644 index 00000000..badbf38c --- /dev/null +++ b/Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc @@ -0,0 +1,25 @@ + + +
+
+ + diff --git a/Magento2/Tests/Templates/ObjectManagerUnitTest.php b/Magento2/Tests/Templates/ObjectManagerUnitTest.php new file mode 100644 index 00000000..755b18bb --- /dev/null +++ b/Magento2/Tests/Templates/ObjectManagerUnitTest.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..c425aea7 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -350,6 +350,11 @@ 8 warning + + *\.phtml$ + 8 + warning + 8 warning