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 |
||
37 | class ClassMetadataFactoryTests extends TestCase |
||
38 | { |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $cacheDirectory; |
||
43 | |||
44 | /** |
||
45 | * @param Adapter $adapter |
||
46 | * @param Extractor $annotationExtractor |
||
47 | * @param Generator $asserterGenerator |
||
48 | * @param Manager $assertionManager |
||
49 | * @param \Closure $reflectionClassFactory |
||
50 | * @param \Closure $phpExtensionFactory |
||
51 | * @param Analyzer $analyzer |
||
52 | */ |
||
53 | View Code Duplication | public function __construct( |
|
74 | |||
75 | /** |
||
76 | * Create the cache directory. |
||
77 | */ |
||
78 | public function setUp() |
||
84 | |||
85 | /** |
||
86 | * Remove the cache directory. |
||
87 | */ |
||
88 | public function tearDown() |
||
92 | |||
93 | /** |
||
94 | * @return AnnotationDriver |
||
95 | */ |
||
96 | View Code Duplication | protected function createDriver() |
|
120 | |||
121 | /** |
||
122 | * @return FileCache |
||
123 | */ |
||
124 | protected function createCache() |
||
128 | |||
129 | /** |
||
130 | * @return ClassMetadataFactory |
||
131 | */ |
||
132 | protected function createFactory() |
||
136 | |||
137 | /** |
||
138 | * @return ClassMetadataFactory |
||
139 | */ |
||
140 | protected function createFactoryWithoutCache() |
||
144 | |||
145 | /** |
||
146 | * Test GetAllMetadata method. |
||
147 | */ |
||
148 | public function testGetAllMetadata() |
||
166 | |||
167 | /** |
||
168 | * Test GetMetadataFor method. |
||
169 | */ |
||
170 | View Code Duplication | public function testGetMetadataFor() |
|
171 | { |
||
172 | $this |
||
173 | ->given($factory = $this->createFactory()) |
||
174 | ->when($metadatas = $factory->getAllMetadata()) |
||
175 | ->then() |
||
176 | ->array($metadatas) |
||
177 | ->hasSize(2) |
||
178 | ->object($classMetadata = $factory->getMetadataFor(User::class)) |
||
179 | ->isInstanceOf(ClassMetadata::class) |
||
180 | ->array($classMetadata->propertiesMetadata()) |
||
181 | ->hasSize(7) |
||
182 | ->hasKey('id') |
||
183 | ->hasKey('name') |
||
184 | ->hasKey('username') |
||
185 | ->hasKey('age') |
||
186 | ->hasKey('email') |
||
187 | ->hasKey('addresses') |
||
188 | ->hasKey('friends') |
||
189 | ->object($propertyMetadata = $classMetadata->propertyMetadata('id')) |
||
190 | ->isNotNull() |
||
191 | ->boolean($propertyMetadata->getMetadata('identifier')) |
||
192 | ->isTrue() |
||
193 | ->string($propertyMetadata->getMetadata('name')) |
||
194 | ->isEqualTo('_id') |
||
195 | ->string($classMetadata->propertyMetadata('name')->getMetadata('name')) |
||
196 | ->isEqualTo('fullName') |
||
197 | ->string($classMetadata->propertyMetadata('addresses')->getMetadata('type')) |
||
198 | ->isEqualTo('ArraySet') |
||
199 | ->string($classMetadata->propertyMetadata('addresses')->getMetadata('of')) |
||
200 | ->isEqualTo('Cubiche\Core\Metadata\Tests\Fixtures\Address') |
||
201 | ; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Test HasMetadataFor method. |
||
206 | */ |
||
207 | public function testHasMetadataFor() |
||
217 | |||
218 | /** |
||
219 | * Test SetMetadataFor method. |
||
220 | */ |
||
221 | public function testSetMetadataFor() |
||
235 | } |
||
236 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.