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 |
||
26 | class CompetitionTest extends UnitTestCase |
||
27 | { |
||
28 | //<editor-fold desc="Public Methods"> |
||
29 | /** |
||
30 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getLocalIdentifier |
||
31 | * @uses \Tfboe\FmLib\Entity\Helpers\NameEntity::getName |
||
32 | * @uses \Tfboe\FmLib\Entity\Helpers\NameEntity::setName |
||
33 | */ |
||
34 | public function testGetLocalIdentifier() |
||
40 | |||
41 | /** |
||
42 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::init |
||
43 | * @uses \Tfboe\FmLib\Entity\Traits\Competition::getPhases |
||
44 | * @uses \Tfboe\FmLib\Entity\Traits\Competition::getTeams |
||
45 | */ |
||
46 | public function testInit() |
||
55 | |||
56 | /** |
||
57 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getLevel() |
||
58 | */ |
||
59 | public function testLevel() |
||
63 | |||
64 | /** |
||
65 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getPhases |
||
66 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getChildren |
||
67 | * @uses \Tfboe\FmLib\Entity\Traits\Phase |
||
68 | * @uses \Tfboe\FmLib\Entity\Traits\Competition::init |
||
69 | * @uses \Tfboe\FmLib\Entity\Traits\Competition::init |
||
70 | */ |
||
71 | public function testPhasesAndChildren() |
||
84 | |||
85 | /** |
||
86 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getTeams |
||
87 | * @uses \Tfboe\FmLib\Entity\Traits\Competition::init |
||
88 | * @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
||
89 | * @uses \Tfboe\FmLib\Entity\Team |
||
90 | */ |
||
91 | public function testTeams() |
||
101 | |||
102 | /** |
||
103 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::setTournament() |
||
104 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getTournament() |
||
105 | * @covers \Tfboe\FmLib\Entity\Traits\Competition::getParent() |
||
106 | * @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
||
107 | * @uses \Tfboe\FmLib\Entity\Helpers\NameEntity::getName |
||
108 | * @uses \Tfboe\FmLib\Entity\Helpers\NameEntity::setName |
||
109 | */ |
||
110 | View Code Duplication | public function testTournamentAndParent() |
|
137 | //</editor-fold desc="Public Methods"> |
||
138 | |||
139 | //<editor-fold desc="Private Methods"> |
||
140 | /** |
||
141 | * @return Competition|MockObject a new competition |
||
142 | */ |
||
143 | private function competition(): MockObject |
||
147 | //</editor-fold desc="Private Methods"> |
||
148 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: