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 | * @var StructureDefinitionInterface |
||
| 81 | */ |
||
| 82 | protected $currentDefinition; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Parse assertions which are a collection of others |
||
| 86 | * |
||
| 87 | * @param string $connective How are they combined? E.g. "||" |
||
| 88 | * @param \stdClass $annotation The annotation to create chained assertions from |
||
| 89 | * |
||
| 90 | * @return \AppserverIo\Doppelgaenger\Entities\Assertions\ChainedAssertion |
||
| 91 | */ |
||
| 92 | protected function createChainedAssertion($connective, \stdClass $annotation) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Will parse assertions from a DocBlock comment piece. If $usedAnnotation is given we will concentrate on that |
||
| 133 | * type of assertion only. |
||
| 134 | * We might return false on error |
||
| 135 | * |
||
| 136 | * @param \stdClass $annotation The annotation to create simple assertions from |
||
| 137 | * |
||
| 138 | * @return boolean|\AppserverIo\Psr\MetaobjectProtocol\Dbc\Assertions\AssertionInterface |
||
| 139 | * |
||
| 140 | * @throws \AppserverIo\Doppelgaenger\Exceptions\ParserException |
||
| 141 | */ |
||
| 142 | protected function createSimpleAssertion(\stdClass $annotation) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Will filter for any referenced structure as a indicated type hinting of complex types |
||
| 196 | * |
||
| 197 | * @param string $string The string potentially containing a structure name |
||
| 198 | * |
||
| 199 | * @return boolean |
||
| 200 | */ |
||
| 201 | protected function filterType($string) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Will filter any combinator defining a logical or-relation |
||
| 234 | * |
||
| 235 | * @param string $docString The DocBlock piece to search in |
||
| 236 | * |
||
| 237 | * @return boolean |
||
| 238 | */ |
||
| 239 | protected function filterOrCombinator($docString) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Will filter for any simple type that may be used to indicate type hinting |
||
| 251 | * |
||
| 252 | * @param string $string The string potentially containing a scalar type hint |
||
| 253 | * |
||
| 254 | * @return boolean|string |
||
| 255 | */ |
||
| 256 | protected function filterScalarType($string) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Will filter for type safe collections of the form array<Type> or Type[] |
||
| 280 | * |
||
| 281 | * @param string $string The string potentially containing a type hint for a typed collection |
||
| 282 | * |
||
| 283 | * @return boolean|string |
||
| 284 | */ |
||
| 285 | protected function filterTypedCollection($string) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Will return an instance of an assertion fitting the passed annotation object |
||
| 310 | * |
||
| 311 | * @param \stdClass $annotation Annotation object to generate assertion from |
||
| 312 | * |
||
| 313 | * @return \AppserverIo\Psr\MetaobjectProtocol\Dbc\Assertions\AssertionInterface |
||
| 314 | * @throws \Exception |
||
| 315 | */ |
||
| 316 | public function getInstance(\stdClass $annotation) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Tries to create assertion of $assertionType |
||
| 340 | * @param string $assertionType the assertion type |
||
| 341 | * @param string $constraint the constraint to validate |
||
| 342 | * @return null|AssertionInterface |
||
| 343 | * @throws \Exception |
||
| 344 | */ |
||
| 345 | public function createAssertion($assertionType, $constraint) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Resolves and returns the fully qualified namespace of $assertionType |
||
| 372 | * or null if $assertionType cannot be resolved to an accessible class |
||
| 373 | * @param string $assertionType the assertion type |
||
| 374 | * @return null|string |
||
| 375 | */ |
||
| 376 | private function getAssertionClassPath($assertionType) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Iterates through the 'use' operators of the current structure and |
||
| 402 | * returns the fully qualified namespace to the Assertion or null if none is found |
||
| 403 | * @param string $assertionType the assertion type |
||
| 404 | * @return null|string |
||
| 405 | */ |
||
| 406 | private function resolveUsedAssertionStructure($assertionType) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Getter for all valid scalar types we can create assertions for |
||
| 423 | * |
||
| 424 | * @return string[] |
||
| 425 | */ |
||
| 426 | public function getValidScalarTypes() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param StructureDefinitionInterface $currentDefinition the definition of the current structure |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | public function setCurrentDefinition(StructureDefinitionInterface $currentDefinition) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return null|StructureDefinitionInterface |
||
| 442 | */ |
||
| 443 | public function getCurrentDefinition() |
||
| 447 | } |
||
| 448 |