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 StartEndWith |
||
17 | { |
||
18 | /** |
||
19 | * Returns true if the string begins with $substring, false otherwise. |
||
20 | * |
||
21 | * By default, the comparison is case-sensitive, |
||
22 | * but can be made insensitive by setting $caseSensitive |
||
23 | * to false. |
||
24 | * |
||
25 | * @param string $substring The substring to look for |
||
26 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
27 | * |
||
28 | * @return bool Whether or not $str starts with $substring |
||
29 | */ |
||
30 | public function startsWith($substring, $caseSensitive = true) |
||
48 | |||
49 | /** |
||
50 | * Returns true if the string ends with $substring, false otherwise. By |
||
51 | * default, the comparison is case-sensitive, but can be made insensitive |
||
52 | * by setting $caseSensitive to false. |
||
53 | * |
||
54 | * @param string $substring The substring to look for |
||
55 | * @param bool $caseSensitive Whether or not to enforce case-sensitivity |
||
56 | * |
||
57 | * @return bool Whether or not $str ends with $substring |
||
58 | */ |
||
59 | public function endsWith($substring, $caseSensitive = true) |
||
79 | } |
||
80 |
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.