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 | 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 | static public function endsWith($haystack, $needles) |
||
| 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 | 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 | 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 | View Code Duplication | static public function getAfter($string, $phrase) |
|
| 172 | |||
| 173 | } |