Conditions | 12 |
Paths | 15 |
Total Lines | 30 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
45 | public function parameter(string $value) |
||
46 | { |
||
47 | $res = []; |
||
48 | $parts = \preg_split('#(%[^%\s]+%)#i', $value, -1, \PREG_SPLIT_DELIM_CAPTURE); |
||
49 | |||
50 | if (false === $parts || (1 === \count($parts) && $value === $parts[0])) { |
||
51 | return $value; |
||
52 | } |
||
53 | |||
54 | $partsN = \count($parts = \array_filter($parts)); |
||
55 | |||
56 | foreach ($parts as $part) { |
||
57 | if ('' !== $part && '%' === $part[0]) { |
||
58 | $val = \substr($part, 1, -1); |
||
59 | |||
60 | if (!\array_key_exists($val, $this->parameters)) { |
||
61 | throw new \InvalidArgumentException(\sprintf('You have requested a non-existent parameter "%s".', $val)); |
||
62 | } |
||
63 | |||
64 | $part = $this->parameters[$val]; |
||
65 | |||
66 | if ($partsN > 1 && !\is_scalar($part)) { |
||
67 | throw new \InvalidArgumentException(\sprintf('Unable to concatenate non-scalar parameter "%s" into %s.', $val, $value)); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | $res[] = $part; |
||
72 | } |
||
73 | |||
74 | return [] === $res ? $value : (1 === \count($res) ? $res[0] : \implode('', $res)); |
||
75 | } |
||
77 |