Total Complexity | 40 |
Total Lines | 415 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RelationsGeneratorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RelationsGeneratorTest, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
15 | class RelationsGeneratorTest extends AbstractTest |
||
16 | { |
||
17 | public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/RelationsGeneratorTest/'; |
||
18 | |||
19 | public const TEST_PROJECT_ROOT_NAMESPACE = parent::TEST_PROJECT_ROOT_NAMESPACE |
||
20 | . '\\RelationsGeneratorTest'; |
||
21 | |||
22 | public const TEST_ENTITY_BASKET = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
23 | . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Basket'; |
||
24 | |||
25 | public const TEST_ENTITY_BASKET_ITEM = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
26 | . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Basket\\Item'; |
||
27 | |||
28 | public const TEST_ENTITY_BASKET_ITEM_OFFER = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
29 | . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Basket\\Item\\Offer'; |
||
30 | |||
31 | public const TEST_ENTITY_NESTED_THING = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
32 | . AbstractGenerator::ENTITIES_FOLDER_NAME |
||
33 | . '\\GeneratedRelations\\Testing\\RelationsTestEntity'; |
||
34 | |||
35 | public const TEST_ENTITY_NESTED_THING2 = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
36 | . AbstractGenerator::ENTITIES_FOLDER_NAME |
||
37 | . '\\GeneratedRelations\\ExtraTesting\\Test\\AnotherRelationsTestEntity'; |
||
38 | |||
39 | public const TEST_ENTITY_NAMESPACING_COMPANY = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
40 | . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Company'; |
||
41 | |||
42 | public const TEST_ENTITY_NAMESPACING_SOME_CLIENT = self::TEST_PROJECT_ROOT_NAMESPACE . '\\' |
||
43 | . AbstractGenerator::ENTITIES_FOLDER_NAME . '\\Some\\Client'; |
||
44 | |||
45 | public const TEST_ENTITY_NAMESPACING_ANOTHER_CLIENT = self::TEST_PROJECT_ROOT_NAMESPACE . |
||
46 | '\\' |
||
47 | . |
||
48 | AbstractGenerator::ENTITIES_FOLDER_NAME . |
||
49 | '\\Another\\Client'; |
||
50 | |||
51 | public const TEST_ENTITIES = [ |
||
52 | self::TEST_ENTITY_BASKET, |
||
53 | self::TEST_ENTITY_BASKET_ITEM, |
||
54 | self::TEST_ENTITY_BASKET_ITEM_OFFER, |
||
55 | self::TEST_ENTITY_NESTED_THING, |
||
56 | self::TEST_ENTITY_NESTED_THING2, |
||
57 | ]; |
||
58 | |||
59 | public const TEST_ENTITIES_NAMESPACING = [ |
||
60 | self::TEST_ENTITY_NAMESPACING_COMPANY, |
||
61 | self::TEST_ENTITY_NAMESPACING_SOME_CLIENT, |
||
62 | self::TEST_ENTITY_NAMESPACING_ANOTHER_CLIENT, |
||
63 | ]; |
||
64 | protected static $buildOnce = true; |
||
65 | protected static $built = false; |
||
66 | /** |
||
67 | * @var EntityGenerator |
||
68 | */ |
||
69 | private $entityGenerator; |
||
70 | /** |
||
71 | * @var RelationsGenerator |
||
72 | */ |
||
73 | private $relationsGenerator; |
||
74 | /** |
||
75 | * @var \ts\Reflection\ReflectionClass |
||
76 | */ |
||
77 | private $reflection; |
||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | private $copiedExtraSuffix = ''; |
||
82 | |||
83 | /** |
||
84 | * @test |
||
85 | * @large |
||
86 | * @coversNothing |
||
87 | */ |
||
88 | public function allHasTypesInConstantArrays(): void |
||
89 | { |
||
90 | $hasTypes = []; |
||
91 | $constants = $this->getReflection()->getConstants(); |
||
92 | foreach ($constants as $constantName => $constantValue) { |
||
93 | if (0 === strpos($constantName, 'HAS') && false === strpos($constantName, 'HAS_TYPES')) { |
||
94 | $hasTypes[$constantName] = $constantValue; |
||
95 | } |
||
96 | } |
||
97 | $hasTypesCounted = count($hasTypes); |
||
98 | $hasTypesDefinedInConstantArray = count(RelationsGenerator::HAS_TYPES); |
||
99 | $fullDiff = function (array $arrayX, array $arrayY): array { |
||
100 | $intersect = array_intersect($arrayX, $arrayY); |
||
101 | |||
102 | return array_merge(array_diff($arrayX, $intersect), array_diff($arrayY, $intersect)); |
||
103 | }; |
||
104 | self::assertSame( |
||
105 | $hasTypesCounted, |
||
106 | $hasTypesDefinedInConstantArray, |
||
107 | 'The number of defined in the constant array RelationsGenerator::HAS_TYPES is not correct:' |
||
108 | . " \n\nfull diff:\n " |
||
109 | . print_r($fullDiff($hasTypes, RelationsGenerator::HAS_TYPES), true) |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return \ts\Reflection\ReflectionClass |
||
115 | * @throws \ReflectionException |
||
116 | */ |
||
117 | private function getReflection(): \ts\Reflection\ReflectionClass |
||
118 | { |
||
119 | if (null === $this->reflection) { |
||
120 | $this->reflection = new \ts\Reflection\ReflectionClass(RelationsGenerator::class); |
||
121 | } |
||
122 | |||
123 | return $this->reflection; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @test |
||
128 | * @large |
||
129 | * @covers ::generateRelationCodeForEntity |
||
130 | * @throws \ReflectionException |
||
131 | */ |
||
132 | public function generateRelations(): void |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @test |
||
174 | * @large |
||
175 | * @covers ::setEntityHasRelationToEntity |
||
176 | * @throws \ReflectionException |
||
177 | * @throws DoctrineStaticMetaException |
||
178 | */ |
||
179 | public function setRelationsBetweenEntities(): void |
||
244 | } |
||
245 | |||
246 | public function setUp() |
||
247 | { |
||
248 | parent::setUp(); |
||
249 | $this->entityGenerator = $this->getEntityGenerator(); |
||
250 | $this->relationsGenerator = $this->getRelationsGenerator(); |
||
251 | if (false === self::$built) { |
||
252 | foreach (self::TEST_ENTITIES as $fqn) { |
||
253 | $this->entityGenerator->generateEntity($fqn); |
||
254 | $this->relationsGenerator->generateRelationCodeForEntity($fqn); |
||
255 | } |
||
256 | self::$built = true; |
||
257 | } |
||
258 | $this->setupCopiedWorkDir(); |
||
259 | $this->relationsGenerator->setPathToProjectRoot($this->copiedWorkDir) |
||
260 | ->setProjectRootNamespace($this->copiedRootNamespace); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Inspect the generated class and ensure that all required interfaces have been implemented |
||
265 | * |
||
266 | * @param string $owningEntityFqn |
||
267 | * @param string $hasType |
||
268 | * @param string $ownedEntityFqn |
||
269 | * @param bool $assertInverse |
||
270 | * |
||
271 | * @return void |
||
272 | * @throws \ReflectionException |
||
273 | * @SuppressWarnings(PHPMD) |
||
274 | */ |
||
275 | private function assertCorrectInterfacesSet( |
||
307 | ); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Get an array of all the interfaces for a class |
||
313 | * |
||
314 | * @param string $classFqn |
||
315 | * |
||
316 | * @return array |
||
317 | * @throws \ReflectionException |
||
318 | */ |
||
319 | private function getOwningEntityInterfaces(string $classFqn): array |
||
320 | { |
||
321 | $owningReflection = new \ts\Reflection\ReflectionClass($classFqn); |
||
322 | |||
323 | return $this->getImplementedInterfacesFromClassFile($owningReflection->getFileName()); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Get a an array of interfaces (short names) by reading the PHP file itself |
||
328 | * |
||
329 | * @param string $classFilePath |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | private function getImplementedInterfacesFromClassFile(string $classFilePath): array |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Get an array of the interfaces we expect an entity to implement based on the has type |
||
357 | * |
||
358 | * @param string $entityFqn |
||
359 | * @param string $hasType |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | private function getExpectedInterfacesForEntityFqn(string $entityFqn, string $hasType): array |
||
364 | { |
||
365 | $expectedInterfaces = []; |
||
366 | $expectedInterfaces[] = \in_array($hasType, RelationsGenerator::HAS_TYPES_PLURAL, true) |
||
367 | ? 'Has' . \ucwords($entityFqn::getDoctrineStaticMeta()->getPlural()) . 'Interface' |
||
368 | : 'Has' . \ucwords($entityFqn::getDoctrineStaticMeta()->getSingular()) . 'Interface'; |
||
369 | if (!\in_array($hasType, RelationsGenerator::HAS_TYPES_UNIDIRECTIONAL, true) |
||
370 | || \in_array($hasType, RelationsGenerator::HAS_TYPES_RECIPROCATED, true) |
||
371 | ) { |
||
372 | $expectedInterfaces[] = 'Reciprocates' . \ucwords($entityFqn::getDoctrineStaticMeta()->getSingular()) |
||
373 | . 'Interface'; |
||
374 | } |
||
375 | |||
376 | return $expectedInterfaces; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Get the inverse has type name, or false if there is no inverse has type |
||
381 | * |
||
382 | * @param string $hasType |
||
383 | * |
||
384 | * @return bool|mixed|null|string |
||
385 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
386 | */ |
||
387 | private function getInverseHasType(string $hasType) |
||
425 | } |
||
426 | |||
427 | protected function getCopiedNamespaceRoot(): string |
||
430 | } |
||
431 | } |
||
432 |