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