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:
Complex classes like DocumentParser 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 DocumentParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class DocumentParser |
||
| 27 | { |
||
| 28 | const PROPERTY_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Property'; |
||
| 29 | const EMBEDDED_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Embedded'; |
||
| 30 | const DOCUMENT_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Document'; |
||
| 31 | const OBJECT_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Object'; |
||
| 32 | const NESTED_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Nested'; |
||
| 33 | |||
| 34 | // Meta fields |
||
| 35 | const ID_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Id'; |
||
| 36 | const PARENT_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\ParentDocument'; |
||
| 37 | const ROUTING_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Routing'; |
||
| 38 | const VERSION_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Version'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Reader Used to read document annotations. |
||
| 42 | */ |
||
| 43 | private $reader; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var DocumentFinder Used to find documents. |
||
| 47 | */ |
||
| 48 | private $finder; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array Contains gathered objects which later adds to documents. |
||
| 52 | */ |
||
| 53 | private $objects = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array Document properties aliases. |
||
| 57 | */ |
||
| 58 | private $aliases = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array Local cache for document properties. |
||
| 62 | */ |
||
| 63 | private $properties = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Analyzers used in documents. |
||
| 67 | * |
||
| 68 | * @var string[] |
||
| 69 | */ |
||
| 70 | private $analyzers = []; |
||
|
|
|||
| 71 | |||
| 72 | /** |
||
| 73 | * @param Reader $reader Used for reading annotations. |
||
| 74 | * @param DocumentFinder $finder Used for resolving namespaces. |
||
| 75 | */ |
||
| 76 | public function __construct(Reader $reader, DocumentFinder $finder) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Parses documents by used annotations and returns mapping for elasticsearch with some extra metadata. |
||
| 85 | * |
||
| 86 | * @param \ReflectionClass $class |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | * @throws MissingDocumentAnnotationException |
||
| 90 | */ |
||
| 91 | public function parse(\ReflectionClass $class) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns document annotation data from reader. |
||
| 126 | * |
||
| 127 | * @param \ReflectionClass $document |
||
| 128 | * |
||
| 129 | * @return Document|null |
||
| 130 | */ |
||
| 131 | private function getDocumentAnnotationData($document) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns property annotation data from reader. |
||
| 138 | * |
||
| 139 | * @param \ReflectionProperty $property |
||
| 140 | * |
||
| 141 | * @return Property|null |
||
| 142 | */ |
||
| 143 | View Code Duplication | private function getPropertyAnnotationData(\ReflectionProperty $property) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Returns Embedded annotation data from reader. |
||
| 156 | * |
||
| 157 | * @param \ReflectionProperty $property |
||
| 158 | * |
||
| 159 | * @return Embedded|null |
||
| 160 | */ |
||
| 161 | View Code Duplication | private function getEmbeddedAnnotationData(\ReflectionProperty $property) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Returns meta field annotation data from reader. |
||
| 174 | * |
||
| 175 | * @param \ReflectionProperty $property |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private function getMetaFieldAnnotationData($property) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns objects used in document. |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | private function getObjects() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Finds aliases for every property used in document including parent classes. |
||
| 215 | * |
||
| 216 | * @param \ReflectionClass $reflectionClass |
||
| 217 | * @param array $metaFields |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | private function getAliases(\ReflectionClass $reflectionClass, array &$metaFields = null) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Checks if class have setter and getter, and returns them in array. |
||
| 301 | * |
||
| 302 | * @param \ReflectionClass $reflectionClass |
||
| 303 | * @param string $property |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | private function getMutatorMethods(\ReflectionClass $reflectionClass, $property, $propertyType) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Registers annotations to registry so that it could be used by reader. |
||
| 354 | */ |
||
| 355 | private function registerAnnotations() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns document type. |
||
| 376 | * |
||
| 377 | * @param string $document Format must be like AcmeBundle:Document. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | private function getDocumentType($document) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns all defined properties including private from parents. |
||
| 392 | * |
||
| 393 | * @param \ReflectionClass $reflectionClass |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | private function getDocumentPropertiesReflection(\ReflectionClass $reflectionClass) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Parses analyzers list from document mapping. |
||
| 426 | * |
||
| 427 | * @param \ReflectionClass $reflectionClass |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | private function getAnalyzers(\ReflectionClass $reflectionClass) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Returns properties of reflection class. |
||
| 469 | * |
||
| 470 | * @param \ReflectionClass $reflectionClass Class to read properties from. |
||
| 471 | * @param array $properties Properties to skip. |
||
| 472 | * @param bool $flag If false exludes properties, true only includes properties. |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | private function getProperties(\ReflectionClass $reflectionClass, $properties = [], $flag = false) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Returns object mapping. |
||
| 513 | * |
||
| 514 | * Loads from cache if it's already loaded. |
||
| 515 | * |
||
| 516 | * @param string $className |
||
| 517 | * |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | private function getObjectMapping($className) |
||
| 553 | } |
||
| 554 |
This check marks private properties in classes that are never used. Those properties can be removed.