-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from adobe-commerce-tier-4/ACP2E-2977
ACP2E-2977: Implement Static test which will check for no usage of object manager in .phtml files
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
Magento2/Tests/Templates/ObjectManagerUnitTest.1.phtml.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters