Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 15 | class ManyToOneOrphanRemovalTest extends OrmFunctionalTestCase |
||
| 16 | { |
||
| 17 | private $personId; |
||
| 18 | |||
| 19 | protected static $_modelSets = [ |
||
| 20 | 'ornemental_orphan_removal' => [ |
||
| 21 | Person::class, |
||
| 22 | PhoneNumber::class, |
||
| 23 | ] |
||
| 24 | ]; |
||
| 25 | |||
| 26 | View Code Duplication | protected function setUp() |
|
| 27 | { |
||
| 28 | $this->useModelSet('ornemental_orphan_removal'); |
||
| 29 | |||
| 30 | parent::setUp(); |
||
| 31 | |||
| 32 | $person = new Person; |
||
| 33 | $person->id = 'ca41a293-799f-4d68-bf79-626c3ad223ec'; |
||
| 34 | |||
| 35 | $phone1 = new PhoneNumber; |
||
| 36 | $phone1->id = 'f4132478-c492-4dfe-aab5-a5b79ae129e7'; |
||
| 37 | $phone1->phonenumber = '123456'; |
||
| 38 | |||
| 39 | $phone2 = new PhoneNumber; |
||
| 40 | $phone2->id = '7faa4cd3-a155-4fbf-bc42-aa4269a4454d'; |
||
| 41 | $phone2->phonenumber = '234567'; |
||
| 42 | |||
| 43 | $phone1->person = $person; |
||
| 44 | $phone2->person = $person; |
||
| 45 | |||
| 46 | $this->_em->persist($phone1); |
||
| 47 | $this->_em->persist($phone2); |
||
| 48 | $this->_em->persist($person); |
||
| 49 | $this->_em->flush(); |
||
| 50 | |||
| 51 | $this->personId = $person->id; |
||
| 52 | $this->_em->clear(); |
||
| 53 | } |
||
| 54 | |||
| 55 | View Code Duplication | public function testOrphanRemovalIsPurelyOrnemental() |
|
| 77 | |||
| 78 | protected function _getEntityManager( |
||
| 86 | } |
||
| 87 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.