Total Complexity | 62 |
Total Lines | 340 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 0 |
Complex classes like SeoManagerTrait 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 SeoManagerTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait SeoManagerTrait |
||
16 | { |
||
17 | protected $exceptRoutes = [ |
||
18 | 'api', |
||
19 | 'telescope', |
||
20 | '_debugbar' |
||
21 | ]; |
||
22 | |||
23 | protected $exceptColumns = [ |
||
24 | "password", |
||
25 | "remember_token", |
||
26 | ]; |
||
27 | |||
28 | public function __construct() |
||
29 | { |
||
30 | $this->exceptRoutes = array_merge($this->exceptRoutes, config('seo-manager.except_routes')); |
||
|
|||
31 | $this->exceptColumns = array_merge($this->exceptColumns, config('seo-manager.except_columns')); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Detect Parameters from the URI |
||
36 | * @param $uri |
||
37 | * @return mixed |
||
38 | */ |
||
39 | private function getParamsFromURI($uri) |
||
40 | { |
||
41 | preg_match_all('/{(.*?)}/', $uri, $output_array); |
||
42 | |||
43 | return $output_array[1]; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Check if the given data is URI param |
||
48 | * @param $param |
||
49 | * @return bool |
||
50 | */ |
||
51 | private function isParam($param) |
||
52 | { |
||
53 | $pattern_params = '/{(.*?)}/'; |
||
54 | preg_match_all($pattern_params, $param, $output_params); |
||
55 | if (!empty($output_params[1])) { |
||
56 | return true; |
||
57 | } |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Check if the given data is Title |
||
63 | * @param $param |
||
64 | * @return bool |
||
65 | */ |
||
66 | private function isTitle($param) |
||
67 | { |
||
68 | $pattern_title = '/~(.*?)~/'; |
||
69 | preg_match_all($pattern_title, $param, $pattern_title); |
||
70 | if (!empty($pattern_title[1])) { |
||
71 | return true; |
||
72 | } |
||
73 | return false; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Remove unnecessary characters from param |
||
78 | * @param $param |
||
79 | * @return string |
||
80 | */ |
||
81 | private function cleanParam($param) |
||
82 | { |
||
83 | return strtolower(str_replace(['{', '}'], '', $param)); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Remove routes which shouldn't be imported to Seo Manager |
||
88 | * @return array |
||
89 | */ |
||
90 | private function cleanRoutes() |
||
91 | { |
||
92 | $routes = \Route::getRoutes(); |
||
93 | $getRoutes = array_keys($routes->get('GET')); |
||
94 | foreach ($getRoutes as $key => $route) { |
||
95 | foreach ($this->exceptRoutes as $rule) { |
||
96 | if (strpos($route, $rule) !== FALSE) { |
||
97 | unset($getRoutes[$key]); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | return $getRoutes; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | * @throws \ReflectionException |
||
107 | */ |
||
108 | private function getAllModels() |
||
109 | { |
||
110 | $path = base_path('app') . '/' . config('seo-manager.models_path'); |
||
111 | |||
112 | $models = File::allFiles($path); |
||
113 | $cleanModelNames = []; |
||
114 | foreach ($models as $model) { |
||
115 | $modelPath = $this->cleanFilePath($model); |
||
116 | $reflectionClass =(new \ReflectionClass($modelPath))->getParentClass(); |
||
117 | if($reflectionClass !== false){ |
||
118 | if($reflectionClass->getName() === "Illuminate\Database\Eloquent\Model" || $reflectionClass->getName() === "Illuminate\Foundation\Auth\User"){ |
||
119 | $cleanModel = [ |
||
120 | 'path' => $modelPath, |
||
121 | 'name' => str_replace('.php', '', $model->getFilename()) |
||
122 | ]; |
||
123 | array_push($cleanModelNames, $cleanModel); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | return $cleanModelNames; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Get Model all Columns |
||
132 | * @param $model |
||
133 | * @return array |
||
134 | */ |
||
135 | public function getColumns($model) |
||
136 | { |
||
137 | $appends = []; |
||
138 | if (method_exists((new $model), 'getAppends')) { |
||
139 | $appends = (new $model)->getAppends(); |
||
140 | } |
||
141 | $table = (new $model)->getTable(); |
||
142 | $columns = $this->getCleanColumns(Schema::getColumnListing($table)); |
||
143 | return array_merge($columns, $appends); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Clean model file path |
||
148 | * @param $file |
||
149 | * @return string |
||
150 | */ |
||
151 | private function cleanFilePath($file) |
||
152 | { |
||
153 | return '\\' . ucfirst(str_replace('/', '\\', substr($file, strpos($file, 'app'), -4))); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Remove unnecessary columns from table columns list |
||
158 | * @param $columns |
||
159 | * @return array |
||
160 | */ |
||
161 | private function getCleanColumns($columns) |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Import Routes to SeoManager database |
||
168 | * @return array|\Illuminate\Database\Eloquent\Collection|static[] |
||
169 | */ |
||
170 | private function importRoutes() |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get mapped Seo Data from Database for Current Route |
||
193 | * @param $property |
||
194 | * @return mixed |
||
195 | */ |
||
196 | private function getMetaData($property) |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Get dynamic title based on user configs for current route |
||
251 | * @param $params |
||
252 | * @param $manager |
||
253 | * @param $routeParams |
||
254 | * @return string |
||
255 | */ |
||
256 | private function getDynamicTitle($params, $manager, $routeParams = null) |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get Open Graph Dynamic Data |
||
295 | * @param $seoManager |
||
296 | * @param $routeParams |
||
297 | * @return array |
||
298 | */ |
||
299 | private function getOgData($seoManager, $routeParams) |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get Open Graph Data Values based on Mapped Params |
||
333 | * @param $value |
||
334 | * @param $manager |
||
335 | * @param $routeParams |
||
336 | * @return mixed |
||
337 | */ |
||
338 | private function getMappedValue($value, $manager, $routeParams) |
||
357 |