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 |
||
20 | class Str |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * Does the string start with? |
||
25 | * @param $haystack |
||
26 | * @param $needles |
||
27 | * @return bool|string if an array is passed in the matching string will be returned, else true/false |
||
28 | */ |
||
29 | 5 | static public function startsWith($haystack, $needles) |
|
48 | |||
49 | /** |
||
50 | * Does the string end with? |
||
51 | * @param $haystack |
||
52 | * @param $needles |
||
53 | * @return array|bool if an array is passed in the matching string will be returned, else true/false |
||
54 | */ |
||
55 | 3 | static public function endsWith($haystack, $needles) |
|
56 | { |
||
57 | 3 | if ($haystack === null) { |
|
58 | return false; |
||
59 | } |
||
60 | 3 | if (is_array($needles)) { |
|
61 | 1 | foreach ((array)$needles as $needle) { |
|
62 | 1 | View Code Duplication | if (($temp = strlen($haystack) - strlen( |
63 | $needle |
||
64 | 1 | )) >= 0 && strpos( |
|
65 | 1 | $haystack, |
|
66 | 1 | $needle, |
|
67 | $temp |
||
68 | 1 | ) !== false |
|
69 | 1 | ) { |
|
70 | 1 | return $needle; |
|
71 | } |
||
72 | 1 | } |
|
73 | 1 | View Code Duplication | } else { |
74 | 3 | if (($temp = strlen($haystack) - strlen($needles)) >= 0 && strpos( |
|
75 | 3 | $haystack, |
|
76 | 3 | $needles, |
|
77 | $temp |
||
78 | 3 | ) !== false |
|
79 | 3 | ) { |
|
80 | 2 | return true; |
|
81 | } |
||
82 | } |
||
83 | |||
84 | 3 | return false; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Does the haystack contain needles? If array in will return array out or false |
||
89 | * @param $haystack |
||
90 | * @param $needles |
||
91 | * @return array|bool if an array is passed in the matching string will be returned, else true/false |
||
92 | */ |
||
93 | 1 | static public function contains($haystack, $needles) |
|
119 | |||
120 | /** |
||
121 | * Get part of a string before a phrase or array of phrases. Returns an array of results keyed by phrase |
||
122 | * Making recursive true makes this work like tokenizing the string |
||
123 | * You can preserve the phrase by marking the variable true |
||
124 | * output array is in the form of |
||
125 | * $data[$phrase][$index] where index is a numeric value |
||
126 | * @param $string |
||
127 | * @param $phrase string|[string] |
||
128 | * @return array |
||
129 | */ |
||
130 | 1 | View Code Duplication | static public function getBefore($string, $phrase) |
147 | |||
148 | /** |
||
149 | * Get part of a string after a phrase or array of phrases. Returns an array of results keyed by phrase |
||
150 | * If no instances of the key were found returns false |
||
151 | * The last member of the array will be blank if the token is the last thing in the string |
||
152 | * @param $string |
||
153 | * @param $phrase string|[string] |
||
154 | * @return array |
||
155 | */ |
||
156 | 1 | View Code Duplication | static public function getAfter($string, $phrase) |
172 | |||
173 | } |