| 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 | ||
| 95 | public function render(Context $context): string | ||
| 96 |     { | ||
| 97 | $context->push(); | ||
| 98 |         $lexerLogical = new Lexer('/\s+(and|or)\s+/'); | ||
| 99 | $lexerConditional = new Lexer( | ||
| 100 |             '/(' | ||
| 101 | . Token::QUOTED_FRAGMENT | ||
| 102 |                 . ')\s*([=!<>a-z_]+)?\s*(' | ||
| 103 | . Token::QUOTED_FRAGMENT | ||
| 104 | . ')?/' | ||
| 105 | ); | ||
| 106 | |||
| 107 | $result = ''; | ||
| 108 |         foreach ($this->blocks as $block) { | ||
| 109 |             if ($block[0] === 'else') { | ||
| 110 | $result = $this->renderAll($block[2], $context); | ||
| 111 | |||
| 112 | break; | ||
| 113 | } | ||
| 114 | |||
| 115 |             if ($block[0] === 'if' || $block[0] === 'elseif') { | ||
| 116 | // Extract logical operators | ||
| 117 | $lexerLogical->matchAll($block[1]); | ||
| 118 | $operators = $lexerLogical->getArrayMatch(1); | ||
| 119 | // Extract individual conditions | ||
| 120 | $individualConditions = $lexerLogical->split($block[1]); | ||
| 121 | |||
| 122 | $conditions = []; | ||
| 123 |                 foreach ($individualConditions as $condition) { | ||
| 124 |                     if ($lexerConditional->match($condition)) { | ||
| 125 | $left = $lexerConditional->isMatchNotNull(1) | ||
| 126 | ? $lexerConditional->getStringMatch(1) | ||
| 127 | : null; | ||
| 128 | $operator = $lexerConditional->isMatchNotNull(2) | ||
| 129 | ? $lexerConditional->getStringMatch(2) | ||
| 130 | : null; | ||
| 131 | $right = $lexerConditional->isMatchNotNull(3) | ||
| 132 | ? $lexerConditional->getStringMatch(3) | ||
| 133 | : null; | ||
| 134 | |||
| 135 | array_push($conditions, [ | ||
| 136 | 'left' => $left, | ||
| 137 | 'operator' => $operator, | ||
| 138 | 'right' => $right, | ||
| 139 | ]); | ||
| 140 |                     } else { | ||
| 141 | throw new ParseException(sprintf( | ||
| 142 | 'Syntax Error in "%s" - Valid syntax: if [conditions]', | ||
| 143 | 'if' | ||
| 144 | )); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 |                 if (count($operators) > 0) { | ||
| 149 | // If statement contains and/or | ||
| 150 | $display = $this->evaluateCondition( | ||
| 151 | $conditions[0]['left'], | ||
| 152 | $conditions[0]['right'], | ||
| 153 | $conditions[0]['operator'], | ||
| 154 | $context | ||
| 155 | ); | ||
| 156 | |||
| 157 |                     foreach ($operators as $key => $operator) { | ||
| 158 |                         if ($operator === 'and') { | ||
| 159 | $display = ( | ||
| 160 | $display | ||
| 161 | && $this->evaluateCondition( | ||
| 162 | $conditions[$key + 1]['left'], | ||
| 163 | $conditions[$key + 1]['right'], | ||
| 164 | $conditions[$key + 1]['operator'], | ||
| 165 | $context | ||
| 166 | ) | ||
| 167 | ); | ||
| 168 |                         } else { | ||
| 169 | $display = ( | ||
| 170 | $display | ||
| 171 | || $this->evaluateCondition( | ||
| 172 | $conditions[$key + 1]['left'], | ||
| 173 | $conditions[$key + 1]['right'], | ||
| 174 | $conditions[$key + 1]['operator'], | ||
| 175 | $context | ||
| 176 | ) | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | } | ||
| 180 |                 } else { | ||
| 181 | // If statement is a single condition | ||
| 182 | $display = $this->evaluateCondition( | ||
| 183 | $conditions[0]['left'], | ||
| 184 | $conditions[0]['right'], | ||
| 185 | $conditions[0]['operator'], | ||
| 186 | $context | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | |||
| 190 | // hook for if not tag | ||
| 191 | $display = $this->negateCondition($display); | ||
| 192 | |||
| 193 |                 if ($display) { | ||
| 194 | $result = $this->renderAll($block[2], $context); | ||
| 195 | |||
| 196 | break; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | $context->pop(); | ||
| 202 | |||
| 203 | return $result; | ||
| 204 | } | ||
| 234 |