Total Complexity | 45 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 5 | ||
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 $additionalCacheName = ''; |
||
27 | protected $relationTypesCovered = []; |
||
28 | |||
29 | protected $excludedClasses = []; |
||
30 | |||
31 | /** |
||
32 | * format is as follows: |
||
33 | * ```php |
||
34 | * [ |
||
35 | * 'valid_methods_edit' => [ |
||
36 | * ClassNameA => false, // 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 => false, // tested and does not have any available methods |
||
40 | * ], |
||
41 | * 'valid_methods_view' => [ |
||
42 | * ClassNameA => false, // 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 => false, // 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 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_image', |
||
81 | 'valid_methods_view_links', |
||
82 | 'valid_methods_edit_links', |
||
83 | 'valid_methods_image_links', |
||
84 | 'rels', |
||
85 | 'validMethods' => [ |
||
86 | 'valid_methods_edit' => [], |
||
87 | 'valid_methods_view' => [], |
||
88 | 'valid_methods_image' => [], |
||
89 | ], |
||
90 | ]; |
||
91 | |||
92 | private static $max_relation_depth = 3; |
||
93 | |||
94 | private static $valid_methods_edit = [ |
||
95 | 'CMSEditLink', |
||
96 | 'getCMSEditLink', |
||
97 | ]; |
||
98 | |||
99 | private static $valid_methods_view = [ |
||
100 | 'getLink', |
||
101 | 'Link', |
||
102 | ]; |
||
103 | |||
104 | private static $valid_methods_image = [ |
||
105 | 'StripThumbnail', |
||
106 | 'CMSThumbnail', |
||
107 | 'getCMSThumbnail', |
||
108 | ]; |
||
109 | |||
110 | public function getFileCache() |
||
113 | } |
||
114 | |||
115 | public function initCache(string $additionalCacheName): self |
||
116 | { |
||
117 | $this->additionalCacheName = $additionalCacheName; |
||
118 | $this->cache = $this->getFileCache()->getCacheValues(self::CACHE_NAME . '_' . $this->additionalCacheName); |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | public function saveCache(): self |
||
124 | { |
||
125 | $this->getFileCache()->setCacheValues(self::CACHE_NAME . '_' . $this->additionalCacheName, $this->cache); |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | public function setExcludedClasses(array $excludedClasses): self |
||
131 | { |
||
132 | $this->excludedClasses = $excludedClasses; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * returns an link to an object that can be edited in the CMS. |
||
139 | * |
||
140 | * @param mixed $dataObject |
||
141 | */ |
||
142 | public function getCMSEditLink($dataObject): string |
||
143 | { |
||
144 | return $this->getLinkInner($dataObject, 'valid_methods_edit'); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * returns a link to an object that can be viewed. |
||
149 | * |
||
150 | * @param mixed $dataObject |
||
151 | */ |
||
152 | public function getLink($dataObject): string |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * returns link to a thumbnail. |
||
159 | * |
||
160 | * @param mixed $dataObject |
||
161 | */ |
||
162 | public function getCMSThumbnail($dataObject): string |
||
163 | { |
||
164 | return $this->getLinkInner($dataObject, 'valid_methods_image'); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * returns an link to an object that can be viewed. |
||
169 | * |
||
170 | * @param mixed $dataObject |
||
171 | */ |
||
172 | protected function getLinkInner($dataObject, string $type): string |
||
184 | } |
||
185 | |||
186 | protected function checkForValidMethods($dataObject, string $type, ?int $relationDepth = 0): string |
||
187 | { |
||
265 | } |
||
266 | |||
267 | protected function getRelations($dataObject): array |
||
285 | } |
||
286 | |||
287 | protected function getValidMethods(string $type): array |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * it either is NOT in the excluded list or it is in the included list. |
||
298 | * |
||
299 | * @param string $dataObjectClassName |
||
300 | * @return boolean |
||
301 | */ |
||
302 | protected function classCanBeIncluded(string $dataObjectClassName): bool |
||
314 |