Skip to content

#210: Fixed HtmlDirectiveSniff.php from causing the Fatal Error for c… #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Magento2/Sniffs/Html/HtmlDirectiveSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Magento2\Sniffs\Html;

use Magento\Framework\Filter\Template;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

Expand All @@ -17,6 +16,11 @@
*/
class HtmlDirectiveSniff implements Sniff
{
const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si';
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si';
const LOOP_PATTERN = '/{{for(?P<loopItem>.*? )(in)(?P<loopData>.*?)}}(?P<loopBody>.*?){{\/for}}/si';
const CONSTRUCTION_PATTERN = '/{{([a-z]{0,10})(.*?)}}(?:(.*?)(?:{{\/(?:\\1)}}))?/si';

/**
* @var array
*/
Expand Down Expand Up @@ -73,7 +77,7 @@ public function process(File $phpcsFile, $stackPtr)
*/
private function processIfDirectives(string $html, File $phpcsFile): string
{
if (preg_match_all(Template::CONSTRUCTION_IF_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
if (preg_match_all(self::CONSTRUCTION_IF_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
foreach ($constructions as $construction) {
// validate {{if <var>}}
$this->validateVariableUsage($phpcsFile, $construction[1]);
Expand All @@ -93,7 +97,7 @@ private function processIfDirectives(string $html, File $phpcsFile): string
*/
private function processDependDirectives(string $html, File $phpcsFile): string
{
if (preg_match_all(Template::CONSTRUCTION_DEPEND_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
if (preg_match_all(self::CONSTRUCTION_DEPEND_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
foreach ($constructions as $construction) {
// validate {{depend <var>}}
$this->validateVariableUsage($phpcsFile, $construction[1]);
Expand All @@ -113,7 +117,7 @@ private function processDependDirectives(string $html, File $phpcsFile): string
*/
private function processForDirectives(string $html, File $phpcsFile): string
{
if (preg_match_all(Template::LOOP_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
if (preg_match_all(self::LOOP_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
foreach ($constructions as $construction) {
// validate {{for in <var>}}
$this->validateVariableUsage($phpcsFile, $construction['loopData']);
Expand All @@ -133,7 +137,7 @@ private function processForDirectives(string $html, File $phpcsFile): string
*/
private function processVarDirectivesAndParams(string $html, File $phpcsFile): string
{
if (preg_match_all(Template::CONSTRUCTION_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
if (preg_match_all(self::CONSTRUCTION_PATTERN, $html, $constructions, PREG_SET_ORDER)) {
foreach ($constructions as $construction) {
if (empty($construction[2])) {
continue;
Expand Down