Complex classes like AssertionFactory 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AssertionFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 40 | class AssertionFactory | ||
| 41 | { | ||
| 42 | |||
| 43 | /** | ||
| 44 | * All simple data types which are known but are aliased without an is_... function. | ||
| 45 | * | ||
| 46 | * @var string[] $scalarTypeMappings | ||
| 47 | */ | ||
| 48 | protected $scalarTypeMappings = array( | ||
| 49 | 'boolean' => 'bool', | ||
| 50 | 'void' => 'null' | ||
| 51 | ); | ||
| 52 | |||
| 53 | /** | ||
| 54 | * All simple data types which are supported by PHP | ||
| 55 | * and have a is_... function. | ||
| 56 | * | ||
| 57 | * @var string[] $validScalarTypes | ||
| 58 | */ | ||
| 59 | protected $validScalarTypes = array( | ||
| 60 | 'array', | ||
| 61 | 'bool', | ||
| 62 | 'callable', | ||
| 63 | 'double', | ||
| 64 | 'float', | ||
| 65 | 'int', | ||
| 66 | 'integer', | ||
| 67 | 'long', | ||
| 68 | 'null', | ||
| 69 | 'numeric', | ||
| 70 | 'object', | ||
| 71 | 'real', | ||
| 72 | 'resource', | ||
| 73 | 'scalar', | ||
| 74 | 'string', | ||
| 75 | 'boolean', | ||
| 76 | 'void' | ||
| 77 | ); | ||
| 78 | |||
| 79 | /** | ||
| 80 | * The definition of the structure we are currently iterating through | ||
| 81 | * | ||
| 82 | * @var StructureDefinitionInterface $currentDefinition | ||
| 83 | */ | ||
| 84 | protected $currentDefinition; | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Parse assertions which are a collection of others | ||
| 88 | * | ||
| 89 | * @param string $connective How are they combined? E.g. "||" | ||
| 90 | * @param \stdClass $annotation The annotation to create chained assertions from | ||
| 91 | * | ||
| 92 | * @return \AppserverIo\Doppelgaenger\Entities\Assertions\ChainedAssertion | ||
| 93 | */ | ||
| 94 | protected function createChainedAssertion($connective, \stdClass $annotation) | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Will parse assertions from a DocBlock comment piece. If $usedAnnotation is given we will concentrate on that | ||
| 135 | * type of assertion only. | ||
| 136 | * We might return false on error | ||
| 137 | * | ||
| 138 | * @param \stdClass $annotation The annotation to create simple assertions from | ||
| 139 | * | ||
| 140 | * @return boolean|\AppserverIo\Psr\MetaobjectProtocol\Dbc\Assertions\AssertionInterface | ||
| 141 | * | ||
| 142 | * @throws \AppserverIo\Doppelgaenger\Exceptions\ParserException | ||
| 143 | */ | ||
| 144 | protected function createSimpleAssertion(\stdClass $annotation) | ||
| 195 | |||
| 196 | /** | ||
| 197 | * Will filter for any referenced structure as a indicated type hinting of complex types | ||
| 198 | * | ||
| 199 | * @param string $string The string potentially containing a structure name | ||
| 200 | * | ||
| 201 | * @return boolean | ||
| 202 | */ | ||
| 203 | protected function filterType($string) | ||
| 233 | |||
| 234 | /** | ||
| 235 | * Will filter any combinator defining a logical or-relation | ||
| 236 | * | ||
| 237 | * @param string $docString The DocBlock piece to search in | ||
| 238 | * | ||
| 239 | * @return boolean | ||
| 240 | */ | ||
| 241 | protected function filterOrCombinator($docString) | ||
| 250 | |||
| 251 | /** | ||
| 252 | * Will filter for any simple type that may be used to indicate type hinting | ||
| 253 | * | ||
| 254 | * @param string $string The string potentially containing a scalar type hint | ||
| 255 | * | ||
| 256 | * @return boolean|string | ||
| 257 | */ | ||
| 258 | protected function filterScalarType($string) | ||
| 279 | |||
| 280 | /** | ||
| 281 | * Will filter for type safe collections of the form array<Type> or Type[] | ||
| 282 | * | ||
| 283 | * @param string $string The string potentially containing a type hint for a typed collection | ||
| 284 | * | ||
| 285 | * @return boolean|string | ||
| 286 | */ | ||
| 287 | protected function filterTypedCollection($string) | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Will return an instance of an assertion fitting the passed annotation object | ||
| 312 | * | ||
| 313 | * @param \stdClass $annotation Annotation object to generate assertion from | ||
| 314 | * | ||
| 315 | * @return \AppserverIo\Psr\MetaobjectProtocol\Dbc\Assertions\AssertionInterface | ||
| 316 | * @throws \Exception | ||
| 317 | */ | ||
| 318 | public function getInstance(\stdClass $annotation) | ||
| 341 | |||
| 342 | /** | ||
| 343 | * Tries to create assertion of $assertionType | ||
| 344 | * | ||
| 345 | * @param string $assertionType the assertion type | ||
| 346 | * @param string $constraint the constraint to validate | ||
| 347 | * | ||
| 348 | * @return null|AssertionInterface | ||
| 349 | * @throws \Exception | ||
| 350 | */ | ||
| 351 | protected function createAssertion($assertionType, $constraint) | ||
| 376 | |||
| 377 | /** | ||
| 378 | * Resolves and returns the fully qualified namespace of $assertionType | ||
| 379 | * or null if $assertionType cannot be resolved to an accessible class | ||
| 380 | * | ||
| 381 | * @param string $assertionType the assertion type | ||
| 382 | * | ||
| 383 | * @return null|string | ||
| 384 | */ | ||
| 385 | protected function getAssertionClassPath($assertionType) | ||
| 408 | |||
| 409 | /** | ||
| 410 | * Iterates through the 'use' operators of the current structure and | ||
| 411 | * returns the fully qualified namespace to the Assertion or null if none is found | ||
| 412 | * | ||
| 413 | * @param string $assertionType the assertion type | ||
| 414 | * | ||
| 415 | * @return null|string | ||
| 416 | */ | ||
| 417 | protected function resolveUsedAssertionStructure($assertionType) | ||
| 431 | |||
| 432 | /** | ||
| 433 | * Getter for all valid scalar types we can create assertions for | ||
| 434 | * | ||
| 435 | * @return string[] | ||
| 436 | */ | ||
| 437 | public function getValidScalarTypes() | ||
| 441 | |||
| 442 | /** | ||
| 443 | * Sets the instance of the current definition | ||
| 444 | * | ||
| 445 | * @param StructureDefinitionInterface $currentDefinition the definition of the current structure | ||
| 446 | * | ||
| 447 | * @return void | ||
| 448 | */ | ||
| 449 | public function setCurrentDefinition(StructureDefinitionInterface $currentDefinition) | ||
| 453 | |||
| 454 | /** | ||
| 455 | * Returns the instance of the current definition | ||
| 456 | * | ||
| 457 | * @return null|StructureDefinitionInterface | ||
| 458 | */ | ||
| 459 | public function getCurrentDefinition() | ||
| 463 | } | ||
| 464 |