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:
Complex classes like RoutableTrait 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 RoutableTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | trait RoutableTrait |
||
26 | { |
||
27 | /** |
||
28 | * The object's route. |
||
29 | * |
||
30 | * @var TranslationString|string|null |
||
31 | */ |
||
32 | protected $slug; |
||
33 | |||
34 | /** |
||
35 | * Whether the slug is editable. |
||
36 | * |
||
37 | * If FALSE, the slug is always auto-generated from its pattern. |
||
38 | * If TRUE, the slug is auto-generated only if the slug is empty. |
||
39 | * |
||
40 | * @var boolean|null |
||
41 | */ |
||
42 | private $isSlugEditable; |
||
43 | |||
44 | /** |
||
45 | * The object's route pattern. |
||
46 | * |
||
47 | * @var TranslationString|string|null |
||
48 | */ |
||
49 | private $slugPattern = ''; |
||
50 | |||
51 | /** |
||
52 | * A prefix for the object's route. |
||
53 | * |
||
54 | * @var TranslationString|string|null |
||
55 | */ |
||
56 | private $slugPrefix = ''; |
||
57 | |||
58 | /** |
||
59 | * A suffix for the object's route. |
||
60 | * |
||
61 | * @var TranslationString|string|null |
||
62 | */ |
||
63 | private $slugSuffix = ''; |
||
64 | |||
65 | /** |
||
66 | * Latest ObjectRoute object concerning the current object. |
||
67 | * |
||
68 | * @var ObjectRouteInterface |
||
69 | */ |
||
70 | private $latestObjectRoute; |
||
71 | |||
72 | /** |
||
73 | * The class name of the object route model. |
||
74 | * |
||
75 | * Must be a fully-qualified PHP namespace and an implementation of |
||
76 | * {@see \Charcoal\Object\ObjectRouteInterface}. Used by the model factory. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $objectRouteClass = ObjectRoute::class; |
||
81 | |||
82 | /** |
||
83 | * Set the object's URL slug pattern. |
||
84 | * |
||
85 | * @param mixed $pattern The slug pattern. |
||
86 | * @return RoutableInterface Chainable |
||
87 | */ |
||
88 | public function setSlugPattern($pattern) |
||
89 | { |
||
90 | if (TranslationString::isTranslatable($pattern)) { |
||
91 | $this->slugPattern = new TranslationString($pattern); |
||
92 | } else { |
||
93 | $this->slugPattern = null; |
||
94 | } |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Retrieve the object's URL slug pattern. |
||
101 | * |
||
102 | * @throws InvalidArgumentException If a slug pattern is not defined. |
||
103 | * @return TranslationString|null |
||
104 | */ |
||
105 | public function slugPattern() |
||
106 | { |
||
107 | if (!$this->slugPattern) { |
||
108 | $metadata = $this->metadata(); |
||
|
|||
109 | |||
110 | if (isset($metadata['routable']['pattern'])) { |
||
111 | $this->setSlugPattern($metadata['routable']['pattern']); |
||
112 | } elseif (isset($metadata['slug_pattern'])) { |
||
113 | $this->setSlugPattern($metadata['slug_pattern']); |
||
114 | } else { |
||
115 | throw new InvalidArgumentException( |
||
116 | sprintf('Undefined route pattern (slug) for %s', get_called_class()) |
||
117 | ); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | return $this->slugPattern; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Retrieve route prefix for the object's URL slug pattern. |
||
126 | * |
||
127 | * @return TranslationString|null |
||
128 | */ |
||
129 | View Code Duplication | public function slugPrefix() |
|
130 | { |
||
131 | if (!$this->slugPrefix) { |
||
132 | $metadata = $this->metadata(); |
||
133 | |||
134 | if (isset($metadata['routable']['prefix'])) { |
||
135 | $affix = $metadata['routable']['prefix']; |
||
136 | |||
137 | if (TranslationString::isTranslatable($affix)) { |
||
138 | $this->slugPrefix = new TranslationString($affix); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return $this->slugPrefix; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Retrieve route suffix for the object's URL slug pattern. |
||
148 | * |
||
149 | * @return TranslationString|null |
||
150 | */ |
||
151 | View Code Duplication | public function slugSuffix() |
|
152 | { |
||
153 | if (!$this->slugSuffix) { |
||
154 | $metadata = $this->metadata(); |
||
155 | |||
156 | if (isset($metadata['routable']['suffix'])) { |
||
157 | $affix = $metadata['routable']['suffix']; |
||
158 | |||
159 | if (TranslationString::isTranslatable($affix)) { |
||
160 | $this->slugSuffix = new TranslationString($affix); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $this->slugSuffix; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Determine if the slug is editable. |
||
170 | * |
||
171 | * @return boolean |
||
172 | */ |
||
173 | public function isSlugEditable() |
||
174 | { |
||
175 | if ($this->isSlugEditable === null) { |
||
176 | $metadata = $this->metadata(); |
||
177 | |||
178 | if (isset($metadata['routable']['editable'])) { |
||
179 | $this->isSlugEditable = !!$metadata['routable']['editable']; |
||
180 | } else { |
||
181 | $this->isSlugEditable = false; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return $this->isSlugEditable; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Set the object's URL slug. |
||
190 | * |
||
191 | * @param mixed $slug The slug. |
||
192 | * @return RoutableInterface Chainable |
||
193 | */ |
||
194 | public function setSlug($slug) |
||
195 | { |
||
196 | if (TranslationString::isTranslatable($slug)) { |
||
197 | $this->slug = new TranslationString($slug); |
||
198 | |||
199 | $values = $this->slug->all(); |
||
200 | foreach ($values as $lang => $val) { |
||
201 | $this->slug[$lang] = $this->slugify($val); |
||
202 | } |
||
203 | } else { |
||
204 | /** @todo Hack used for regenerating route */ |
||
205 | if (isset($_POST['slug'])) { |
||
206 | $this->slug = []; |
||
207 | } else { |
||
208 | $this->slug = null; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Retrieve the object's URL slug. |
||
217 | * |
||
218 | * @return TranslationString|null |
||
219 | */ |
||
220 | public function slug() |
||
221 | { |
||
222 | return $this->slug; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Generate a URL slug from the object's URL slug pattern. |
||
227 | * |
||
228 | * @throws UnexpectedValueException If the slug is empty. |
||
229 | * @return TranslationString |
||
230 | */ |
||
231 | public function generateSlug() |
||
232 | { |
||
233 | $translator = TranslationConfig::instance(); |
||
234 | $languages = $translator->availableLanguages(); |
||
235 | $patterns = $this->slugPattern(); |
||
236 | $curSlug = $this->slug(); |
||
237 | $newSlug = new TranslationString(); |
||
238 | |||
239 | $origLang = $translator->currentLanguage(); |
||
240 | foreach ($languages as $lang) { |
||
241 | $pattern = $patterns[$lang]; |
||
242 | |||
243 | $translator->setCurrentLanguage($lang); |
||
244 | if ($this->isSlugEditable() && isset($curSlug[$lang]) && strlen($curSlug[$lang])) { |
||
245 | $newSlug[$lang] = $curSlug[$lang]; |
||
246 | } else { |
||
247 | $newSlug[$lang] = $this->generateRoutePattern($pattern); |
||
248 | if (!strlen($newSlug[$lang])) { |
||
249 | throw new UnexpectedValueException( |
||
250 | sprintf('The slug is empty. The pattern is "%s"', $pattern) |
||
251 | ); |
||
252 | } |
||
253 | } |
||
254 | $newSlug[$lang] = $this->finalizeSlug($newSlug[$lang]); |
||
255 | |||
256 | $objectRoute = $this->createRouteObject(); |
||
257 | if ($objectRoute->source()->tableExists()) { |
||
258 | $objectRoute->setData([ |
||
259 | 'lang' => $lang, |
||
260 | 'slug' => $newSlug[$lang], |
||
261 | 'route_obj_type' => $this->objType(), |
||
262 | 'route_obj_id' => $this->id() |
||
263 | ]); |
||
264 | |||
265 | if (!$objectRoute->isSlugUnique()) { |
||
266 | $objectRoute->generateUniqueSlug(); |
||
267 | $newSlug[$lang] = $objectRoute->slug(); |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | $translator->setCurrentLanguage($origLang); |
||
272 | |||
273 | return $newSlug; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Generate a route from the given pattern. |
||
278 | * |
||
279 | * @uses self::parseRouteToken() If a view renderer is unavailable. |
||
280 | * @param string $pattern The slug pattern. |
||
281 | * @return string Returns the generated route. |
||
282 | */ |
||
283 | protected function generateRoutePattern($pattern) |
||
293 | |||
294 | /** |
||
295 | * Parse the given slug (URI token) for the current object. |
||
296 | * |
||
297 | * @used-by self::generateRoutePattern() If a view renderer is unavailable. |
||
298 | * @uses self::filterRouteToken() For customize the route value filtering, |
||
299 | * @param string|array $token The token to parse relative to the model entry. |
||
300 | * @throws InvalidArgumentException If a route token is not a string. |
||
301 | * @return string |
||
302 | */ |
||
303 | protected function parseRouteToken($token) |
||
335 | |||
336 | /** |
||
337 | * Filter the given value for a URI. |
||
338 | * |
||
339 | * @used-by self::parseRouteToken() To resolve the token's value. |
||
340 | * @param mixed $value A value to filter. |
||
341 | * @param string $token The parsed token. |
||
342 | * @return string The filtered $value. |
||
343 | */ |
||
344 | protected function filterRouteToken($value, $token = null) |
||
358 | |||
359 | /** |
||
360 | * Route generation. |
||
361 | * |
||
362 | * Saves all routes to {@see \Charcoal\Object\ObjectRoute}. |
||
363 | * |
||
364 | * @param mixed $slug Slug by langs. |
||
365 | * @return void |
||
366 | */ |
||
367 | protected function generateObjectRoute($slug = null) |
||
423 | |||
424 | /** |
||
425 | * Retrieve the latest object route. |
||
426 | * |
||
427 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
428 | * @throws InvalidArgumentException If the given language is invalid. |
||
429 | * @return ObjectRouteInterface Latest object route. |
||
430 | */ |
||
431 | protected function getLatestObjectRoute($lang = null) |
||
491 | |||
492 | /** |
||
493 | * Sync the object routes with the object's data. |
||
494 | * @return void |
||
495 | */ |
||
496 | public function syncObjectRoutes() |
||
497 | { |
||
498 | $model = $this->createRouteObject(); |
||
499 | |||
523 | |||
524 | /** |
||
525 | * Retrieve the object's URI. |
||
526 | * |
||
527 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
528 | * @return TranslationString|string |
||
529 | */ |
||
530 | public function url($lang = null) |
||
545 | |||
546 | /** |
||
547 | * Convert a string into a slug. |
||
548 | * |
||
549 | * @param string $str The string to slugify. |
||
550 | * @return string The slugified string. |
||
551 | */ |
||
552 | public function slugify($str) |
||
612 | |||
613 | /** |
||
614 | * Finalize slug. |
||
615 | * |
||
616 | * Adds any prefix and suffix defined in the routable configuration set. |
||
617 | * |
||
618 | * @param string $slug A slug. |
||
619 | * @throws UnexpectedValueException If the slug affixes are invalid. |
||
620 | * @return string |
||
621 | */ |
||
622 | protected function finalizeSlug($slug) |
||
644 | |||
645 | /** |
||
646 | * Delete all object routes. |
||
647 | * |
||
648 | * Should be called on object deletion {@see \Charcoal\Model\AbstractModel::preDelete()}. |
||
649 | * |
||
650 | * @return boolean Success or failure. |
||
651 | */ |
||
652 | protected function deleteObjectRoutes() |
||
680 | |||
681 | /** |
||
682 | * Create a route object. |
||
683 | * |
||
684 | * @return ObjectRouteInterface |
||
685 | */ |
||
686 | public function createRouteObject() |
||
692 | |||
693 | /** |
||
694 | * Set the class name of the object route model. |
||
695 | * |
||
696 | * @param string $className The class name of the object route model. |
||
697 | * @throws InvalidArgumentException If the class name is not a string. |
||
698 | * @return AbstractPropertyDisplay Chainable |
||
699 | */ |
||
700 | protected function setObjectRouteClass($className) |
||
712 | |||
713 | /** |
||
714 | * Retrieve the class name of the object route model. |
||
715 | * |
||
716 | * @return string |
||
717 | */ |
||
718 | public function objectRouteClass() |
||
722 | |||
723 | /** |
||
724 | * Retrieve the object model factory. |
||
725 | * |
||
726 | * @return \Charcoal\Factory\FactoryInterface |
||
727 | */ |
||
728 | abstract public function modelFactory(); |
||
729 | |||
730 | /** |
||
731 | * Retrieve the routable object's template identifier. |
||
732 | * |
||
733 | * @return mixed |
||
734 | */ |
||
735 | abstract public function templateIdent(); |
||
736 | } |
||
737 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.