| Total Complexity | 55 | 
| Total Lines | 322 | 
| Duplicated Lines | 0 % | 
| Changes | 8 | ||
| Bugs | 1 | Features | 0 | 
Complex classes like FindEditableObjects 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.
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 FindEditableObjects, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 17 | class FindEditableObjects | ||
| 18 | { | ||
| 19 | use Extensible; | ||
| 20 | use Configurable; | ||
| 21 | use Injectable; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @var string | ||
| 25 | */ | ||
| 26 | private const CACHE_NAME = 'FindEditableObjectsCache'; | ||
| 27 | |||
| 28 | protected $additionalCacheName = ''; | ||
| 29 | |||
| 30 | protected $relationTypesCovered = []; | ||
| 31 | |||
| 32 | protected $excludedClasses = []; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * format is as follows: | ||
| 36 | * ```php | ||
| 37 | * [ | ||
| 38 | * 'valid_methods_edit' => [ | ||
| 39 | * ClassNameA => false, // tested and does not have any available methods | ||
| 40 | * ClassNameB => MethodName1, // tested found method MethodName1 that can be used. | ||
| 41 | * ClassNameC => MethodName2, // tested found method MethodName2 that can be used. | ||
| 42 | * ClassNameD => false, // tested and does not have any available methods | ||
| 43 | * ], | ||
| 44 | * 'valid_methods_view' => [ | ||
| 45 | * ClassNameA => false, // tested and does not have any available methods | ||
| 46 | * ClassNameB => MethodName1, // tested found method MethodName1 that can be used. | ||
| 47 | * ClassNameC => MethodName2, // tested found method MethodName2 that can be used. | ||
| 48 | * ClassNameD => false, // tested and does not have any available methods | ||
| 49 | * ], | ||
| 50 | * 'valid_methods_view_links' => [ | ||
| 51 | * [ClassNameX_IDY] => 'MyLinkView', | ||
| 52 | * [ClassNameX_IDZ] => 'MyLinkView', | ||
| 53 | * ], | ||
| 54 | * 'valid_methods_edit_links' => [ | ||
| 55 | * [ClassNameX_IDY] => 'MyLinkEdit', | ||
| 56 | * [ClassNameX_IDZ] => 'MyLinkEdit', | ||
| 57 | * ], | ||
| 58 | * 'rels' => | ||
| 59 | * 'ClassNameY' => [ | ||
| 60 | * 'MethodA' => RelationClassNameB, | ||
| 61 | * 'MethodC' => RelationClassNameD, | ||
| 62 | * ], | ||
| 63 | * ], | ||
| 64 | * 'validMethods' => [ | ||
| 65 | * 'valid_methods_edit' => [ | ||
| 66 | * 'A', | ||
| 67 | * 'B', | ||
| 68 | * ] | ||
| 69 | * 'valid_methods_view' => [ | ||
| 70 | * 'A', | ||
| 71 | * 'B', | ||
| 72 | * ] | ||
| 73 | * ] | ||
| 74 | * ] | ||
| 75 | * ``` | ||
| 76 | * we use false to be able to use empty to work out if it has been tested before. | ||
| 77 | * | ||
| 78 | * @var array | ||
| 79 | */ | ||
| 80 | protected $cache = [ | ||
| 81 | 'valid_methods_edit', | ||
| 82 | 'valid_methods_view', | ||
| 83 | 'valid_methods_image', | ||
| 84 | 'valid_methods_view_links', | ||
| 85 | 'valid_methods_edit_links', | ||
| 86 | 'valid_methods_image_links', | ||
| 87 | 'rels', | ||
| 88 | 'validMethods' => [ | ||
| 89 | 'valid_methods_edit' => [], | ||
| 90 | 'valid_methods_view' => [], | ||
| 91 | 'valid_methods_image' => [], | ||
| 92 | ], | ||
| 93 | ]; | ||
| 94 | |||
| 95 | private static $max_relation_depth = 3; | ||
| 96 | |||
| 97 | private static $valid_methods_edit = [ | ||
| 98 | 'CMSEditLink', | ||
| 99 | 'getCMSEditLink', | ||
| 100 | 'EditLink', | ||
| 101 | 'getEditLink', | ||
| 102 | ]; | ||
| 103 | |||
| 104 | private static $valid_methods_view = [ | ||
| 105 | 'getLink', | ||
| 106 | 'Link', | ||
| 107 | ]; | ||
| 108 | |||
| 109 | private static $valid_methods_image = [ | ||
| 110 | 'StripThumbnail', | ||
| 111 | 'CMSThumbnail', | ||
| 112 | 'getCMSThumbnail', | ||
| 113 | ]; | ||
| 114 | |||
| 115 | public function getFileCache() | ||
| 116 |     { | ||
| 117 | return Injector::inst()->get(Cache::class); | ||
| 118 | } | ||
| 119 | |||
| 120 | public function initCache(string $additionalCacheName): self | ||
| 121 |     { | ||
| 122 | $this->additionalCacheName = $additionalCacheName; | ||
| 123 | $this->cache = $this->getFileCache()->getCacheValues(self::CACHE_NAME . '_' . $this->additionalCacheName); | ||
| 124 | |||
| 125 | return $this; | ||
| 126 | } | ||
| 127 | |||
| 128 | public function saveCache(): self | ||
| 129 |     { | ||
| 130 | $this->getFileCache()->setCacheValues(self::CACHE_NAME . '_' . $this->additionalCacheName, $this->cache); | ||
| 131 | |||
| 132 | return $this; | ||
| 133 | } | ||
| 134 | |||
| 135 | public function setExcludedClasses(array $excludedClasses): self | ||
| 136 |     { | ||
| 137 | $this->excludedClasses = $excludedClasses; | ||
| 138 | |||
| 139 | return $this; | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * returns an link to an object that can be edited in the CMS. | ||
| 144 | * | ||
| 145 | * @param mixed $dataObject | ||
| 146 | */ | ||
| 147 | public function getCMSEditLink($dataObject): string | ||
| 148 |     { | ||
| 149 | return $this->getLinkInner($dataObject, 'valid_methods_edit'); | ||
| 150 | } | ||
| 151 | |||
| 152 | /** | ||
| 153 | * returns a link to an object that can be viewed. | ||
| 154 | * | ||
| 155 | * @param mixed $dataObject | ||
| 156 | */ | ||
| 157 | public function getLink($dataObject): string | ||
| 160 | } | ||
| 161 | |||
| 162 | /** | ||
| 163 | * returns link to a thumbnail. | ||
| 164 | * | ||
| 165 | * @param mixed $dataObject | ||
| 166 | */ | ||
| 167 | public function getCMSThumbnail($dataObject): string | ||
| 168 |     { | ||
| 169 | return $this->getLinkInner($dataObject, 'valid_methods_image'); | ||
| 170 | } | ||
| 171 | |||
| 172 | /** | ||
| 173 | * returns an link to an object that can be viewed. | ||
| 174 | * | ||
| 175 | * @param mixed $dataObject | ||
| 176 | */ | ||
| 177 | protected function getLinkInner($dataObject, string $type): string | ||
| 189 | } | ||
| 190 | |||
| 191 | protected function checkForValidMethods($dataObject, string $type, ?int $relationDepth = 0): string | ||
| 277 | } | ||
| 278 | |||
| 279 | protected function cleanupLink(?string $link = null): string | ||
| 280 |     { | ||
| 281 |         if ($link) { | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | protected function getRelations($dataObject): array | ||
| 315 | } | ||
| 316 | |||
| 317 | protected function getValidMethods(string $type): array | ||
| 324 | } | ||
| 325 | |||
| 326 | /** | ||
| 327 | * it either is NOT in the excluded list or it is in the included list. | ||
| 328 | */ | ||
| 329 | protected function classCanBeIncluded(string $dataObjectClassName): bool | ||
| 341 |