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 @@ + + +