Skip to content

Commit 5b71a74

Browse files
committed
ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files
1 parent 7521587 commit 5b71a74

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* **********************************************************************
16+
*/
17+
declare(strict_types = 1);
18+
19+
namespace Magento2\Sniffs\Phtml;
20+
21+
use PHP_CodeSniffer\Files\File;
22+
use PHP_CodeSniffer\Sniffs\Sniff;
23+
24+
class PhtmlTemplateObjectManagerSniff implements Sniff
25+
{
26+
private const WARNING_CODE_OBJECT_MANAGER_USAGE = 'ObjectManagerUsageFound';
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function register()
32+
{
33+
return [T_DOUBLE_COLON];
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$tokens = $phpcsFile->getTokens();
42+
43+
if ($tokens[$stackPtr - 1]['content'] !== 'ObjectManager'
44+
&& $tokens[$stackPtr + 1]['content'] !== 'getInstance'
45+
) {
46+
return;
47+
}
48+
49+
$phpcsFile->addWarning(
50+
'ObjectManager should not be used in .phtml template ' .
51+
'as it’s not a template’s responsibility to create objects.',
52+
$stackPtr,
53+
self::WARNING_CODE_OBJECT_MANAGER_USAGE
54+
);
55+
}
56+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* **********************************************************************
16+
*/
17+
18+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
19+
?>
20+
21+
<div class="block test">
22+
</div>
23+
<script type="jquery/ui">
24+
</script>
25+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* **********************************************************************
16+
*/
17+
declare(strict_types = 1);
18+
19+
namespace Magento2\Tests\Phtml;
20+
21+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
22+
23+
class PhtmlTemplateObjectManagerUnitTest extends AbstractSniffUnitTest
24+
{
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getWarningList($filename = '')
29+
{
30+
if ($filename === 'PhtmlTemplateObjectManager.1.phtml.inc') {
31+
return [
32+
7 => 1
33+
];
34+
}
35+
return [];
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getErrorList($filename = '')
42+
{
43+
return [];
44+
}
45+
}

Magento2/ruleset.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@
350350
<severity>8</severity>
351351
<type>warning</type>
352352
</rule>
353+
<rule ref="Magento2.Phtml.PhtmlTemplateObjectManager">
354+
<include-pattern>*\.phtml$</include-pattern>
355+
<severity>8</severity>
356+
<type>warning</type>
357+
</rule>
353358
<rule ref="Magento2.Legacy.ObsoleteConnection">
354359
<severity>8</severity>
355360
<type>warning</type>

0 commit comments

Comments
 (0)