| Conditions | 10 |
| Paths | 41 |
| Total Lines | 48 |
| Code Lines | 25 |
| 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 |
||
| 49 | public function inflect(string $word) : string |
||
| 50 | { |
||
| 51 | if (isset($this->cache['singularize'][$word])) { |
||
| 52 | return $this->cache['singularize'][$word]; |
||
| 53 | } |
||
| 54 | |||
| 55 | if (! isset($this->rules['merged']['uninflected'])) { |
||
| 56 | $this->rules['merged']['uninflected'] = array_merge( |
||
| 57 | $this->rules['uninflected'], |
||
| 58 | $this->uninflected->getUninflected() |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (! isset($this->rules['merged']['irregular'])) { |
||
| 63 | $this->rules['merged']['irregular'] = array_merge( |
||
| 64 | $this->rules['irregular'], |
||
| 65 | array_flip($this->pluralizer->getRules()['irregular']) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (! isset($this->rules['cacheUninflected']) || ! isset($this->rules['cacheIrregular'])) { |
||
| 70 | $this->rules['cacheUninflected'] = '(?:' . implode('|', $this->rules['merged']['uninflected']) . ')'; |
||
| 71 | $this->rules['cacheIrregular'] = '(?:' . implode('|', array_keys($this->rules['merged']['irregular'])) . ')'; |
||
| 72 | } |
||
| 73 | |||
| 74 | if (preg_match('/(.*)\\b(' . $this->rules['cacheIrregular'] . ')$/i', $word, $regs)) { |
||
| 75 | $this->cache['singularize'][$word] = $regs[1] . $word[0] . substr($this->rules['merged']['irregular'][strtolower($regs[2])], 1); |
||
| 76 | |||
| 77 | return $this->cache['singularize'][$word]; |
||
| 78 | } |
||
| 79 | |||
| 80 | if (preg_match('/^(' . $this->rules['cacheUninflected'] . ')$/i', $word, $regs)) { |
||
| 81 | $this->cache['singularize'][$word] = $word; |
||
| 82 | |||
| 83 | return $word; |
||
| 84 | } |
||
| 85 | |||
| 86 | foreach ($this->rules['rules'] as $rule => $replacement) { |
||
| 87 | if (preg_match($rule, $word)) { |
||
| 88 | $this->cache['singularize'][$word] = preg_replace($rule, $replacement, $word); |
||
| 89 | |||
| 90 | return $this->cache['singularize'][$word]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->cache['singularize'][$word] = $word; |
||
| 95 | |||
| 96 | return $word; |
||
| 97 | } |
||
| 99 |