| Conditions | 1 |
| Paths | 1 |
| Total Lines | 51 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 122 | public function slugify($str) |
||
| 123 | { |
||
| 124 | // Character options |
||
| 125 | $separator = '_'; |
||
| 126 | $punctuation_modifier = $separator; |
||
| 127 | |||
| 128 | // Punctuation |
||
| 129 | $punctuation_characters = ['&', '%', '?', ')', '(', '\\', '"', "'", ':', '#', '.', ',', ';', '!']; |
||
| 130 | |||
| 131 | // Unescaped HTML characters string |
||
| 132 | $unescaped_html_characters = '/&(raquo|laquo|rsaquo|lsaquo|rdquo|ldquo|rsquo|lsquo|hellip|amp|nbsp|quot|ordf|ordm);/'; |
||
| 133 | |||
| 134 | $separator = '-'; |
||
| 135 | |||
| 136 | $slug = preg_replace('/[^\p{L}\s]/u', '-', $str); |
||
| 137 | |||
| 138 | $slug = mb_strtolower($slug, 'UTF-8'); |
||
| 139 | |||
| 140 | // Strip HTML |
||
| 141 | $slug = strip_tags($slug); |
||
| 142 | |||
| 143 | // Strip Whitespace |
||
| 144 | $slug = trim($slug); |
||
| 145 | |||
| 146 | // Remove diacritics |
||
| 147 | $slug = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde|cedil|ring);/', '$1', htmlentities($slug, ENT_COMPAT, 'UTF-8')); |
||
| 148 | |||
| 149 | // Remove unescaped HTML characters |
||
| 150 | $slug = preg_replace($unescaped_html_characters, '', $slug); |
||
| 151 | |||
| 152 | // Get rid of punctuation |
||
| 153 | $slug = str_replace($punctuation_characters, $separator, $slug); |
||
| 154 | |||
| 155 | // Post-cleanup, get rid of spaces, repeating dash symbols symbols and surround whitespace/separators |
||
| 156 | $slug = trim($slug); |
||
| 157 | |||
| 158 | // Replace whitespace by seperator |
||
| 159 | $slug = preg_replace('/\s+/', $separator, $slug); |
||
| 160 | |||
| 161 | // Squeeze multiple dashes or underscores |
||
| 162 | $slug = preg_replace('/[-_]{2,}/', '-', $slug); |
||
| 163 | |||
| 164 | // Strip leading and trailing dashes or underscores |
||
| 165 | $slug = trim($slug, '-_'); |
||
| 166 | |||
| 167 | // Finally, remove all whitespace |
||
| 168 | $slug = preg_replace('/[_]+/', $separator, $slug); |
||
| 169 | //$slug = str_replace('_', $separator, $slug); |
||
| 170 | |||
| 171 | return $slug; |
||
| 172 | } |
||
| 173 | } |
||
| 174 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..