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, $ResourceSets, $ends) = $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 | //one-to-many |
||
| 89 | $meta->addResourceReferencePropertyBidirectional( |
||
| 90 | $principal, |
||
| 91 | $dependent, |
||
| 92 | $principalProp, |
||
| 93 | $dependentProp |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | $key = 'metadata'; |
||
| 98 | $this->handlePostBoot($isCaching, $hasCache, $key, $meta); |
||
| 99 | } |
||
| 100 | |||
| 101 | private static function setupRoute() |
||
| 102 | { |
||
| 103 | $valueArray = []; |
||
| 104 | |||
| 105 | Route::any('odata.svc/{section}', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index') |
||
| 106 | ->where(['section' => '.*']); |
||
| 107 | Route::any('odata.svc', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index'); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Register the application services. Boot-time only. |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function register() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | protected function getCandidateModels() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $meta |
||
| 142 | * @param $ends |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | protected function getEntityTypesAndResourceSets($meta, $ends) |
||
| 173 | |||
| 174 | public function calculateRoundTripRelations() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param $remix |
||
| 210 | * @param $lines |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | private function calculateRoundTripRelationsSecondPass($remix, $lines) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $hooks |
||
| 265 | * @param $lines |
||
| 266 | * @param $remix |
||
| 267 | */ |
||
| 268 | private function calculateRoundTripRelationsFirstPass($hooks, &$lines, &$remix) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param $principalType |
||
| 327 | * @param $principalMult |
||
| 328 | * @param $principalProperty |
||
| 329 | * @param $dependentType |
||
| 330 | * @param $dependentMult |
||
| 331 | * @param $dependentProperty |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | private function calculateRoundTripRelationsGenForwardReverse( |
||
| 360 | } |
||
| 361 |
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.