Conditions | 7 |
Paths | 8 |
Total Lines | 52 |
Code Lines | 36 |
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 __construct(string $snippetKey, ?string $filterType = null, string $paramKey, array $originalParams, string $originalFilter) |
||
45 | { |
||
46 | $this->snippetKey = $snippetKey; |
||
47 | $this->filterType = $filterType; |
||
48 | |||
49 | $this->paramKey = $paramKey; |
||
50 | |||
51 | $this->originalParams = $originalParams; |
||
52 | $this->originalFilter = $originalFilter; |
||
53 | |||
54 | $this->processedParams = $originalParams; |
||
55 | $this->processedFilter = $originalFilter; |
||
56 | |||
57 | switch ($filterType) { |
||
58 | case 'like': |
||
|
|||
59 | $newValue = '%'.$this->getOriginalParamValue().'%'; |
||
60 | $this->replaceParamValue($newValue); |
||
61 | break; |
||
62 | case 'string_any': |
||
63 | $newValue = '{'.implode(', ', array_map('strval', $this->getOriginalParamValue())).'}'; |
||
64 | $this->replaceParamValue($newValue); |
||
65 | break; |
||
66 | case 'numeric_any': |
||
67 | $newValue = '{'.implode(', ', array_map('intval', $this->getOriginalParamValue())).'}'; |
||
68 | $this->replaceParamValue($newValue); |
||
69 | break; |
||
70 | case 'in': |
||
71 | $newFilter = ''; |
||
72 | $newParams = []; |
||
73 | |||
74 | $i = 0; |
||
75 | |||
76 | foreach ($this->getOriginalParamValue() as $value) { |
||
77 | $newFilter .= ':'.$paramKey.'_'.$i.','; |
||
78 | $newParams[$paramKey.'_'.$i] = $value; |
||
79 | |||
80 | ++$i; |
||
81 | } |
||
82 | |||
83 | if (strlen($newFilter) > 0) { |
||
84 | $newFilter = substr($newFilter, 0, strlen($newFilter) - 1); |
||
85 | } |
||
86 | |||
87 | $this->removeParams([$this->getParamKey()]); |
||
88 | |||
89 | $processedParams = array_merge($this->processedParams, $newParams); |
||
90 | $processedFilter = str_replace(':'.$paramKey, $newFilter, $originalFilter); |
||
91 | |||
92 | $this->setProcessedParams($processedParams); |
||
93 | $this->setProcessedFilter($processedFilter); |
||
94 | |||
95 | break; |
||
96 | } |
||
213 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.