| Total Complexity | 46 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | 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 |
||
| 15 | class FindEditableObjects |
||
| 16 | { |
||
| 17 | use Extensible; |
||
| 18 | use Configurable; |
||
| 19 | use Injectable; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private const CACHE_NAME = 'FindEditableObjectsCache'; |
||
| 25 | |||
| 26 | protected $relationTypesCovered = []; |
||
| 27 | |||
| 28 | protected $excludedClasses = []; |
||
| 29 | protected $includedClasses = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * format is as follows: |
||
| 33 | * ```php |
||
| 34 | * [ |
||
| 35 | * 'valid_methods_edit' => [ |
||
| 36 | * ClassNameA => true, // tested and does not have any available methods |
||
| 37 | * ClassNameB => MethodName1, // tested found method MethodName1 that can be used. |
||
| 38 | * ClassNameC => MethodName2, // tested found method MethodName2 that can be used. |
||
| 39 | * ClassNameD => true, // tested and does not have any available methods |
||
| 40 | * ], |
||
| 41 | * 'valid_methods_view' => [ |
||
| 42 | * ClassNameA => true, // tested and does not have any available methods |
||
| 43 | * ClassNameB => MethodName1, // tested found method MethodName1 that can be used. |
||
| 44 | * ClassNameC => MethodName2, // tested found method MethodName2 that can be used. |
||
| 45 | * ClassNameD => true, // tested and does not have any available methods |
||
| 46 | * ], |
||
| 47 | * 'valid_methods_view_links' => [ |
||
| 48 | * [ClassNameX_IDY] => 'MyLinkView', |
||
| 49 | * [ClassNameX_IDZ] => 'MyLinkView', |
||
| 50 | * ], |
||
| 51 | * 'valid_methods_edit_links' => [ |
||
| 52 | * [ClassNameX_IDY] => 'MyLinkEdit', |
||
| 53 | * [ClassNameX_IDZ] => 'MyLinkEdit', |
||
| 54 | * ], |
||
| 55 | * 'rels' => |
||
| 56 | * 'ClassNameY' => [ |
||
| 57 | * 'MethodA' => RelationClassNameB, |
||
| 58 | * 'MethodC' => RelationClassNameD, |
||
| 59 | * ], |
||
| 60 | * ], |
||
| 61 | * 'validMethods' => [ |
||
| 62 | * 'valid_methods_edit' => [ |
||
| 63 | * 'A', |
||
| 64 | * 'B', |
||
| 65 | * ] |
||
| 66 | * 'valid_methods_view' => [ |
||
| 67 | * 'A', |
||
| 68 | * 'B', |
||
| 69 | * ] |
||
| 70 | * ] |
||
| 71 | * ] |
||
| 72 | * ``` |
||
| 73 | * we use true rather than false to be able to use empty to work out if it has been tested before. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $cache = [ |
||
| 78 | 'valid_methods_edit', |
||
| 79 | 'valid_methods_view', |
||
| 80 | 'valid_methods_view_links', |
||
| 81 | 'valid_methods_edit_links', |
||
| 82 | 'rels', |
||
| 83 | 'validMethods' => [ |
||
| 84 | 'valid_methods_edit' => [], |
||
| 85 | 'valid_methods_view' => [], |
||
| 86 | ], |
||
| 87 | ]; |
||
| 88 | |||
| 89 | private static $max_relation_depth = 3; |
||
| 90 | |||
| 91 | private static $valid_methods_edit = [ |
||
| 92 | 'CMSEditLink', |
||
| 93 | 'getCMSEditLink', |
||
| 94 | ]; |
||
| 95 | |||
| 96 | private static $valid_methods_view = [ |
||
| 97 | 'getLink', |
||
| 98 | 'Link', |
||
| 99 | ]; |
||
| 100 | |||
| 101 | public function getFileCache() |
||
| 102 | { |
||
| 103 | return Injector::inst()->get(Cache::class); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function initCache(): self |
||
| 107 | { |
||
| 108 | $this->cache = $this->getFileCache()->getCacheValues(self::CACHE_NAME); |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function setExcludedClasses(array $excludedClasses): self |
||
| 114 | { |
||
| 115 | $this->excludedClasses = $excludedClasses; |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function setIncludedClasses(array $includedClasses): self |
||
| 121 | { |
||
| 122 | $this->includedClasses = $includedClasses; |
||
| 123 | |||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function saveCache(): self |
||
| 128 | { |
||
| 129 | $this->getFileCache()->setCacheValues(self::CACHE_NAME, $this->cache); |
||
| 130 | |||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * returns an link to an object that can be edited in the CMS. |
||
| 136 | * |
||
| 137 | * @param mixed $dataObject |
||
| 138 | */ |
||
| 139 | public function getCMSEditLink($dataObject): string |
||
| 140 | { |
||
| 141 | return $this->getLinkInner($dataObject, 'valid_methods_edit'); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * returns an link to an object that can be viewed. |
||
| 146 | * |
||
| 147 | * @param mixed $dataObject |
||
| 148 | */ |
||
| 149 | public function getLink($dataObject): string |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * returns an link to an object that can be viewed. |
||
| 156 | * |
||
| 157 | * @param mixed $dataObject |
||
| 158 | */ |
||
| 159 | protected function getLinkInner($dataObject, string $type): string |
||
| 171 | } |
||
| 172 | |||
| 173 | protected function checkForValidMethods($dataObject, string $type, int $relationDepth = 0): string |
||
| 255 | } |
||
| 256 | |||
| 257 | protected function getRelations($dataObject): array |
||
| 275 | } |
||
| 276 | |||
| 277 | protected function getValidMethods(string $type): array |
||
| 284 | } |
||
| 285 | |||
| 286 | protected function classCanBeIncluded(string $dataObjectClassName): bool |
||
| 302 |