Conditions | 1 |
Paths | 1 |
Total Lines | 52 |
Code Lines | 34 |
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 |
||
115 | public function stopWordProvider() { |
||
116 | |||
117 | $provider[] = array( |
||
118 | 'en', |
||
119 | 'Foo', |
||
120 | false |
||
121 | ); |
||
122 | |||
123 | $provider[] = array( |
||
124 | 'en', |
||
125 | 'the', |
||
126 | true |
||
127 | ); |
||
128 | |||
129 | $provider[] = array( |
||
130 | 'ja', |
||
131 | 'それぞれ', |
||
132 | true |
||
133 | ); |
||
134 | |||
135 | $provider[] = array( |
||
136 | 'zh', |
||
137 | '不单', |
||
138 | true |
||
139 | ); |
||
140 | |||
141 | $provider[] = array( |
||
142 | 'es', |
||
143 | 'arriba', |
||
144 | true |
||
145 | ); |
||
146 | |||
147 | $provider[] = array( |
||
148 | 'fr', |
||
149 | 'devrait', |
||
150 | true |
||
151 | ); |
||
152 | |||
153 | $provider[] = array( |
||
154 | 'pt', |
||
155 | 'conhecido', |
||
156 | true |
||
157 | ); |
||
158 | |||
159 | $provider[] = array( |
||
160 | 'pt-br', |
||
161 | 'mediante', |
||
162 | true |
||
163 | ); |
||
164 | |||
165 | return $provider; |
||
166 | } |
||
167 | |||
169 |
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.