Complex classes like MetadataProvider 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 MetadataProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class MetadataProvider extends MetadataBaseProvider |
||
15 | { |
||
16 | protected $multConstraints = [ '0..1' => ['1'], '1' => ['0..1', '*'], '*' => ['1', '*']]; |
||
17 | protected static $METANAMESPACE = "Data"; |
||
18 | |||
19 | /** |
||
20 | 1 | * Bootstrap the application services. Post-boot. |
|
21 | * |
||
22 | 1 | * @return void |
|
23 | */ |
||
24 | public function boot() |
||
25 | 1 | { |
|
26 | self::$METANAMESPACE = env('ODataMetaNamespace', 'Data'); |
||
27 | // If we aren't migrated, there's no DB tables to pull metadata _from_, so bail out early |
||
28 | 1 | try { |
|
29 | 1 | if (!Schema::hasTable('migrations')) { |
|
30 | return; |
||
31 | } |
||
32 | } catch (\Exception $e) { |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | self::setupRoute(); |
||
37 | $isCaching = true === $this->getIsCaching(); |
||
38 | $hasCache = Cache::has('metadata'); |
||
39 | |||
40 | if ($isCaching && $hasCache) { |
||
41 | $meta = Cache::get('metadata'); |
||
42 | App::instance('metadata', $meta); |
||
1 ignored issue
–
show
|
|||
43 | return; |
||
44 | } |
||
45 | $meta = App::make('metadata'); |
||
46 | |||
47 | $modelNames = $this->getCandidateModels(); |
||
48 | |||
49 | list($EntityTypes) = $this->getEntityTypesAndResourceSets($meta, $modelNames); |
||
50 | |||
51 | // need to lift EntityTypes |
||
52 | $biDirect = $this->calculateRoundTripRelations(); |
||
53 | |||
54 | // now that endpoints are hooked up, tackle the relationships |
||
55 | // if we'd tried earlier, we'd be guaranteed to try to hook a relation up to null, which would be bad |
||
56 | foreach ($biDirect as $line) { |
||
57 | $principalType = $line['principalType']; |
||
58 | $principalMult = $line['principalMult']; |
||
59 | $principalProp = $line['principalProp']; |
||
60 | $dependentType = $line['dependentType']; |
||
61 | $dependentMult = $line['dependentMult']; |
||
62 | $dependentProp = $line['dependentProp']; |
||
63 | if (!isset($EntityTypes[$principalType]) || !isset($EntityTypes[$dependentType])) { |
||
64 | continue; |
||
65 | } |
||
66 | $principal = $EntityTypes[$principalType]; |
||
67 | $dependent = $EntityTypes[$dependentType]; |
||
68 | //many-to-many |
||
69 | if ('*' == $principalMult && '*' == $dependentMult) { |
||
70 | $meta->addResourceSetReferencePropertyBidirectional( |
||
71 | $principal, |
||
72 | $dependent, |
||
73 | $principalProp, |
||
74 | $dependentProp |
||
75 | ); |
||
76 | continue; |
||
77 | } |
||
78 | //one-to-one |
||
79 | if ('0..1' == $principalMult || '0..1' == $dependentMult) { |
||
80 | $meta->addResourceReferenceSinglePropertyBidirectional( |
||
81 | $principal, |
||
82 | $dependent, |
||
83 | $principalProp, |
||
84 | $dependentProp |
||
85 | ); |
||
86 | continue; |
||
87 | } |
||
88 | //principal-one-to-dependent-many |
||
89 | if ('*' == $principalMult) { |
||
90 | $meta->addResourceReferencePropertyBidirectional( |
||
91 | $principal, |
||
92 | $dependent, |
||
93 | $principalProp, |
||
94 | $dependentProp |
||
95 | ); |
||
96 | continue; |
||
97 | } |
||
98 | //dependent-one-to-principal-many |
||
99 | $meta->addResourceReferencePropertyBidirectional( |
||
100 | $dependent, |
||
101 | $principal, |
||
102 | $dependentProp, |
||
103 | $principalProp |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | $key = 'metadata'; |
||
108 | $this->handlePostBoot($isCaching, $hasCache, $key, $meta); |
||
109 | } |
||
110 | |||
111 | private static function setupRoute() |
||
119 | |||
120 | /** |
||
121 | * Register the application services. Boot-time only. |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | public function register() |
||
131 | |||
132 | /** |
||
133 | * @return array |
||
134 | */ |
||
135 | protected function getCandidateModels() |
||
149 | |||
150 | /** |
||
151 | * @param $meta |
||
152 | * @param $ends |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function getEntityTypesAndResourceSets($meta, $ends) |
||
183 | |||
184 | public function calculateRoundTripRelations() |
||
217 | |||
218 | /** |
||
219 | * @param $remix |
||
220 | * @param $lines |
||
221 | * @return array |
||
222 | */ |
||
223 | private function calculateRoundTripRelationsSecondPass($remix, $lines) |
||
272 | |||
273 | /** |
||
274 | * @param $hooks |
||
275 | * @param $lines |
||
276 | * @param $remix |
||
277 | */ |
||
278 | private function calculateRoundTripRelationsFirstPass($hooks, &$lines, &$remix) |
||
334 | |||
335 | /** |
||
336 | * @param $principalType |
||
337 | * @param $principalMult |
||
338 | * @param $principalProperty |
||
339 | * @param $dependentType |
||
340 | * @param $dependentMult |
||
341 | * @param $dependentProperty |
||
342 | * @return array |
||
343 | */ |
||
344 | private function calculateRoundTripRelationsGenForwardReverse( |
||
370 | } |
||
371 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.