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 |
||
24 | class Inflections |
||
25 | { |
||
26 | /** |
||
27 | * @var array<string , Inflections> |
||
28 | */ |
||
29 | static private $inflections = []; |
||
30 | |||
31 | /** |
||
32 | * Returns inflections for the specified locale. |
||
33 | * |
||
34 | * Note: Inflections are shared for the same locale. If you need to alter an instance you |
||
35 | * MUST clone it first, otherwise your changes will affect others. |
||
36 | */ |
||
37 | static public function get(string $locale = INFLECTOR_DEFAULT_LOCALE): Inflections |
||
53 | |||
54 | /** |
||
55 | * Rules for {@link pluralize()}. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $plurals = []; |
||
60 | |||
61 | /** |
||
62 | * Rules for {@link singularize()}. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $singulars = []; |
||
67 | |||
68 | /** |
||
69 | * Uncountables. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $uncountables = []; |
||
74 | |||
75 | /** |
||
76 | * Rules for {@link humanize()}. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $humans = []; |
||
81 | |||
82 | /** |
||
83 | * Acronyms. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $acronyms = []; |
||
88 | |||
89 | /** |
||
90 | * Acronyms regex. |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $acronym_regex = '/(?=a)b/'; |
||
95 | |||
96 | /** |
||
97 | * Returns the {@link $acronyms}, {@link $acronym_regex}, {@link $plurals}, {@link $singulars}, |
||
98 | * {@link $uncountables} and {@link $humans} properties. |
||
99 | * |
||
100 | * @param string $property |
||
101 | * |
||
102 | * @return mixed |
||
103 | * |
||
104 | * @throws PropertyNotDefined in attempt to read an inaccessible property. If the {@link PropertyNotDefined} |
||
105 | * class is not available a {@link \InvalidArgumentException} is thrown instead. |
||
106 | */ |
||
107 | public function __get(string $property) |
||
125 | |||
126 | /** |
||
127 | * Specifies a new acronym. An acronym must be specified as it will appear |
||
128 | * in a camelized string. An underscore string that contains the acronym |
||
129 | * will retain the acronym when passed to {@link camelize}, {@link humanize}, or |
||
130 | * {@link titleize}. A camelized string that contains the acronym will maintain |
||
131 | * the acronym when titleized or humanized, and will convert the acronym |
||
132 | * into a non-delimited single lowercase word when passed to {@link underscore}. |
||
133 | * |
||
134 | * <pre> |
||
135 | * $this->acronym('HTML'); |
||
136 | * $this->titleize('html'); // 'HTML' |
||
137 | * $this->camelize('html'); // 'HTML' |
||
138 | * $this->underscore('MyHTML'); // 'my_html' |
||
139 | * </pre> |
||
140 | * |
||
141 | * The acronym, however, must occur as a delimited unit and not be part of |
||
142 | * another word for conversions to recognize it: |
||
143 | * |
||
144 | * <pre> |
||
145 | * $this->acronym('HTTP'); |
||
146 | * $this->camelize('my_http_delimited'); // 'MyHTTPDelimited' |
||
147 | * $this->camelize('https'); // 'Https', not 'HTTPs' |
||
148 | * $this->underscore('HTTPS'); // 'http_s', not 'https' |
||
149 | * |
||
150 | * $this->acronym('HTTPS'); |
||
151 | * $this->camelize('https'); // 'HTTPS' |
||
152 | * $this->underscore('HTTPS'); // 'https' |
||
153 | * </pre> |
||
154 | * |
||
155 | * Note: Acronyms that are passed to {@link pluralize} will no longer be |
||
156 | * recognized, since the acronym will not occur as a delimited unit in the |
||
157 | * pluralized result. To work around this, you must specify the pluralized |
||
158 | * form as an acronym as well: |
||
159 | * |
||
160 | * <pre> |
||
161 | * $this->acronym('API'); |
||
162 | * $this->camelize($this->pluralize('api')); // 'Apis' |
||
163 | * |
||
164 | * $this->acronym('APIs'); |
||
165 | * $this->camelize($this->pluralize('api')); // 'APIs' |
||
166 | * </pre> |
||
167 | * |
||
168 | * {@link acronym} may be used to specify any word that contains an acronym or |
||
169 | * otherwise needs to maintain a non-standard capitalization. The only |
||
170 | * restriction is that the word must begin with a capital letter. |
||
171 | * |
||
172 | * <pre> |
||
173 | * $this->acronym('RESTful'); |
||
174 | * $this->underscore('RESTful'); // 'restful' |
||
175 | * $this->underscore('RESTfulController'); // 'restful_controller' |
||
176 | * $this->titleize('RESTfulController'); // 'RESTful Controller' |
||
177 | * $this->camelize('restful'); // 'RESTful' |
||
178 | * $this->camelize('restful_controller'); // 'RESTfulController' |
||
179 | * |
||
180 | * $this->acronym('McHammer'); |
||
181 | * $this->underscore('McHammer'); // 'mchammer' |
||
182 | * $this->camelize('mchammer'); // 'McHammer' |
||
183 | * </pre> |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function acronym(string $acronym): self |
||
194 | |||
195 | /** |
||
196 | * Specifies a new pluralization rule and its replacement. |
||
197 | * |
||
198 | * <pre> |
||
199 | * $this->plural('/^(ax|test)is$/i', '\1es'); |
||
200 | * $this->plural('/(buffal|tomat)o$/i', '\1oes'); |
||
201 | * $this->plural('/^(m|l)ouse$/i', '\1ice'); |
||
202 | * </pre> |
||
203 | * |
||
204 | * @param string $rule A regex string. |
||
205 | * @param string $replacement The replacement should always be a string that may include |
||
206 | * references to the matched data from the rule. |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | View Code Duplication | public function plural(string $rule, string $replacement): self |
|
219 | |||
220 | /** |
||
221 | * Specifies a new singularization rule and its replacement. |
||
222 | * |
||
223 | * <pre> |
||
224 | * $this->singular('/(n)ews$/i', '\1ews'); |
||
225 | * $this->singular('/([^aeiouy]|qu)ies$/i', '\1y'); |
||
226 | * $this->singular('/(quiz)zes$/i', '\1'); |
||
227 | * </pre> |
||
228 | * |
||
229 | * @param string $rule A regex string. |
||
230 | * @param string $replacement The replacement should always be a string that may include |
||
231 | * references to the matched data from the rule. |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | View Code Duplication | public function singular(string $rule, string $replacement): self |
|
244 | |||
245 | /** |
||
246 | * Specifies a new irregular that applies to both pluralization and singularization at the |
||
247 | * same time. This can only be used for strings, not regular expressions. You simply pass |
||
248 | * the irregular in singular and plural form. |
||
249 | * |
||
250 | * <pre> |
||
251 | * $this->irregular('child', 'children'); |
||
252 | * $this->irregular('person', 'people'); |
||
253 | * </pre> |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function irregular(string $singular, string $plural): self |
||
296 | |||
297 | /** |
||
298 | * Add uncountable words that shouldn't be attempted inflected. |
||
299 | * |
||
300 | * <pre> |
||
301 | * $this->uncountable('money'); |
||
302 | * $this->uncountable(explode(' ', 'money information rice')); |
||
303 | * </pre> |
||
304 | * |
||
305 | * @param string|array $word |
||
306 | * |
||
307 | * @return $this |
||
308 | */ |
||
309 | public function uncountable($word): self |
||
322 | |||
323 | /** |
||
324 | * Specifies a humanized form of a string by a regular expression rule or by a string mapping. |
||
325 | * When using a regular expression based replacement, the normal humanize formatting is |
||
326 | * called after the replacement. When a string is used, the human form should be specified |
||
327 | * as desired (example: 'The name', not 'the_name'). |
||
328 | * |
||
329 | * <pre> |
||
330 | * $this->human('/_cnt$/i', '\1_count'); |
||
331 | * $this->human('legacy_col_person_name', 'Name'); |
||
332 | * </pre> |
||
333 | * |
||
334 | * @param string $rule A regular expression rule or a string mapping. Strings that starts with |
||
335 | * "/", "#" or "~" are recognized as regular expressions. |
||
336 | * |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function human(string $rule, string $replacement): self |
||
352 | } |
||
353 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.