Skip to content

Commit

Permalink
Merge pull request #265 from adobe-commerce-tier-4/ACP2E-2977
Browse files Browse the repository at this point in the history
ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files
  • Loading branch information
sidolov authored Oct 22, 2024
2 parents f5cf74f + 86dbcf7 commit 95d3139
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Magento2/Sniffs/Templates/ObjectManagerSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* **********************************************************************
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Templates;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* 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 ObjectManagerSniff implements Sniff
{
private const WARNING_CODE_OBJECT_MANAGER_USAGE = 'ObjectManagerUsageFound';

/**
* @inheritdoc
*/
public function register()
{
return [T_DOUBLE_COLON];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->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
);
}
}
25 changes: 25 additions & 0 deletions Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* **********************************************************************
*/

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
?>

<div class="block test">
</div>
<script type="jquery/ui">
</script>

45 changes: 45 additions & 0 deletions Magento2/Tests/Templates/ObjectManagerUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* **********************************************************************
*/
declare(strict_types = 1);

namespace Magento2\Tests\Templates;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ObjectManagerUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getWarningList($filename = '')
{
if ($filename === 'ObjectManagerUnitTest.1.phtml.inc') {
return [
18 => 1
];
}
return [];
}

/**
* @inheritdoc
*/
public function getErrorList($filename = '')
{
return [];
}
}
5 changes: 5 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Templates.ObjectManager">
<include-pattern>*\.phtml$</include-pattern>
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.ObsoleteConnection">
<severity>8</severity>
<type>warning</type>
Expand Down

0 comments on commit 95d3139

Please sign in to comment.