Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
13 | class MetadataProvider extends ServiceProvider |
||
14 | { |
||
15 | protected static $METANAMESPACE = "Data"; |
||
16 | |||
17 | /** |
||
18 | * Bootstrap the application services. Post-boot. |
||
19 | * |
||
20 | 1 | * @return void |
|
21 | */ |
||
22 | 1 | public function boot() |
|
23 | { |
||
24 | self::$METANAMESPACE = env('ODataMetaNamespace', 'Data'); |
||
25 | 1 | // If we aren't migrated, there's no DB tables to pull metadata _from_, so bail out early |
|
26 | try { |
||
27 | if (!Schema::hasTable('migrations')) { |
||
28 | 1 | return; |
|
29 | 1 | } |
|
30 | } catch (\Exception $e) { |
||
31 | return; |
||
32 | } |
||
33 | |||
34 | self::setupRoute(); |
||
35 | $isCaching = true === $this->checkIsCaching(); |
||
36 | $hasCache = Cache::has('metadata'); |
||
37 | |||
38 | if ($isCaching && $hasCache) { |
||
39 | $meta = Cache::get('metadata'); |
||
40 | App::instance('metadata', $meta); |
||
1 ignored issue
–
show
|
|||
41 | return; |
||
42 | } |
||
43 | $meta = App::make('metadata'); |
||
44 | |||
45 | $modelNames = $this->getCandidateModels(); |
||
46 | |||
47 | list($EntityTypes, $ResourceSets, $ends) = $this->getEntityTypesAndResourceSets($meta, $modelNames); |
||
48 | |||
49 | // now that endpoints are hooked up, tackle the relationships |
||
50 | // if we'd tried earlier, we'd be guaranteed to try to hook a relation up to null, which would be bad |
||
51 | foreach ($ends as $bitter) { |
||
52 | $fqModelName = $bitter; |
||
53 | $instance = new $fqModelName(); |
||
54 | $instance->hookUpRelationships($EntityTypes, $ResourceSets); |
||
55 | } |
||
56 | View Code Duplication | if ($isCaching) { |
|
57 | if (!$hasCache) { |
||
58 | $cacheTime = env('APP_METADATA_CACHE_DURATION', null); |
||
59 | $cacheTime = !is_numeric($cacheTime) ? 10 : abs($cacheTime); |
||
60 | Cache::put('metadata', $meta, $cacheTime); |
||
61 | } |
||
62 | } else { |
||
63 | Cache::forget('metadata'); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | private static function setupRoute() |
||
75 | |||
76 | /** |
||
77 | * Register the application services. Boot-time only. |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public function register() |
||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | */ |
||
91 | protected function getCandidateModels() |
||
112 | |||
113 | /** |
||
114 | * @param $meta |
||
115 | * @param $ends |
||
116 | * @return array |
||
117 | */ |
||
118 | protected function getEntityTypesAndResourceSets($meta, $ends) |
||
145 | |||
146 | /** |
||
147 | * @return mixed |
||
148 | */ |
||
149 | protected function checkIsCaching() |
||
153 | } |
||
154 |
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.