88
99namespace Magento2 \Sniffs \Html ;
1010
11- use Magento \Framework \Filter \Template ;
1211use PHP_CodeSniffer \Sniffs \Sniff ;
1312use PHP_CodeSniffer \Files \File ;
1413
1716 */
1817class HtmlDirectiveSniff implements Sniff
1918{
19+ const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{ \\/depend\s*}}/si ' ;
20+ const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{ \\/if\s*}}/si ' ;
21+ const LOOP_PATTERN = '/{{for(?P<loopItem>.*? )(in)(?P<loopData>.*?)}}(?P<loopBody>.*?){{\/for}}/si ' ;
22+ const CONSTRUCTION_PATTERN = '/{{([a-z]{0,10})(.*?)}}(?:(.*?)(?:{{\/(?: \\1)}}))?/si ' ;
23+
2024 /**
2125 * @var array
2226 */
@@ -73,7 +77,7 @@ public function process(File $phpcsFile, $stackPtr)
7377 */
7478 private function processIfDirectives (string $ html , File $ phpcsFile ): string
7579 {
76- if (preg_match_all (Template ::CONSTRUCTION_IF_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
80+ if (preg_match_all (self ::CONSTRUCTION_IF_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
7781 foreach ($ constructions as $ construction ) {
7882 // validate {{if <var>}}
7983 $ this ->validateVariableUsage ($ phpcsFile , $ construction [1 ]);
@@ -93,7 +97,7 @@ private function processIfDirectives(string $html, File $phpcsFile): string
9397 */
9498 private function processDependDirectives (string $ html , File $ phpcsFile ): string
9599 {
96- if (preg_match_all (Template ::CONSTRUCTION_DEPEND_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
100+ if (preg_match_all (self ::CONSTRUCTION_DEPEND_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
97101 foreach ($ constructions as $ construction ) {
98102 // validate {{depend <var>}}
99103 $ this ->validateVariableUsage ($ phpcsFile , $ construction [1 ]);
@@ -113,7 +117,7 @@ private function processDependDirectives(string $html, File $phpcsFile): string
113117 */
114118 private function processForDirectives (string $ html , File $ phpcsFile ): string
115119 {
116- if (preg_match_all (Template ::LOOP_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
120+ if (preg_match_all (self ::LOOP_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
117121 foreach ($ constructions as $ construction ) {
118122 // validate {{for in <var>}}
119123 $ this ->validateVariableUsage ($ phpcsFile , $ construction ['loopData ' ]);
@@ -133,7 +137,7 @@ private function processForDirectives(string $html, File $phpcsFile): string
133137 */
134138 private function processVarDirectivesAndParams (string $ html , File $ phpcsFile ): string
135139 {
136- if (preg_match_all (Template ::CONSTRUCTION_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
140+ if (preg_match_all (self ::CONSTRUCTION_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
137141 foreach ($ constructions as $ construction ) {
138142 if (empty ($ construction [2 ])) {
139143 continue ;
0 commit comments