Conditions | 10 |
Paths | 37 |
Total Lines | 81 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Tests | 36 |
CRAP Score | 10.1815 |
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:
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 |
||
58 | 388 | public static function maclaurinSeries( |
|
59 | SimpleNumberInterface $input, // x value in series |
||
60 | callable $numerator, // a function determining what the sign (+/-) is at the nth term |
||
61 | callable $exponent, // a function determining the exponent of x at the nth term |
||
62 | callable $denominator, // a function determining the denominator at the nth term |
||
63 | int $startTermAt = 0, |
||
64 | int $scale = 10, |
||
65 | int $consecutiveDivergeLimit = 5, |
||
66 | int $totalDivergeLimit = 10): ImmutableDecimal |
||
67 | { |
||
68 | |||
69 | 388 | ++$scale; |
|
70 | |||
71 | 388 | $sum = Numbers::makeZero($scale); |
|
72 | 388 | $value = Numbers::make(Numbers::IMMUTABLE, $input->getValue(NumberBase::Ten), $scale); |
|
|
|||
73 | |||
74 | 388 | $continue = true; |
|
75 | 388 | $termNumber = $startTermAt; |
|
76 | |||
77 | 388 | $adjustmentOfZero = 0; |
|
78 | 388 | $prevDiff = Numbers::makeZero($scale); |
|
79 | 388 | $prevSum = $sum; |
|
80 | 388 | $divergeCount = -1; |
|
81 | 388 | $persistentDivergeCount = -1; |
|
82 | 388 | $currentScale = 0; |
|
83 | |||
84 | 388 | while ($continue) { |
|
85 | 388 | $term = Numbers::makeOne($scale); |
|
86 | |||
87 | try { |
||
88 | 388 | $exTerm = $value->pow($exponent($termNumber)); |
|
89 | 388 | $term = $term->multiply($exTerm); |
|
90 | 388 | $term = $term->divide($denominator($termNumber), $scale); |
|
91 | 388 | $term = $term->multiply($numerator($termNumber)); |
|
92 | } catch (IntegrityConstraint $constraint) { |
||
93 | return $sum->truncateToScale($currentScale+1); |
||
94 | } |
||
95 | |||
96 | /** @var ImmutableDecimal $term */ |
||
97 | 388 | if ($term->numberOfLeadingZeros() >= $scale && !$term->isWhole()) { |
|
98 | $continue = false; |
||
99 | } |
||
100 | |||
101 | 388 | $currentScale = $term->numberOfLeadingZeros(); |
|
102 | |||
103 | 388 | if ($term->isEqual(0)) { |
|
104 | 388 | $adjustmentOfZero++; |
|
105 | } else { |
||
106 | 388 | $adjustmentOfZero = 0; |
|
107 | } |
||
108 | |||
109 | 388 | if ($adjustmentOfZero > 15) { |
|
110 | 388 | $continue = false; |
|
111 | } |
||
112 | |||
113 | /** @var ImmutableDecimal $sum */ |
||
114 | 388 | $sum = $sum->add($term); |
|
115 | 388 | $currDiff = $sum->subtract($prevSum)->abs(); |
|
116 | |||
117 | 388 | if ($prevDiff->isLessThan($currDiff)) { |
|
118 | 388 | $divergeCount++; |
|
119 | 388 | $persistentDivergeCount++; |
|
120 | } else { |
||
121 | 388 | $divergeCount = 0; |
|
122 | } |
||
123 | |||
124 | 388 | if ($divergeCount === $consecutiveDivergeLimit || $persistentDivergeCount === $totalDivergeLimit) { |
|
125 | throw new OptionalExit( |
||
126 | 'Series appear to be diverging. Current diverge count: '.$divergeCount.' | Persistent diverge count: '.$persistentDivergeCount, |
||
127 | 'A call was made to SeriesProvider::maclaurinSeries() that seems to be diverging. Exiting the loop.', |
||
128 | 'The series being calculated appears to be diverging, and the process has been stopped in an attempt to avoid an infinite loop.' |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | 388 | $prevDiff = $currDiff; |
|
133 | 388 | $prevSum = $sum; |
|
134 | |||
135 | 388 | $termNumber++; |
|
136 | } |
||
137 | |||
138 | 388 | return $sum->roundToScale($scale); |
|
139 | |||
221 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.