| Conditions | 10 |
| Paths | 35 |
| Total Lines | 32 |
| Code Lines | 20 |
| 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 |
||
| 14 | public static function fromArray(array $route) |
||
| 15 | { |
||
| 16 | if (!isset($route['pattern'])) { |
||
| 17 | throw new InvalidArgumentException('Required parameter pattern is not set.'); |
||
| 18 | } |
||
| 19 | |||
| 20 | if (!isset($route['method'])) { |
||
| 21 | throw new InvalidArgumentException('Required parameter method is not set.'); |
||
| 22 | } |
||
| 23 | |||
| 24 | if (!isset($route['controller'])) { |
||
| 25 | throw new InvalidArgumentException('Required parameter controller is not set.'); |
||
| 26 | } |
||
| 27 | |||
| 28 | if (isset($route['value']) && !is_array($route['value'])) { |
||
| 29 | $route['value'] = []; |
||
| 30 | } |
||
| 31 | |||
| 32 | $methods = !is_array($route['method']) ? [$route['method']] : $route['method']; |
||
| 33 | $assert = isset($route['assert']) ? $route['assert'] : []; |
||
| 34 | $value = isset($route['value']) ? $route['value'] : []; |
||
| 35 | $name = isset($route['name']) ? $route['name'] : ''; |
||
| 36 | |||
| 37 | return new Route( |
||
| 38 | new Methods($methods), |
||
| 39 | new Pattern($route['pattern']), |
||
| 40 | new Controller($route['controller']), |
||
| 41 | new Asserts($assert), |
||
| 42 | new Values($value), |
||
| 43 | new Name($name) |
||
|
|
|||
| 44 | ); |
||
| 45 | } |
||
| 46 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.