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 namespace Gears\String\Methods; |
||
16 | trait Contains |
||
17 | { |
||
18 | /** |
||
19 | * Returns true if the string contains $needle, false otherwise. |
||
20 | * |
||
21 | * By default the comparison is case-sensitive, but can be made |
||
22 | * insensitive by setting $caseSensitive to false. |
||
23 | * |
||
24 | * @param string $needle Substring to look for. |
||
25 | * |
||
26 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
||
27 | * |
||
28 | * @return bool Whether or not $str contains $needle. |
||
29 | */ |
||
30 | public function contains($needle, $caseSensitive = true) |
||
41 | |||
42 | /** |
||
43 | * Returns true if the string contains all $needles, false otherwise. |
||
44 | * |
||
45 | * By default the comparison is case-sensitive, but can be made |
||
46 | * insensitive by setting $caseSensitive to false. |
||
47 | * |
||
48 | * @param array $needles SubStrings to look for. |
||
49 | * |
||
50 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
||
51 | * |
||
52 | * @return bool Whether or not $str contains $needle. |
||
53 | */ |
||
54 | View Code Duplication | public function containsAll($needles, $caseSensitive = true) |
|
65 | |||
66 | /** |
||
67 | * Returns true if the string contains any $needles, false otherwise. |
||
68 | * |
||
69 | * By default the comparison is case-sensitive, but can be made |
||
70 | * insensitive by setting $caseSensitive to false. |
||
71 | * |
||
72 | * @param array $needles SubStrings to look for. |
||
73 | * |
||
74 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity. |
||
75 | * |
||
76 | * @return bool Whether or not $str contains $needle. |
||
77 | */ |
||
78 | View Code Duplication | public function containsAny($needles, $caseSensitive = true) |
|
89 | } |
||
90 |
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.