Conditions | 1 |
Paths | 1 |
Total Lines | 80 |
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 |
||
167 | public function testAcquireTermIdsReusesExistingTerms() { |
||
168 | $termsArray = [ |
||
169 | 'label' => [ |
||
170 | 'en' => 'same', |
||
171 | 'de' => 'same', |
||
172 | ], |
||
173 | 'description' => [ 'en' => 'same' ], |
||
174 | 'alias' => [ |
||
175 | 'en' => [ 'same', 'same', 'another', 'yet another' ] |
||
176 | ] |
||
177 | ]; |
||
178 | |||
179 | // We will populate DB with two terms that both have |
||
180 | // text "same". One is of type "label" in language "en", |
||
181 | // and the other is of type "alias" in language "en. |
||
182 | // |
||
183 | // TermIdsAcquirer should then reuse those terms for the given |
||
184 | // termsArray above, meaning thoese pre-inserted terms will |
||
185 | // appear (their ids) in the returned array from |
||
186 | // TermIdsAcquirer::acquireTermIds( $termsArray ) |
||
187 | $typeIdsAcquirer = new InMemoryTypeIdsStore(); |
||
188 | $alreadyAcquiredTypeIds = $typeIdsAcquirer->acquireTypeIds( |
||
189 | [ 'label', 'description', 'alias' ] |
||
190 | ); |
||
191 | |||
192 | $this->db->insert( 'wbt_text', [ 'wbx_text' => 'same' ] ); |
||
193 | $sameTextId = $this->db->insertId(); |
||
194 | |||
195 | $this->db->insert( |
||
196 | 'wbt_text_in_lang', |
||
197 | [ 'wbxl_text_id' => $sameTextId, 'wbxl_language' => 'en' ] |
||
198 | ); |
||
199 | $enSameTextInLangId = $this->db->insertId(); |
||
200 | |||
201 | $this->db->insert( |
||
202 | 'wbt_term_in_lang', |
||
203 | [ 'wbtl_text_in_lang_id' => $enSameTextInLangId, |
||
204 | 'wbtl_type_id' => $alreadyAcquiredTypeIds['label'] ] |
||
205 | ); |
||
206 | $labelEnSameTermInLangId = (string)$this->db->insertId(); |
||
207 | |||
208 | $this->db->insert( |
||
209 | 'wbt_term_in_lang', |
||
210 | [ 'wbtl_text_in_lang_id' => $enSameTextInLangId, |
||
211 | 'wbtl_type_id' => $alreadyAcquiredTypeIds['alias'] ] |
||
212 | ); |
||
213 | $aliasEnSameTermInLangId = (string)$this->db->insertId(); |
||
214 | |||
215 | $dbTermIdsAcquirer = new DatabaseTermIdsAcquirer( |
||
216 | $this->loadBalancer, |
||
217 | $typeIdsAcquirer |
||
218 | ); |
||
219 | |||
220 | $acquiredTermIds = $dbTermIdsAcquirer->acquireTermIds( $termsArray ); |
||
221 | |||
222 | $this->assertCount( 7, $acquiredTermIds ); |
||
223 | |||
224 | // We will assert that the returned ids of acquired terms contains |
||
225 | // one occurence of the term id for en label "same" that already existed in db, |
||
226 | // and two occurences of the term id for en alias "same" that already existed |
||
227 | // in db. |
||
228 | $this->assertCount( |
||
229 | 1, |
||
230 | array_filter( |
||
231 | $acquiredTermIds, |
||
232 | function ( $id ) use ( $labelEnSameTermInLangId ) { |
||
233 | return $id === $labelEnSameTermInLangId; |
||
234 | } |
||
235 | ) |
||
236 | ); |
||
237 | $this->assertCount( |
||
238 | 2, |
||
239 | array_filter( |
||
240 | $acquiredTermIds, |
||
241 | function ( $id ) use ( $aliasEnSameTermInLangId ) { |
||
242 | return $id === $aliasEnSameTermInLangId; |
||
243 | } |
||
244 | ) |
||
245 | ); |
||
246 | } |
||
247 | |||
287 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.