Complex classes like ReflectionService 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 ReflectionService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ReflectionService implements \TYPO3\CMS\Core\SingletonInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
| 31 | */ |
||
| 32 | protected $objectManager; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Whether this service has been initialized. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $initialized = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend |
||
| 43 | */ |
||
| 44 | protected $dataCache; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Whether class alterations should be detected on each initialization. |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $detectClassChanges = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * All available class names to consider. Class name = key, value is the |
||
| 55 | * UNIX timestamp the class was reflected. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $reflectedClassNames = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Array of tags and the names of classes which are tagged with them. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $taggedClasses = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Array of class names and their tags and values. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $classTagsValues = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Array of class names, method names and their tags and values. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $methodTagsValues = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Array of class names, method names, their parameters and additional |
||
| 84 | * information about the parameters. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $methodParameters = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Array of class names and names of their properties. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $classPropertyNames = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Array of class names and names of their methods. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $classMethodNames = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Array of class names, property names and their tags and values. |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $propertyTagsValues = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * List of tags which are ignored while reflecting class and method annotations. |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $ignoredTags = ['package', 'subpackage', 'license', 'copyright', 'author', 'version', 'const']; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Indicates whether the Reflection cache needs to be updated. |
||
| 120 | * |
||
| 121 | * This flag needs to be set as soon as new Reflection information was |
||
| 122 | * created. |
||
| 123 | * |
||
| 124 | * @see reflectClass() |
||
| 125 | * @see getMethodReflection() |
||
| 126 | * @var bool |
||
| 127 | */ |
||
| 128 | protected $dataCacheNeedsUpdate = false; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Local cache for Class schemata |
||
| 132 | * |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | protected $classSchemata = []; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface |
||
| 139 | */ |
||
| 140 | protected $configurationManager; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var string |
||
| 144 | */ |
||
| 145 | protected $cacheIdentifier; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Internal runtime cache of method reflection objects |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $methodReflections = []; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager |
||
| 156 | */ |
||
| 157 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager |
||
| 164 | */ |
||
| 165 | public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Sets the data cache. |
||
| 172 | * |
||
| 173 | * The cache must be set before initializing the Reflection Service. |
||
| 174 | * |
||
| 175 | * @param \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $dataCache Cache for the Reflection service |
||
| 176 | */ |
||
| 177 | public function setDataCache(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $dataCache) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Initializes this service |
||
| 184 | * |
||
| 185 | * @throws Exception |
||
| 186 | */ |
||
| 187 | public function initialize() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns whether the Reflection Service is initialized. |
||
| 200 | * |
||
| 201 | * @return bool true if the Reflection Service is initialized, otherwise false |
||
| 202 | */ |
||
| 203 | public function isInitialized() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Shuts the Reflection Service down. |
||
| 210 | */ |
||
| 211 | public function shutdown() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns all tags and their values the specified class is tagged with |
||
| 221 | * |
||
| 222 | * @param string $className Name of the class |
||
| 223 | * @return array An array of tags and their values or an empty array if no tags were found |
||
| 224 | */ |
||
| 225 | public function getClassTagsValues($className) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the values of the specified class tag |
||
| 238 | * |
||
| 239 | * @param string $className Name of the class containing the property |
||
| 240 | * @param string $tag Tag to return the values of |
||
| 241 | * @return array An array of values or an empty array if the tag was not found |
||
| 242 | */ |
||
| 243 | public function getClassTagValues($className, $tag) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the names of all properties of the specified class |
||
| 256 | * |
||
| 257 | * @param string $className Name of the class to return the property names of |
||
| 258 | * @return array An array of property names or an empty array if none exist |
||
| 259 | */ |
||
| 260 | public function getClassPropertyNames($className) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns the class schema for the given class |
||
| 270 | * |
||
| 271 | * @param mixed $classNameOrObject The class name or an object |
||
| 272 | * @return ClassSchema |
||
| 273 | */ |
||
| 274 | public function getClassSchema($classNameOrObject) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Wrapper for method_exists() which tells if the given method exists. |
||
| 285 | * |
||
| 286 | * @param string $className Name of the class containing the method |
||
| 287 | * @param string $methodName Name of the method |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function hasMethod($className, $methodName) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns all tags and their values the specified method is tagged with |
||
| 306 | * |
||
| 307 | * @param string $className Name of the class containing the method |
||
| 308 | * @param string $methodName Name of the method to return the tags and values of |
||
| 309 | * @return array An array of tags and their values or an empty array of no tags were found |
||
| 310 | */ |
||
| 311 | public function getMethodTagsValues($className, $methodName) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns an array of parameters of the given method. Each entry contains |
||
| 327 | * additional information about the parameter position, type hint etc. |
||
| 328 | * |
||
| 329 | * @param string $className Name of the class containing the method |
||
| 330 | * @param string $methodName Name of the method to return parameter information of |
||
| 331 | * @return array An array of parameter names and additional information or an empty array of no parameters were found |
||
| 332 | */ |
||
| 333 | public function getMethodParameters($className, $methodName) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns all tags and their values the specified class property is tagged with |
||
| 347 | * |
||
| 348 | * @param string $className Name of the class containing the property |
||
| 349 | * @param string $propertyName Name of the property to return the tags and values of |
||
| 350 | * @return array An array of tags and their values or an empty array of no tags were found |
||
| 351 | */ |
||
| 352 | public function getPropertyTagsValues($className, $propertyName) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the values of the specified class property tag |
||
| 365 | * |
||
| 366 | * @param string $className Name of the class containing the property |
||
| 367 | * @param string $propertyName Name of the tagged property |
||
| 368 | * @param string $tag Tag to return the values of |
||
| 369 | * @return array An array of values or an empty array if the tag was not found |
||
| 370 | */ |
||
| 371 | public function getPropertyTagValues($className, $propertyName, $tag) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Tells if the specified class is known to this reflection service and |
||
| 384 | * reflection information is available. |
||
| 385 | * |
||
| 386 | * @param string $className Name of the class |
||
| 387 | * @return bool If the class is reflected by this service |
||
| 388 | */ |
||
| 389 | public function isClassReflected($className) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Tells if the specified class is tagged with the given tag |
||
| 396 | * |
||
| 397 | * @param string $className Name of the class |
||
| 398 | * @param string $tag Tag to check for |
||
| 399 | * @return bool TRUE if the class is tagged with $tag, otherwise FALSE |
||
| 400 | */ |
||
| 401 | public function isClassTaggedWith($className, $tag) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Tells if the specified class property is tagged with the given tag |
||
| 417 | * |
||
| 418 | * @param string $className Name of the class |
||
| 419 | * @param string $propertyName Name of the property |
||
| 420 | * @param string $tag Tag to check for |
||
| 421 | * @return bool TRUE if the class property is tagged with $tag, otherwise FALSE |
||
| 422 | */ |
||
| 423 | public function isPropertyTaggedWith($className, $propertyName, $tag) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Reflects the given class and stores the results in this service's properties. |
||
| 439 | * |
||
| 440 | * @param string $className Full qualified name of the class to reflect |
||
| 441 | */ |
||
| 442 | protected function reflectClass($className) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Builds class schemata from classes annotated as entities or value objects |
||
| 478 | * |
||
| 479 | * @param string $className |
||
| 480 | * @throws Exception |
||
| 481 | * @return ClassSchema The class schema |
||
| 482 | */ |
||
| 483 | protected function buildClassSchema($className) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Converts the given parameter reflection into an information array |
||
| 517 | * |
||
| 518 | * @param ParameterReflection $parameter The parameter to reflect |
||
| 519 | * @param int $parameterPosition |
||
| 520 | * @param MethodReflection|null $method |
||
| 521 | * @return array Parameter information array |
||
| 522 | */ |
||
| 523 | protected function convertParameterReflectionToArray(ParameterReflection $parameter, $parameterPosition, MethodReflection $method = null) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Returns the Reflection of a method. |
||
| 563 | * |
||
| 564 | * @param string $className Name of the class containing the method |
||
| 565 | * @param string $methodName Name of the method to return the Reflection for |
||
| 566 | * @return MethodReflection the method Reflection object |
||
| 567 | */ |
||
| 568 | protected function getMethodReflection($className, $methodName) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Tries to load the reflection data from this service's cache. |
||
| 579 | */ |
||
| 580 | protected function loadFromCache() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Exports the internal reflection data into the ReflectionData cache. |
||
| 592 | * |
||
| 593 | * @throws Exception |
||
| 594 | */ |
||
| 595 | protected function saveToCache() |
||
| 618 | } |
||
| 619 |