@@ -49,8 +49,23 @@ public function process(File $phpcsFile, $stackPtr)
49
49
return ;
50
50
}
51
51
52
- $ this ->usedVariables = [];
52
+ $ html = $ this ->processIfDirectives ($ html , $ phpcsFile );
53
+ $ html = $ this ->processDependDirectives ($ html , $ phpcsFile );
54
+ $ html = $ this ->processForDirectives ($ html , $ phpcsFile );
55
+ $ html = $ this ->processVarDirectivesAndParams ($ html , $ phpcsFile );
53
56
57
+ $ this ->validateDefinedVariables ($ phpcsFile , $ html );
58
+ }
59
+
60
+ /**
61
+ * Process the {{if}} directives in the file
62
+ *
63
+ * @param string $html
64
+ * @param File $phpcsFile
65
+ * @return string The processed template
66
+ */
67
+ private function processIfDirectives (string $ html , File $ phpcsFile ): string
68
+ {
54
69
if (preg_match_all (Template::CONSTRUCTION_IF_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
55
70
foreach ($ constructions as $ construction ) {
56
71
// validate {{if <var>}}
@@ -59,6 +74,18 @@ public function process(File $phpcsFile, $stackPtr)
59
74
}
60
75
}
61
76
77
+ return $ html ;
78
+ }
79
+
80
+ /**
81
+ * Process the {{depend}} directives in the file
82
+ *
83
+ * @param string $html
84
+ * @param File $phpcsFile
85
+ * @return string The processed template
86
+ */
87
+ private function processDependDirectives (string $ html , File $ phpcsFile ): string
88
+ {
62
89
if (preg_match_all (Template::CONSTRUCTION_DEPEND_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
63
90
foreach ($ constructions as $ construction ) {
64
91
// validate {{depend <var>}}
@@ -67,6 +94,18 @@ public function process(File $phpcsFile, $stackPtr)
67
94
}
68
95
}
69
96
97
+ return $ html ;
98
+ }
99
+
100
+ /**
101
+ * Process the {{for}} directives in the file
102
+ *
103
+ * @param string $html
104
+ * @param File $phpcsFile
105
+ * @return string The processed template
106
+ */
107
+ private function processForDirectives (string $ html , File $ phpcsFile ): string
108
+ {
70
109
if (preg_match_all (Template::LOOP_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
71
110
foreach ($ constructions as $ construction ) {
72
111
// validate {{for in <var>}}
@@ -75,6 +114,18 @@ public function process(File $phpcsFile, $stackPtr)
75
114
}
76
115
}
77
116
117
+ return $ html ;
118
+ }
119
+
120
+ /**
121
+ * Process the all var directives and var directive params in the file
122
+ *
123
+ * @param string $html
124
+ * @param File $phpcsFile
125
+ * @return string The processed template
126
+ */
127
+ private function processVarDirectivesAndParams (string $ html , File $ phpcsFile ): string
128
+ {
78
129
if (preg_match_all (Template::CONSTRUCTION_PATTERN , $ html , $ constructions , PREG_SET_ORDER )) {
79
130
foreach ($ constructions as $ construction ) {
80
131
if (empty ($ construction [2 ])) {
@@ -89,7 +140,7 @@ public function process(File $phpcsFile, $stackPtr)
89
140
}
90
141
}
91
142
92
- $ this -> validateDefinedVariables ( $ phpcsFile , $ html) ;
143
+ return $ html ;
93
144
}
94
145
95
146
/**
@@ -187,18 +238,5 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText)
187
238
'HtmlTemplates.DirectiveUsage.UndefinedVariable '
188
239
);
189
240
}
190
-
191
- $ extraDefinedVariables = array_diff ($ definedVariables , $ this ->usedVariables );
192
- foreach ($ extraDefinedVariables as $ extraDefinedVariable ) {
193
- if (substr ($ extraDefinedVariable , 0 , 4 ) !== 'var ' ) {
194
- continue ;
195
- }
196
- $ phpcsFile ->addError (
197
- 'Template @vars comment block contains a variable not used in the template. ' . PHP_EOL
198
- . 'Extra variable: ' . $ extraDefinedVariable ,
199
- null ,
200
- 'HtmlTemplates.DirectiveUsage.ExtraVariable '
201
- );
202
- }
203
241
}
204
242
}
0 commit comments