Conditions | 10 |
Paths | 37 |
Total Lines | 80 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 37 |
CRAP Score | 10.1685 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
45 | 36 | public static function maclaurinSeries( |
|
46 | SimpleNumberInterface $input, // x value in series |
||
47 | callable $numerator, // a function determining what the sign (+/-) is at the nth term |
||
48 | callable $exponent, // a function determining the exponent of x at the nth term |
||
49 | callable $denominator, // a function determining the denominator at the nth term |
||
50 | int $startTermAt = 0, |
||
51 | int $precision = 10, |
||
52 | int $consecutiveDivergeLimit = 5, |
||
53 | int $totalDivergeLimit = 10): ImmutableDecimal |
||
54 | { |
||
55 | |||
56 | 36 | ++$precision; |
|
57 | |||
58 | 36 | $sum = Numbers::makeZero($precision); |
|
59 | 36 | $value = Numbers::make(Numbers::IMMUTABLE, $input->getValue(), $precision); |
|
60 | |||
61 | 36 | $continue = true; |
|
62 | 36 | $termNumber = $startTermAt; |
|
63 | |||
64 | 36 | $adjustmentOfZero = 0; |
|
65 | 36 | $prevDiff = Numbers::makeZero($precision); |
|
66 | 36 | $prevSum = $sum; |
|
67 | 36 | $divergeCount = -1; |
|
68 | 36 | $persistentDivergeCount = -1; |
|
69 | 36 | $currentPrecision = 0; |
|
70 | |||
71 | 36 | while ($continue) { |
|
72 | 36 | $term = Numbers::makeOne($precision); |
|
73 | |||
74 | try { |
||
75 | 36 | $exTerm = $value->pow($exponent($termNumber)); |
|
76 | 36 | $term = $term->multiply($exTerm); |
|
77 | 36 | $term = $term->divide($denominator($termNumber)); |
|
78 | 36 | $term = $term->multiply($numerator($termNumber)); |
|
79 | } catch (IntegrityConstraint $constraint) { |
||
80 | return $sum->truncateToPrecision($currentPrecision+1); |
||
81 | } |
||
82 | |||
83 | /** @var ImmutableDecimal $term */ |
||
84 | 36 | if ($term->numberOfLeadingZeros() >= $precision && !$term->isWhole()) { |
|
85 | 29 | $continue = false; |
|
86 | } |
||
87 | |||
88 | 36 | $currentPrecision = $term->numberOfLeadingZeros(); |
|
89 | |||
90 | 36 | if ($term->isEqual(0)) { |
|
91 | 9 | $adjustmentOfZero++; |
|
92 | } else { |
||
93 | 36 | $adjustmentOfZero = 0; |
|
94 | } |
||
95 | |||
96 | 36 | if ($adjustmentOfZero > 15) { |
|
97 | 9 | $continue = false; |
|
98 | } |
||
99 | |||
100 | /** @var ImmutableDecimal $sum */ |
||
101 | 36 | $sum = $sum->add($term); |
|
102 | 36 | $currDiff = $sum->subtract($prevSum)->abs(); |
|
103 | |||
104 | 36 | if ($prevDiff->isLessThan($currDiff)) { |
|
105 | 36 | $divergeCount++; |
|
106 | 36 | $persistentDivergeCount++; |
|
107 | } else { |
||
108 | 36 | $divergeCount = 0; |
|
109 | } |
||
110 | |||
111 | 36 | if ($divergeCount === $consecutiveDivergeLimit || $persistentDivergeCount === $totalDivergeLimit) { |
|
112 | throw new OptionalExit( |
||
113 | 'Series appear to be diverging. Current diverge count: '.$divergeCount.' | Persistent diverge count: '.$persistentDivergeCount, |
||
114 | 'A call was made to SeriesProvider::maclaurinSeries() that seems to be diverging. Exiting the loop.' |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | 36 | $prevDiff = $currDiff; |
|
119 | 36 | $prevSum = $sum; |
|
120 | |||
121 | 36 | $termNumber++; |
|
122 | } |
||
123 | |||
124 | 36 | return $sum->roundToPrecision($precision); |
|
125 | |||
128 | } |