| Conditions | 10 |
| Paths | 384 |
| Total Lines | 18 |
| Code Lines | 10 |
| 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 |
||
| 29 | public function generate($params) |
||
| 30 | { |
||
| 31 | $subject = isset($params['subject']) && $params['subject'] !== null ? (string) $params['subject'] : ''; |
||
| 32 | |||
| 33 | // Simple, safe slug: trim, collapse whitespace to '-', fallback to 'topic' if empty |
||
| 34 | $subject = trim($subject); |
||
| 35 | $slug = $subject === '' ? 'topic' : preg_replace('~\s+~u', '-', $subject); |
||
| 36 | $slug = trim($slug, '-'); |
||
| 37 | |||
| 38 | $topic = isset($params['topic']) ? (int) $params['topic'] : 0; |
||
| 39 | $has_start = isset($params['start']) && $params['start'] !== '' && $params['start'] !== null; |
||
| 40 | $start = $has_start ? $params['start'] : null; |
||
| 41 | |||
| 42 | // Semantic pagination format is dot-appended after the id (e.g., t/slug-id.10) |
||
| 43 | $url = 't/' . rawurlencode($slug) . '-' . $topic . ($has_start && $start !== 0 ? '.' . $start : ''); |
||
| 44 | unset($params['subject'], $params['topic'], $params['start']); |
||
| 45 | |||
| 46 | return $url . $this->generateQuery($params); |
||
| 47 | } |
||
| 49 |