Conditions | 16 |
Paths | 8 |
Total Lines | 109 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
94 | public function render(Context $context): string |
||
95 | { |
||
96 | $context->push(); |
||
97 | $lexerLogical = new Lexer('/\s+(and|or)\s+/'); |
||
98 | $lexerConditional = new Lexer( |
||
99 | '/(' |
||
100 | . Token::QUOTED_FRAGMENT |
||
101 | . ')\s*([=!<>a-z_]+)?\s*(' |
||
102 | . Token::QUOTED_FRAGMENT |
||
103 | . ')?/' |
||
104 | ); |
||
105 | |||
106 | $result = ''; |
||
107 | foreach ($this->blocks as $block) { |
||
108 | if ($block[0] === 'else') { |
||
109 | $result = $this->renderAll($block[2], $context); |
||
110 | |||
111 | break; |
||
112 | } |
||
113 | |||
114 | if ($block[0] === 'if' || $block[0] === 'elseif') { |
||
115 | // Extract logical operators |
||
116 | $lexerLogical->matchAll($block[1]); |
||
117 | $operators = $lexerLogical->getArrayMatch(1); |
||
118 | // Extract individual conditions |
||
119 | $individualConditions = $lexerLogical->split($block[1]); |
||
120 | |||
121 | $conditions = []; |
||
122 | foreach ($individualConditions as $condition) { |
||
123 | if ($lexerConditional->match($condition)) { |
||
124 | $left = $lexerConditional->isMatchNotNull(1) |
||
125 | ? $lexerConditional->getStringMatch(1) |
||
126 | : null; |
||
127 | $operator = $lexerConditional->isMatchNotNull(2) |
||
128 | ? $lexerConditional->getStringMatch(2) |
||
129 | : null; |
||
130 | $right = $lexerConditional->isMatchNotNull(3) |
||
131 | ? $lexerConditional->getStringMatch(3) |
||
132 | : null; |
||
133 | |||
134 | array_push($conditions, [ |
||
135 | 'left' => $left, |
||
136 | 'operator' => $operator, |
||
137 | 'right' => $right, |
||
138 | ]); |
||
139 | } else { |
||
140 | throw new ParseException(sprintf( |
||
141 | 'Syntax Error in "%s" - Valid syntax: if [conditions]', |
||
142 | 'if' |
||
143 | )); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if (count($operators) > 0) { |
||
148 | // If statement contains and/or |
||
149 | $display = $this->evaluateCondition( |
||
150 | $conditions[0]['left'], |
||
151 | $conditions[0]['right'], |
||
152 | $conditions[0]['operator'], |
||
153 | $context |
||
154 | ); |
||
155 | |||
156 | foreach ($operators as $key => $operator) { |
||
157 | if ($operator === 'and') { |
||
158 | $display = ( |
||
159 | $display |
||
160 | && $this->evaluateCondition( |
||
161 | $conditions[$key + 1]['left'], |
||
162 | $conditions[$key + 1]['right'], |
||
163 | $conditions[$key + 1]['operator'], |
||
164 | $context |
||
165 | ) |
||
166 | ); |
||
167 | } else { |
||
168 | $display = ( |
||
169 | $display |
||
170 | || $this->evaluateCondition( |
||
171 | $conditions[$key + 1]['left'], |
||
172 | $conditions[$key + 1]['right'], |
||
173 | $conditions[$key + 1]['operator'], |
||
174 | $context |
||
175 | ) |
||
176 | ); |
||
177 | } |
||
178 | } |
||
179 | } else { |
||
180 | // If statement is a single condition |
||
181 | $display = $this->evaluateCondition( |
||
182 | $conditions[0]['left'], |
||
183 | $conditions[0]['right'], |
||
184 | $conditions[0]['operator'], |
||
185 | $context |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | // hook for if not tag |
||
190 | $display = $this->negateCondition($display); |
||
191 | |||
192 | if ($display) { |
||
193 | $result = $this->renderAll($block[2], $context); |
||
194 | |||
195 | break; |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | $context->pop(); |
||
201 | |||
202 | return $result; |
||
203 | } |
||
233 |