| Conditions | 10 |
| Paths | 96 |
| Total Lines | 29 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
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 |
||
| 31 | public function generate($params) |
||
| 32 | { |
||
| 33 | // Detect sprintf/substitution tokens so they survive for later sprintf() |
||
| 34 | $name = $params['name'] ?? ''; |
||
| 35 | $u = $params['u'] ?? 0; |
||
| 36 | |||
| 37 | $isNameToken = is_string($name) && $name !== '' && $name[0] === '%' && preg_match('~^%\d\$s$~m', $name) === 1; |
||
| 38 | $isUidToken = is_string($u) && $u !== '' && $u[0] === '%' && preg_match('~^%\d\$d$~m', $u) === 1; |
||
| 39 | |||
| 40 | if ($isNameToken) |
||
| 41 | { |
||
| 42 | $slug = $name; // keep token as-is (do NOT encode) |
||
| 43 | } |
||
| 44 | else |
||
| 45 | { |
||
| 46 | // Safely build a slug from the display name; guard against null |
||
| 47 | $name = (string) $name; |
||
| 48 | $name = trim($name); |
||
| 49 | $slug = $name === '' ? 'member' : preg_replace('~\s+~u', '-', $name); |
||
| 50 | $slug = trim($slug, '-'); |
||
| 51 | $slug = rawurlencode($slug); |
||
| 52 | } |
||
| 53 | |||
| 54 | $uid = $isUidToken ? $u : (int) $u; |
||
| 55 | |||
| 56 | $url = 'p/' . $slug . '-' . $uid; |
||
| 57 | unset($params['name'], $params['u'], $params['action']); |
||
| 58 | |||
| 59 | return $url . $this->generateQuery($params); |
||
| 60 | } |
||
| 62 |