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 |
||
17 | class Str |
||
18 | { |
||
19 | /** |
||
20 | * Determine if a given string starts with a given substring. |
||
21 | * |
||
22 | * @param string $haystack |
||
23 | * @param array $needles Array of string needles |
||
24 | * |
||
25 | * @return bool |
||
26 | */ |
||
27 | 32 | View Code Duplication | public static function startsWith(string $haystack, array $needles = []) : bool |
37 | |||
38 | /** |
||
39 | * Determine if a given string ends with a given substring. |
||
40 | * |
||
41 | * @param string $haystack |
||
42 | * @param array $needles Array of string needles |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | 32 | View Code Duplication | public static function endsWith(string $haystack, array $needles = []) : bool |
56 | |||
57 | /** |
||
58 | * Determine if a given string starts and ends with a given substring. |
||
59 | * |
||
60 | * @param string $haystack |
||
61 | * @param array $needles |
||
62 | * @return bool |
||
63 | */ |
||
64 | 20 | public static function startsEndsWith(string $haystack, array $needles = []) : bool |
|
74 | } |
||
75 |
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.