Skip to content

Commit bb1bc39

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into simplify-install-instructions
2 parents 12ae332 + d7ece6d commit bb1bc39

File tree

7 files changed

+318
-166
lines changed

7 files changed

+318
-166
lines changed

.github/workflows/php.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
php-version:
16-
- "8.1"
1716
- "8.2"
1817
- "8.3"
18+
- "8.4"
1919
dependencies:
2020
- "lowest"
2121
- "highest"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Templates;
20+
21+
use PHP_CodeSniffer\Files\File;
22+
use PHP_CodeSniffer\Sniffs\Sniff;
23+
24+
/**
25+
* Templates must not instantiate new objects within their code.
26+
* All objects must be passed from the Block object.
27+
*
28+
* @see https://developer.adobe.com/commerce/php/coding-standards/technical-guidelines/#62-presentation-layer 6.2.6
29+
* @link https://developer.adobe.com/commerce/frontend-core/guide/layouts/xml-instructions/#obtain-arguments-examples-in-template
30+
* @link https://developer.adobe.com/commerce/frontend-core/guide/templates/override/#getting-argument-values-from-layout
31+
*/
32+
class ObjectManagerSniff implements Sniff
33+
{
34+
private const WARNING_CODE_OBJECT_MANAGER_USAGE = 'ObjectManagerUsageFound';
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function register()
40+
{
41+
return [T_DOUBLE_COLON];
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function process(File $phpcsFile, $stackPtr)
48+
{
49+
$tokens = $phpcsFile->getTokens();
50+
51+
if ($tokens[$stackPtr - 1]['content'] !== 'ObjectManager'
52+
&& $tokens[$stackPtr + 1]['content'] !== 'getInstance'
53+
) {
54+
return;
55+
}
56+
57+
$phpcsFile->addWarning(
58+
'ObjectManager should not be used in .phtml template. ' .
59+
'Templates must not instantiate new objects within their code. ' .
60+
'All objects must be passed from the Block object.',
61+
$stackPtr,
62+
self::WARNING_CODE_OBJECT_MANAGER_USAGE
63+
);
64+
}
65+
}
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+
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\Templates;
20+
21+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
22+
23+
class ObjectManagerUnitTest extends AbstractSniffUnitTest
24+
{
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getWarningList($filename = '')
29+
{
30+
if ($filename === 'ObjectManagerUnitTest.1.phtml.inc') {
31+
return [
32+
18 => 1
33+
];
34+
}
35+
return [];
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getErrorList($filename = '')
42+
{
43+
return [];
44+
}
45+
}

Magento2/ruleset.xml

+5
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.Templates.ObjectManager">
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>

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"AFL-3.0"
77
],
88
"type": "phpcodesniffer-standard",
9-
"version": "35",
9+
"version": "36",
1010
"require": {
11-
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
11+
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
1212
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2 || ^1.0",
1313
"webonyx/graphql-php": "^15.0",
1414
"ext-simplexml": "*",

0 commit comments

Comments
 (0)