| Conditions | 1 |
| Paths | 1 |
| Total Lines | 53 |
| 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 |
||
| 65 | public function sampleQueryProvider() { |
||
| 66 | |||
| 67 | $provider['Standard query'] = [ |
||
|
|
|||
| 68 | [ |
||
| 69 | '[[Modification date::+]];?Modification date;limit=10', |
||
| 70 | ], |
||
| 71 | [ |
||
| 72 | [ |
||
| 73 | 'label'=> '', |
||
| 74 | 'typeid' => '_wpg', |
||
| 75 | 'mode' => 2, |
||
| 76 | 'format' => false, |
||
| 77 | 'key' => '', |
||
| 78 | 'redi' => '' |
||
| 79 | ], |
||
| 80 | [ |
||
| 81 | 'label'=> 'Modification date', |
||
| 82 | 'typeid' => '_dat', |
||
| 83 | 'mode' => 1, |
||
| 84 | 'format' => '', |
||
| 85 | 'key' => '_MDAT', |
||
| 86 | 'redi' => '' |
||
| 87 | ] |
||
| 88 | ] |
||
| 89 | ]; |
||
| 90 | |||
| 91 | $provider['Compound query'] = [ |
||
| 92 | [ |
||
| 93 | '[[Modification date::+]];?Modification date;limit=10', |
||
| 94 | '[[Modification date::+]];?Modification date' |
||
| 95 | ], |
||
| 96 | [ |
||
| 97 | [ |
||
| 98 | 'label'=> '', |
||
| 99 | 'typeid' => '_wpg', |
||
| 100 | 'mode' => 2, |
||
| 101 | 'format' => false, |
||
| 102 | 'key' => '', |
||
| 103 | 'redi' => '' |
||
| 104 | ], |
||
| 105 | [ |
||
| 106 | 'label'=> 'Modification date', |
||
| 107 | 'typeid' => '_dat', |
||
| 108 | 'mode' => 1, |
||
| 109 | 'format' => '', |
||
| 110 | 'key' => '_MDAT', |
||
| 111 | 'redi' => '' |
||
| 112 | ] |
||
| 113 | ] |
||
| 114 | ]; |
||
| 115 | |||
| 116 | return $provider; |
||
| 117 | } |
||
| 118 | |||
| 120 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.