Conditions | 11 |
Paths | 9 |
Total Lines | 52 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
54 | public function calcAdjacentIntervals(array $bounds): array |
||
55 | { |
||
56 | |||
57 | $newIntervals = []; |
||
58 | $activeIntervals = []; |
||
59 | |||
60 | $boundsCount = count($bounds); |
||
61 | |||
62 | // Create new intervals for each set of two consecutive bounds, |
||
63 | // and calculate its total value. |
||
64 | for ($i = 1; $i < $boundsCount; $i++) { |
||
65 | |||
66 | // Set the current bound. |
||
67 | [$curBoundValue, $curBoundType, $curBoundIncluded, $curBoundIntervalKey] = $bounds[$i - 1]; |
||
68 | [$nextBoundValue, $nextBoundType, $nextBoundIncluded] = $bounds[$i]; |
||
69 | |||
70 | if ($curBoundType === '+') { |
||
71 | // If this is a low bound, |
||
72 | // add the key of the interval to the array of active intervals. |
||
73 | $activeIntervals[$curBoundIntervalKey] = true; |
||
74 | } else { |
||
75 | // If this is an high bound, remove the key. |
||
76 | unset($activeIntervals[$curBoundIntervalKey]); |
||
77 | } |
||
78 | |||
79 | if ( |
||
80 | isset($this->addStep, $this->substractStep) && ( |
||
81 | ($nextBoundIncluded && $nextBoundType === '+') |
||
82 | || (!$nextBoundIncluded && $nextBoundType === '+') |
||
83 | ) |
||
84 | ) { |
||
85 | $newHighBound = ($this->substractStep)($nextBoundValue); |
||
86 | } else { |
||
87 | $newHighBound = $nextBoundValue; |
||
88 | } |
||
89 | |||
90 | if ( |
||
91 | isset($this->addStep, $this->substractStep) && $curBoundType === '-' && $curBoundIncluded |
||
92 | ) { |
||
93 | $newLowBound = ($this->addStep)($curBoundValue); |
||
94 | } else { |
||
95 | $newLowBound = $curBoundValue; |
||
96 | } |
||
97 | |||
98 | $newIntervals[] = [ |
||
99 | $newLowBound, |
||
100 | $newHighBound, |
||
101 | $activeIntervals |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | return $newIntervals; |
||
106 | } |
||
108 |