Total Complexity | 44 |
Total Lines | 316 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 1 |
Complex classes like StrTokenGenerator 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 StrTokenGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class StrTokenGenerator |
||
17 | { |
||
18 | /** @var \Illuminate\Foundation\Application The Laravel application instance. */ |
||
19 | protected $app; |
||
20 | |||
21 | /** @var mixed The Laravel application configs. */ |
||
22 | protected $config; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $text = ''; |
||
26 | |||
27 | /** @var null */ |
||
28 | protected $date = null; |
||
29 | |||
30 | /** @var null */ |
||
31 | protected $entity = null; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $entities = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $vars = []; |
||
38 | |||
39 | /** @var bool */ |
||
40 | protected $clearEmptyTokens = true; |
||
41 | |||
42 | /** |
||
43 | * StrTokenGenerator constructor. |
||
44 | */ |
||
45 | public function __construct($app = null) |
||
46 | { |
||
47 | if (!$app) { |
||
48 | $app = app(); //Fallback when $app is not given |
||
49 | } |
||
50 | $this->app = $app; |
||
51 | |||
52 | $this->config = $this->app['config']; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $text |
||
57 | * @return StrTokenGenerator |
||
58 | */ |
||
59 | public function setText(string $text = ''): self |
||
60 | { |
||
61 | $this->text = $text; |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param Carbon $date |
||
68 | * @return StrTokenGenerator |
||
69 | */ |
||
70 | public function setDate(Carbon $date): self |
||
71 | { |
||
72 | $this->date = $date; |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param Model $entity |
||
79 | * @return StrTokenGenerator |
||
80 | */ |
||
81 | public function setEntity(Model $entity): self |
||
82 | { |
||
83 | $this->entity = $entity; |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param array $entities|Illuminate\Database\Eloquent\Model |
||
90 | * @return \Fomvasss\LaravelStrTokens\StrTokenGenerator |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | public function setEntities(array $entities): self |
||
94 | { |
||
95 | foreach ($entities as $key => $entity) { |
||
96 | $this->ensureValidEntity($entity); |
||
97 | } |
||
98 | |||
99 | $this->entities = $entities; |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param array $vars |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function setVars(array $vars): self |
||
109 | { |
||
110 | $this->vars = $vars; |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string $key |
||
117 | * @param $value |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setVar(string $key, $value): self |
||
121 | { |
||
122 | $this->vars[$key] = $value; |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return StrTokenGenerator |
||
129 | */ |
||
130 | public function doNotClearEmptyTokens(): self |
||
131 | { |
||
132 | $this->clearEmptyTokens = false; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return StrTokenGenerator |
||
139 | */ |
||
140 | public function clearEmptyTokens(): self |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | public function replace(): string |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Token scan with CMS Drupal :) |
||
192 | * https://api.drupal.org/api/drupal/includes%21token.inc/function/token_scan/7.x |
||
193 | * preg_match_all('/\[([^\]:]*):([^\]]*)\]/', $tokenStr, $matches); |
||
194 | * |
||
195 | * @param $text |
||
196 | * @return array |
||
197 | */ |
||
198 | private function tokenScan(string $text): array |
||
199 | { |
||
200 | |||
201 | // Matches tokens with the following pattern: [$type:$name] |
||
202 | // $type and $name may not contain [ ] characters. |
||
203 | // $type may not contain : or whitespace characters, but $name may. |
||
204 | preg_match_all('/ |
||
205 | \\[ # [ - pattern start |
||
206 | ([^\\s\\[\\]:]*) # match $type not containing whitespace : [ or ] |
||
207 | : # : - separator |
||
208 | ([^\\[\\]]*) # match $name not containing [ or ] |
||
209 | \\] # ] - pattern end |
||
210 | /x', $text, $matches); |
||
211 | $types = $matches[1]; |
||
212 | $tokens = $matches[2]; |
||
213 | |||
214 | // Iterate through the matches, building an associative array containing |
||
215 | // $tokens grouped by $types, pointing to the version of the token found in |
||
216 | // the source text. For example, $results['node']['title'] = '[node:title]'; |
||
217 | $results = []; |
||
218 | for ($i = 0; $i < count($tokens); $i++) { |
||
219 | $results[$types[$i]][$tokens[$i]] = $matches[0][$i]; |
||
220 | } |
||
221 | |||
222 | return $results; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param array $tokens |
||
227 | * @param string $type |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function eloquentModelTokens(Model $eloquentModel, array $tokens, string $type): array |
||
231 | { |
||
232 | $replacements = []; |
||
233 | |||
234 | foreach ($tokens as $key => $original) { |
||
235 | $function = explode(':', $key)[0]; |
||
236 | $strTokenMethod = Str::camel('str_token_'.$function); |
||
237 | |||
238 | // Exists token generate method (defined user) |
||
239 | if (method_exists($eloquentModel, $strTokenMethod)) { |
||
240 | |||
241 | $replacements[$original] = $eloquentModel->{$strTokenMethod}($eloquentModel, ...explode(':', $key)); |
||
242 | |||
243 | // Exists relation function (defined user) |
||
244 | } elseif (method_exists($eloquentModel, $function)) { |
||
245 | |||
246 | $newOriginal = str_replace("$type:", '', $original); |
||
247 | |||
248 | if ($eloquentModel->{$function} instanceof Model) { |
||
249 | $tm = new static(); |
||
250 | |||
251 | $replacements[$original] = $tm->setText($newOriginal)->setEntity($eloquentModel->{$function})->replace(); |
||
252 | } elseif ($eloquentModel->{$function} instanceof Collection && ($firstRelatedEntity = $eloquentModel->{$function}->first())) { |
||
253 | $tm = new static(); |
||
254 | |||
255 | $replacements[$original] = $tm->setText($newOriginal)->setEntity($firstRelatedEntity)->replace(); |
||
256 | } |
||
257 | // Is field model |
||
258 | } else { |
||
259 | // TODO: make and check available model fields |
||
260 | $replacements[$original] = $eloquentModel->{$key}; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return $replacements; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param array $tokens |
||
269 | * @return array |
||
270 | */ |
||
271 | protected function configTokens(array $tokens): array |
||
272 | { |
||
273 | $replacements = []; |
||
274 | |||
275 | $disable = $this->config->get('str-tokens.disable_configs', []); |
||
276 | |||
277 | foreach ($tokens as $name => $original) { |
||
278 | if (! Str::is($disable, $name)) { |
||
279 | $res = $this->config->get($name, ''); |
||
280 | $replacements[$original] = is_string($res) ? $res : ''; |
||
281 | } |
||
282 | } |
||
283 | |||
284 | return $replacements; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @param array $tokens |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function varTokens(array $tokens): array |
||
292 | { |
||
293 | $replacements = []; |
||
294 | |||
295 | foreach ($tokens as $name => $original) { |
||
296 | $res = $this->vars[$name] ?? ''; |
||
297 | $replacements[$original] = is_string($res) ? $res : ''; |
||
298 | } |
||
299 | |||
300 | return $replacements; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param $tokens |
||
305 | * @return array |
||
306 | */ |
||
307 | protected function dateTokens(array $tokens):array |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param $entity |
||
326 | * @throws \Exception |
||
327 | */ |
||
328 | protected function ensureValidEntity($entity) |
||
332 | } |
||
333 | } |
||
334 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths