Conditions | 1 |
Paths | 1 |
Total Lines | 62 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
112 | public function resultProvider() { |
||
113 | |||
114 | #0 |
||
115 | $provider[] = array( |
||
|
|||
116 | array( 'query' => array() ), |
||
117 | array(), |
||
118 | false, |
||
119 | null |
||
120 | ); |
||
121 | |||
122 | #1 |
||
123 | $provider[] = array( |
||
124 | array( |
||
125 | 'query-continue-offset' => 3, |
||
126 | 'query' => array() |
||
127 | ), |
||
128 | array(), |
||
129 | true, |
||
130 | null |
||
131 | ); |
||
132 | |||
133 | #2 |
||
134 | $provider[] = array( |
||
135 | array( |
||
136 | 'query-continue-offset' => 3, |
||
137 | 'query' => array( |
||
138 | 'printrequests' => array( |
||
139 | array( 'label' => 'Category', 'mode' => 0 ) |
||
140 | ) |
||
141 | ) |
||
142 | ), |
||
143 | array( |
||
144 | 'printrequests' => array( |
||
145 | array( 'label' => 'Category', 'mode' => 0 ) |
||
146 | ) |
||
147 | ), |
||
148 | true, |
||
149 | new DIProperty( '_INST' ) |
||
150 | ); |
||
151 | |||
152 | #3 |
||
153 | $provider[] = array( |
||
154 | array( |
||
155 | 'query' => array( |
||
156 | 'printrequests' => array( |
||
157 | array( 'label' => 'Category', 'mode' => 0 ) |
||
158 | ), |
||
159 | 'results' => array() |
||
160 | ), |
||
161 | ), |
||
162 | array( |
||
163 | 'printrequests' => array( |
||
164 | array( 'label' => 'Category', 'mode' => 0 ) |
||
165 | ), |
||
166 | 'results' => array() |
||
167 | ), |
||
168 | false, |
||
169 | new DIProperty( '_INST' ) |
||
170 | ); |
||
171 | |||
172 | return $provider; |
||
173 | } |
||
174 | |||
176 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.