Skip to content
50 changes: 50 additions & 0 deletions Magento2/Sniffs/Legacy/EmailTemplateSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

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

/**
* Test for obsolete email directives in view/email/*.html
*/
class EmailTemplateSniff implements Sniff
{
private const OBSOLETE_EMAIL_DIRECTIVES = [
'/\{\{htmlescape.*?\}\}/i' => 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.',
'/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.',
];

private const ERROR_CODE = 'FoundObsoleteEmailDirective';

/**
* @inheritdoc
*/
public function register(): array
{
return [
T_INLINE_HTML
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$content = $phpcsFile->getTokens()[$stackPtr]['content'];
foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $directiveRegex => $errorMessage) {
if (preg_match($directiveRegex, $content)) {
$phpcsFile->addError(
$errorMessage,
$stackPtr,
self::ERROR_CODE
);
}
}
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Legacy/EmailTemplateUnitTest.1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>{{var "H1"}}</h1>
<h2>{{var "H2"}}</h2>
<p>{{var "p"}} {{var "p"}}</p>

4 changes: 4 additions & 0 deletions Magento2/Tests/Legacy/EmailTemplateUnitTest.2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>{{htmlescape "H1"}}</h1>
<h2>{{escapehtml "H2"}}</h2>
<p>{{escapehtml "p"}} {{htmlescape "p"}}</p>
<p class="greeting">{{trans "Translateme"}}</p>
39 changes: 39 additions & 0 deletions Magento2/Tests/Legacy/EmailTemplateUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class EmailTemplateUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList($testFile = '')
{
if ($testFile === 'EmailTemplateUnitTest.1.html') {
return [];
}
if ($testFile === 'EmailTemplateUnitTest.2.html') {
return [
1 => 1,
2 => 1,
3 => 2,
];
}

return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = '')
{
return [];
}
}
6 changes: 6 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
<severity>10</severity>
<type>error</type>
</rule>
<rule ref="Magento2.Legacy.EmailTemplate.FoundObsoleteEmailDirective">
<include-pattern>view/email/*.html</include-pattern>
<include-pattern>view/*/email/*.html</include-pattern>
<severity>10</severity>
<type>error</type>
</rule>

<!-- Severity 9 warnings: Possible security and issues that may cause bugs. -->
<rule ref="Generic.Files.ByteOrderMark">
Expand Down