| Conditions | 10 |
| Paths | 41 |
| Total Lines | 39 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 44 | public function inflect(string $word) : string |
||
| 45 | { |
||
| 46 | if (isset($this->cache['pluralize'][$word])) { |
||
| 47 | return $this->cache['pluralize'][$word]; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (! isset($this->rules['merged']['irregular'])) { |
||
| 51 | $this->rules['merged']['irregular'] = $this->rules['irregular']; |
||
| 52 | } |
||
| 53 | |||
| 54 | if (! isset($this->rules['merged']['uninflected'])) { |
||
| 55 | $this->rules['merged']['uninflected'] = array_merge( |
||
| 56 | $this->rules['uninflected'], |
||
| 57 | $this->uninflected->getUninflected() |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (! isset($this->rules['cacheUninflected']) || ! isset($this->rules['cacheIrregular'])) { |
||
| 62 | $this->rules['cacheUninflected'] = '(?:' . implode('|', $this->rules['merged']['uninflected']) . ')'; |
||
| 63 | $this->rules['cacheIrregular'] = '(?:' . implode('|', array_keys($this->rules['merged']['irregular'])) . ')'; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (preg_match('/(.*)\\b(' . $this->rules['cacheIrregular'] . ')$/i', $word, $regs)) { |
||
| 67 | $this->cache['pluralize'][$word] = $regs[1] . $word[0] . substr($this->rules['merged']['irregular'][strtolower($regs[2])], 1); |
||
| 68 | |||
| 69 | return $this->cache['pluralize'][$word]; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (preg_match('/^(' . $this->rules['cacheUninflected'] . ')$/i', $word, $regs)) { |
||
| 73 | $this->cache['pluralize'][$word] = $word; |
||
| 74 | |||
| 75 | return $word; |
||
| 76 | } |
||
| 77 | |||
| 78 | foreach ($this->rules['rules'] as $rule => $replacement) { |
||
| 79 | if (preg_match($rule, $word)) { |
||
| 80 | $this->cache['pluralize'][$word] = preg_replace($rule, $replacement, $word); |
||
| 81 | |||
| 82 | return $this->cache['pluralize'][$word]; |
||
| 83 | } |
||
| 87 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: