| Total Complexity | 40 |
| Total Lines | 391 |
| 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); |
||
| 10 | class RelationsGeneratorTest extends AbstractTest |
||
| 11 | { |
||
| 12 | public const WORK_DIR = AbstractTest::VAR_PATH.'/RelationsGeneratorTest/'; |
||
| 13 | |||
| 14 | public const TEST_ENTITY_BASKET = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 15 | .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Basket'; |
||
| 16 | |||
| 17 | public const TEST_ENTITY_BASKET_ITEM = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 18 | .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Basket\\Item'; |
||
| 19 | |||
| 20 | public const TEST_ENTITY_BASKET_ITEM_OFFER = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 21 | .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Basket\\Item\\Offer'; |
||
| 22 | |||
| 23 | public const TEST_ENTITY_NESTED_THING = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 24 | .AbstractGenerator::ENTITIES_FOLDER_NAME |
||
| 25 | .'\\GeneratedRelations\\Testing\\RelationsTestEntity'; |
||
| 26 | |||
| 27 | public const TEST_ENTITY_NESTED_THING2 = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 28 | .AbstractGenerator::ENTITIES_FOLDER_NAME |
||
| 29 | .'\\GeneratedRelations\\ExtraTesting\\Test\\AnotherRelationsTestEntity'; |
||
| 30 | |||
| 31 | public const TEST_ENTITY_NAMESPACING_COMPANY = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 32 | .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Company'; |
||
| 33 | |||
| 34 | public const TEST_ENTITY_NAMESPACING_SOME_CLIENT = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 35 | .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Some\\Client'; |
||
| 36 | |||
| 37 | public const TEST_ENTITY_NAMESPACING_ANOTHER_CLIENT = self::TEST_PROJECT_ROOT_NAMESPACE.'\\' |
||
| 38 | .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Another\\Client'; |
||
| 39 | |||
| 40 | public const TEST_ENTITIES = [ |
||
| 41 | self::TEST_ENTITY_BASKET, |
||
| 42 | self::TEST_ENTITY_BASKET_ITEM, |
||
| 43 | self::TEST_ENTITY_BASKET_ITEM_OFFER, |
||
| 44 | self::TEST_ENTITY_NESTED_THING, |
||
| 45 | self::TEST_ENTITY_NESTED_THING2, |
||
| 46 | ]; |
||
| 47 | |||
| 48 | public const TEST_ENTITIES_NAMESPACING = [ |
||
| 49 | self::TEST_ENTITY_NAMESPACING_COMPANY, |
||
| 50 | self::TEST_ENTITY_NAMESPACING_SOME_CLIENT, |
||
| 51 | self::TEST_ENTITY_NAMESPACING_ANOTHER_CLIENT, |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var EntityGenerator |
||
| 56 | */ |
||
| 57 | protected $entityGenerator; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var RelationsGenerator |
||
| 61 | */ |
||
| 62 | protected $relationsGenerator; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \ReflectionClass |
||
| 66 | */ |
||
| 67 | protected $reflection; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Schema |
||
| 71 | */ |
||
| 72 | protected $schema; |
||
| 73 | |||
| 74 | /** |
||
| 75 | */ |
||
| 76 | public function testAllHasTypesInConstantArrays() |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return \ReflectionClass |
||
| 104 | * @throws \ReflectionException |
||
| 105 | */ |
||
| 106 | protected function getReflection(): \ReflectionClass |
||
| 107 | { |
||
| 108 | if (null === $this->reflection) { |
||
| 109 | $this->reflection = new \ReflectionClass(RelationsGenerator::class); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this->reflection; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @throws \ReflectionException |
||
| 117 | */ |
||
| 118 | public function testGenerateRelations() |
||
| 119 | { |
||
| 120 | /** |
||
| 121 | * @var \SplFileInfo $i |
||
| 122 | */ |
||
| 123 | foreach (self::TEST_ENTITIES as $entityFqn) { |
||
| 124 | foreach ($this->relationsGenerator->getRelativePathRelationsGenerator() as $relativePath => $i) { |
||
| 125 | if ($i->isDir()) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | $entityRefl = new \ReflectionClass($entityFqn); |
||
| 129 | $namespace = $entityRefl->getNamespaceName(); |
||
| 130 | $className = $entityRefl->getShortName(); |
||
| 131 | $namespaceNoEntities = substr($namespace, strpos( |
||
| 132 | $namespace, |
||
| 133 | AbstractGenerator::ENTITIES_FOLDER_NAME |
||
| 134 | ) + \strlen(AbstractGenerator::ENTITIES_FOLDER_NAME)); |
||
| 135 | $subPathNoEntites = str_replace('\\', '/', $namespaceNoEntities); |
||
| 136 | $plural = ucfirst($entityFqn::getPlural()); |
||
| 137 | $singular = ucfirst($entityFqn::getSingular()); |
||
| 138 | $relativePath = str_replace( |
||
| 139 | ['TemplateEntity', 'TemplateEntities'], |
||
| 140 | [$singular, $plural], |
||
| 141 | $relativePath |
||
| 142 | ); |
||
| 143 | $createdFile = realpath(static::WORK_DIR) |
||
| 144 | .'/'.AbstractCommand::DEFAULT_SRC_SUBFOLDER |
||
| 145 | .'/'.AbstractGenerator::ENTITY_RELATIONS_FOLDER_NAME |
||
| 146 | .'/'.$subPathNoEntites.'/' |
||
| 147 | .$className.'/'.$relativePath; |
||
| 148 | $this->assertNoMissedReplacements($createdFile); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | $this->qaGeneratedCode(); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get a an array of interfaces (short names) by reading the PHP file itself |
||
| 156 | * |
||
| 157 | * @param string $classFilePath |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | protected function getImplementedInterfacesFromClassFile(string $classFilePath): array |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get an array of all the interfaces for a class |
||
| 174 | * |
||
| 175 | * @param string $classFqn |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | * @throws \ReflectionException |
||
| 179 | */ |
||
| 180 | protected function getOwningEntityInterfaces(string $classFqn): array |
||
| 181 | { |
||
| 182 | $owningReflection = new \ReflectionClass($classFqn); |
||
| 183 | |||
| 184 | return $this->getImplementedInterfacesFromClassFile($owningReflection->getFileName()); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get an array of the interfaces we expect an entity to implement based on the has type |
||
| 189 | * |
||
| 190 | * @param string $entityFqn |
||
| 191 | * @param string $hasType |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | protected function getExpectedInterfacesForEntityFqn(string $entityFqn, string $hasType): array |
||
| 196 | { |
||
| 197 | $expectedInterfaces = []; |
||
| 198 | $expectedInterfaces[] = \in_array($hasType, RelationsGenerator::HAS_TYPES_PLURAL, true) |
||
| 199 | ? 'Has'.\ucwords($entityFqn::getPlural()).'Interface' |
||
| 200 | : 'Has'.\ucwords($entityFqn::getSingular()).'Interface'; |
||
| 201 | if (!\in_array($hasType, RelationsGenerator::HAS_TYPES_UNIDIRECTIONAL, true) |
||
| 202 | || \in_array($hasType, RelationsGenerator::HAS_TYPES_RECIPROCATED, true) |
||
| 203 | ) { |
||
| 204 | $expectedInterfaces[] = 'Reciprocates'.\ucwords($entityFqn::getSingular()).'Interface'; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $expectedInterfaces; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the inverse has type name, or false if there is no inverse has type |
||
| 212 | * |
||
| 213 | * @param string $hasType |
||
| 214 | * |
||
| 215 | * @return bool|mixed|null|string |
||
| 216 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 217 | */ |
||
| 218 | protected function getInverseHasType(string $hasType) |
||
| 219 | { |
||
| 220 | $inverseHasType = null; |
||
| 221 | switch ($hasType) { |
||
| 222 | case RelationsGenerator::HAS_ONE_TO_ONE: |
||
| 223 | case RelationsGenerator::HAS_MANY_TO_MANY: |
||
| 224 | return \str_replace( |
||
| 225 | RelationsGenerator::PREFIX_OWNING, |
||
| 226 | RelationsGenerator::PREFIX_INVERSE, |
||
| 227 | $hasType |
||
| 228 | ); |
||
| 229 | break; |
||
|
|
|||
| 230 | |||
| 231 | case RelationsGenerator::HAS_INVERSE_ONE_TO_ONE: |
||
| 232 | case RelationsGenerator::HAS_INVERSE_MANY_TO_MANY: |
||
| 233 | $inverseHasType = \str_replace( |
||
| 234 | RelationsGenerator::PREFIX_INVERSE, |
||
| 235 | RelationsGenerator::PREFIX_OWNING, |
||
| 236 | $hasType |
||
| 237 | ); |
||
| 238 | break; |
||
| 239 | |||
| 240 | case RelationsGenerator::HAS_MANY_TO_ONE: |
||
| 241 | $inverseHasType = RelationsGenerator::HAS_ONE_TO_MANY; |
||
| 242 | break; |
||
| 243 | case RelationsGenerator::HAS_ONE_TO_MANY: |
||
| 244 | $inverseHasType = RelationsGenerator::HAS_MANY_TO_ONE; |
||
| 245 | break; |
||
| 246 | case RelationsGenerator::HAS_UNIDIRECTIONAL_ONE_TO_ONE: |
||
| 247 | case RelationsGenerator::HAS_UNIDIRECTIONAL_ONE_TO_MANY: |
||
| 248 | case RelationsGenerator::HAS_UNIDIRECTIONAL_MANY_TO_ONE: |
||
| 249 | $inverseHasType = false; |
||
| 250 | break; |
||
| 251 | default: |
||
| 252 | $this->fail('Failed getting $inverseHasType for $hasType '.$hasType); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $inverseHasType; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Inspect the generated class and ensure that all required interfaces have been implemented |
||
| 260 | * |
||
| 261 | * @param string $owningEntityFqn |
||
| 262 | * @param string $hasType |
||
| 263 | * @param string $ownedEntityFqn |
||
| 264 | * @param bool $assertInverse |
||
| 265 | * |
||
| 266 | * @return mixed |
||
| 267 | * @throws \ReflectionException |
||
| 268 | * @SuppressWarnings(PHPMD) |
||
| 269 | */ |
||
| 270 | public function assertCorrectInterfacesSet( |
||
| 271 | string $owningEntityFqn, |
||
| 272 | string $hasType, |
||
| 273 | string $ownedEntityFqn, |
||
| 274 | bool $assertInverse = true |
||
| 275 | ) { |
||
| 276 | $owningInterfaces = $this->getOwningEntityInterfaces($owningEntityFqn); |
||
| 277 | $expectedInterfaces = $this->getExpectedInterfacesForEntityFqn($ownedEntityFqn, $hasType); |
||
| 278 | |||
| 279 | $missingOwningInterfaces = []; |
||
| 280 | foreach ($expectedInterfaces as $expectedInterface) { |
||
| 281 | if (!\in_array($expectedInterface, $owningInterfaces, true)) { |
||
| 282 | $missingOwningInterfaces[] = $expectedInterface; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | $this->assertEmpty( |
||
| 286 | $missingOwningInterfaces, |
||
| 287 | 'Entity '.$owningEntityFqn.' has some expected owning interfaces missing for hasType: ' |
||
| 288 | .$hasType."\n\n" |
||
| 289 | .print_r($missingOwningInterfaces, true) |
||
| 290 | ); |
||
| 291 | |||
| 292 | if ($assertInverse) { |
||
| 293 | $inverseHasType = $this->getInverseHasType($hasType); |
||
| 294 | if (false === $inverseHasType) { |
||
| 295 | return; |
||
| 296 | } |
||
| 297 | $this->assertCorrectInterfacesSet( |
||
| 298 | $ownedEntityFqn, |
||
| 299 | $inverseHasType, |
||
| 300 | $owningEntityFqn, |
||
| 301 | false |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | public function testSetRelationsBetweenEntities() |
||
| 368 | } |
||
| 369 | |||
| 370 | public function testNamingCollisions() |
||
| 371 | { |
||
| 372 | $this->entityGenerator = $this->getEntityGenerator(); |
||
| 373 | |||
| 374 | foreach (self::TEST_ENTITIES_NAMESPACING as $fqn) { |
||
| 375 | $this->entityGenerator->generateEntity($fqn); |
||
| 376 | } |
||
| 377 | |||
| 378 | $this->assertNull($this->relationsGenerator->setEntityHasRelationToEntity( |
||
| 379 | self::TEST_ENTITY_NAMESPACING_COMPANY, |
||
| 380 | 'OneToMany', |
||
| 381 | self::TEST_ENTITY_NAMESPACING_SOME_CLIENT |
||
| 382 | )); |
||
| 383 | |||
| 384 | $this->assertNull($this->relationsGenerator->setEntityHasRelationToEntity( |
||
| 385 | self::TEST_ENTITY_NAMESPACING_COMPANY, |
||
| 386 | 'OneToMany', |
||
| 387 | self::TEST_ENTITY_NAMESPACING_ANOTHER_CLIENT |
||
| 388 | )); |
||
| 389 | |||
| 390 | $this->getSchema()->validate(); |
||
| 391 | } |
||
| 392 | |||
| 393 | public function setup() |
||
| 401 | } |
||
| 402 | } |
||
| 403 | } |
||
| 404 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.