Complex classes like AnnotationDriver 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 AnnotationDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AnnotationDriver extends AbstractDriver |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Annotations reader |
||
| 19 | * @var \Doctrine\Common\Annotations\AnnotationReader $reader |
||
| 20 | */ |
||
| 21 | private $reader; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Extensions of the files to read |
||
| 25 | * @var array $paths |
||
| 26 | */ |
||
| 27 | protected $extensions = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The array of class names. |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $classNames = []; |
||
| 34 | |||
| 35 | |||
| 36 | 47 | public function __construct(Annotations\AnnotationReader $reader, $paths = []) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Get all the metadata class names known to this driver. |
||
| 47 | * @throws DrestException |
||
| 48 | * @return array $classes |
||
| 49 | */ |
||
| 50 | 44 | public function getAllClassNames() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Add an extension to look for classes |
||
| 96 | * @param string $extension - can be a string or an array of extensions |
||
| 97 | */ |
||
| 98 | 47 | public function addExtension($extension) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Remove all registered extensions, if an extension name is passed, only remove that entry |
||
| 110 | * @param string $extension |
||
| 111 | */ |
||
| 112 | 1 | public function removeExtensions($extension = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Does the class contain a drest resource object |
||
| 126 | * @param string $className |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 32 | public function isDrestResource($className) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Load metadata for a class name |
||
| 144 | * @param object|string $className - Pass in either the class name, or an instance of that class |
||
| 145 | * @return Mapping\ClassMetaData|null $metaData - return null if metadata couldn't be populated from annotations |
||
| 146 | * @throws DrestException |
||
| 147 | */ |
||
| 148 | 45 | public function loadMetadataForClass($className) |
|
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Process the method |
||
| 180 | * @param \ReflectionMethod[] $methods |
||
| 181 | * @param Mapping\ClassMetaData $metadata |
||
| 182 | * @throws DrestException |
||
| 183 | */ |
||
| 184 | 40 | protected function processMethods($methods, Mapping\ClassMetaData $metadata) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Factory method for the Annotation Driver |
||
| 217 | * |
||
| 218 | * @param array|string $paths |
||
| 219 | * @return AnnotationDriver |
||
| 220 | */ |
||
| 221 | 47 | public static function create($paths = []) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Driver registration template method. |
||
| 230 | */ |
||
| 231 | 31 | public static function register() { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Register out annotation classes with the annotation registry. |
||
| 237 | */ |
||
| 238 | 107 | public static function registerAnnotations() |
|
| 242 | } |
||
| 243 |