| Conditions | 29 |
| Paths | 29 |
| Total Lines | 254 |
| Code Lines | 191 |
| 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 |
||
| 109 | public function onFinishProduction(Node $node): void |
||
| 110 | { |
||
| 111 | if ($this->hasReference($node)) { |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | switch ($node->getName()) { |
||
| 115 | case QueryAstNodeType::GET_INPUT: |
||
| 116 | $this->addMethodCall( |
||
| 117 | $node, |
||
| 118 | 'getInput', |
||
| 119 | $this->input |
||
| 120 | ); |
||
| 121 | break; |
||
| 122 | |||
| 123 | case QueryAstNodeType::SET_OUTPUT: |
||
| 124 | $this->isDefinite = $node->getAttribute('is_definite'); |
||
| 125 | $this->stmts[] = new Return_($this->getReference($node->getChild(0))); |
||
| 126 | break; |
||
| 127 | |||
| 128 | case QueryAstNodeType::CREATE_FILTER_CONTEXT: |
||
| 129 | $this->addMethodCall( |
||
| 130 | $node, |
||
| 131 | 'createFilterContext', |
||
| 132 | $this->getReference($node->getChild(0)) |
||
| 133 | ); |
||
| 134 | break; |
||
| 135 | |||
| 136 | case QueryAstNodeType::SPLIT: |
||
| 137 | $this->addMethodCall( |
||
| 138 | $node, |
||
| 139 | 'split', |
||
| 140 | $this->getReference($node->getChild(0)) |
||
| 141 | ); |
||
| 142 | break; |
||
| 143 | |||
| 144 | case QueryAstNodeType::EVALUATE: |
||
| 145 | $this->addMethodCall( |
||
| 146 | $node, |
||
| 147 | 'evaluate', |
||
| 148 | $this->getReference($node->getChild(0)), |
||
| 149 | $this->getReference($node->getChild(1)) |
||
| 150 | ); |
||
| 151 | break; |
||
| 152 | |||
| 153 | case QueryAstNodeType::FILTER: |
||
| 154 | $this->addMethodCall( |
||
| 155 | $node, |
||
| 156 | 'filter', |
||
| 157 | $this->getReference($node->getChild(0)), |
||
| 158 | $this->getReference($node->getChild(1)) |
||
| 159 | ); |
||
| 160 | break; |
||
| 161 | |||
| 162 | case QueryAstNodeType::EVALUATE_LOGICAL_OR: |
||
| 163 | $this->addMethodCall( |
||
| 164 | $node, |
||
| 165 | 'evaluateLogicalOr', |
||
| 166 | $this->getReference($node->getChild(0)), |
||
| 167 | $this->getReference($node->getChild(1)) |
||
| 168 | ); |
||
| 169 | break; |
||
| 170 | |||
| 171 | case QueryAstNodeType::EVALUATE_LOGICAL_AND: |
||
| 172 | $this->addMethodCall( |
||
| 173 | $node, |
||
| 174 | 'evaluateLogicalAnd', |
||
| 175 | $this->getReference($node->getChild(0)), |
||
| 176 | $this->getReference($node->getChild(1)) |
||
| 177 | ); |
||
| 178 | break; |
||
| 179 | |||
| 180 | case QueryAstNodeType::EVALUATE_LOGICAL_NOT: |
||
| 181 | $this->addMethodCall( |
||
| 182 | $node, |
||
| 183 | 'evaluateLogicalNot', |
||
| 184 | $this->getReference($node->getChild(0)) |
||
| 185 | ); |
||
| 186 | break; |
||
| 187 | |||
| 188 | case QueryAstNodeType::CALCULATE_IS_EQUAL: |
||
| 189 | $this->addMethodCall( |
||
| 190 | $node, |
||
| 191 | 'calculateIsEqual', |
||
| 192 | $this->getReference($node->getChild(0)), |
||
| 193 | $this->getReference($node->getChild(1)) |
||
| 194 | ); |
||
| 195 | break; |
||
| 196 | |||
| 197 | case QueryAstNodeType::CALCULATE_IS_GREATER: |
||
| 198 | $this->addMethodCall( |
||
| 199 | $node, |
||
| 200 | 'calculateIsGreater', |
||
| 201 | $this->getReference($node->getChild(0)), |
||
| 202 | $this->getReference($node->getChild(1)) |
||
| 203 | ); |
||
| 204 | break; |
||
| 205 | |||
| 206 | case QueryAstNodeType::CALCULATE_IS_REGEXP: |
||
| 207 | $this->addMethodCall( |
||
| 208 | $node, |
||
| 209 | 'calculateIsRegExp', |
||
| 210 | $this->php->val($node->getAttribute('pattern')), |
||
| 211 | $this->getReference($node->getChild(0)) |
||
| 212 | ); |
||
| 213 | break; |
||
| 214 | |||
| 215 | case QueryAstNodeType::FETCH_CHILDREN: |
||
| 216 | $this->addMethodCall( |
||
| 217 | $node, |
||
| 218 | 'fetchChildren', |
||
| 219 | $this->getReference($node->getChild(0)), |
||
| 220 | new Arg( |
||
| 221 | $this->getReference($node->getChild(1)), |
||
| 222 | false, |
||
| 223 | true |
||
| 224 | ) |
||
| 225 | ); |
||
| 226 | break; |
||
| 227 | |||
| 228 | case QueryAstNodeType::FETCH_CHILDREN_DEEP: |
||
| 229 | $this->addMethodCall( |
||
| 230 | $node, |
||
| 231 | 'fetchChildrenDeep', |
||
| 232 | $this->getReference($node->getChild(0)), |
||
| 233 | new Arg( |
||
| 234 | $this->getReference($node->getChild(1)), |
||
| 235 | false, |
||
| 236 | true |
||
| 237 | ) |
||
| 238 | ); |
||
| 239 | break; |
||
| 240 | |||
| 241 | case QueryAstNodeType::MATCH_ANY_CHILD: |
||
| 242 | $this->addMethodCall( |
||
| 243 | $node, |
||
| 244 | 'matchAnyChild', |
||
| 245 | $this->getReference($node->getChild(0)) |
||
| 246 | ); |
||
| 247 | break; |
||
| 248 | |||
| 249 | case QueryAstNodeType::MATCH_PROPERTY_STRICTLY: |
||
| 250 | $this->addMethodCall( |
||
| 251 | $node, |
||
| 252 | 'matchPropertyStrictly', |
||
| 253 | $this->getReference($node->getChild(0)), |
||
| 254 | ); |
||
| 255 | break; |
||
| 256 | |||
| 257 | case QueryAstNodeType::MATCH_ELEMENT_STRICTLY: |
||
| 258 | $this->addMethodCall( |
||
| 259 | $node, |
||
| 260 | 'matchElementStrictly', |
||
| 261 | $this->getReference($node->getChild(0)), |
||
| 262 | ); |
||
| 263 | break; |
||
| 264 | |||
| 265 | case QueryAstNodeType::AGGREGATE: |
||
| 266 | $this->addMethodCall( |
||
| 267 | $node, |
||
| 268 | 'aggregate', |
||
| 269 | $this->php->val($node->getAttribute('name')), |
||
| 270 | $this->getReference($node->getChild(0)), |
||
| 271 | ); |
||
| 272 | break; |
||
| 273 | |||
| 274 | case QueryAstNodeType::POPULATE_LITERAL: |
||
| 275 | $this->addMethodCall( |
||
| 276 | $node, |
||
| 277 | 'populateLiteral', |
||
| 278 | $this->getReference($node->getChild(0)), |
||
| 279 | $this->getReference($node->getChild(1)) |
||
| 280 | ); |
||
| 281 | break; |
||
| 282 | |||
| 283 | case QueryAstNodeType::POPULATE_LITERAL_ARRAY: |
||
| 284 | $this->addMethodCall( |
||
| 285 | $node, |
||
| 286 | 'populateLiteralArray', |
||
| 287 | $this->getReference($node->getChild(0)), |
||
| 288 | new Arg( |
||
| 289 | $this->getReference($node->getChild(1)), |
||
| 290 | false, |
||
| 291 | true |
||
| 292 | ) |
||
| 293 | ); |
||
| 294 | break; |
||
| 295 | |||
| 296 | case QueryAstNodeType::POPULATE_INDEX_LIST: |
||
| 297 | $this->addMethodCall( |
||
| 298 | $node, |
||
| 299 | 'populateIndexList', |
||
| 300 | $this->getReference($node->getChild(0)), |
||
| 301 | ...array_map( |
||
| 302 | [$this->php, 'val'], |
||
| 303 | $node->getAttribute('indexList') |
||
| 304 | ) |
||
| 305 | ); |
||
| 306 | break; |
||
| 307 | |||
| 308 | case QueryAstNodeType::POPULATE_INDEX_SLICE: |
||
| 309 | $attributes = $node->getAttributeList(); |
||
| 310 | $this->addMethodCall( |
||
| 311 | $node, |
||
| 312 | 'populateIndexSlice', |
||
| 313 | $this->getReference($node->getChild(0)), |
||
| 314 | $this->php->val($attributes['start'] ?? null), |
||
| 315 | $this->php->val($attributes['end'] ?? null), |
||
| 316 | $this->php->val($attributes['step'] ?? null) |
||
| 317 | ); |
||
| 318 | |||
| 319 | break; |
||
| 320 | |||
| 321 | case QueryAstNodeType::POPULATE_NAME_LIST: |
||
| 322 | $this->addMethodCall( |
||
| 323 | $node, |
||
| 324 | 'populateNameList', |
||
| 325 | $this->getReference($node->getChild(0)), |
||
| 326 | ...array_map( |
||
| 327 | [$this->php, 'val'], |
||
| 328 | $node->getAttribute('nameList') |
||
| 329 | ) |
||
| 330 | ); |
||
| 331 | break; |
||
| 332 | |||
| 333 | case QueryAstNodeType::CREATE_SCALAR: |
||
| 334 | $attributes = $node->getAttributeList(); |
||
| 335 | // TODO: allow accessing null attributes |
||
| 336 | $value = $this->php->val($attributes['value'] ?? null); |
||
| 337 | $this->addMethodCall( |
||
| 338 | $node, |
||
| 339 | 'createScalar', |
||
| 340 | $value |
||
| 341 | ); |
||
| 342 | break; |
||
| 343 | |||
| 344 | case QueryAstNodeType::CREATE_ARRAY: |
||
| 345 | // [ X:APPEND_TO_ARRAY ] |
||
| 346 | $items = []; |
||
| 347 | foreach ($node->getChildList() as $child) { |
||
| 348 | $items[] = $this->getReference($child); |
||
| 349 | } |
||
| 350 | $this->stmts[] = new Assign( |
||
| 351 | $this->createReference($node), |
||
| 352 | $this->php->val($items) |
||
| 353 | ); |
||
| 354 | break; |
||
| 355 | |||
| 356 | case QueryAstNodeType::APPEND_TO_ARRAY: |
||
| 357 | // [ 0:<value> ] |
||
| 358 | $this->setReference( |
||
| 359 | $node, |
||
| 360 | $this->getReference($node->getChild(0)) |
||
| 361 | ); |
||
| 362 | break; |
||
| 363 | } |
||
| 410 |