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