Conditions | 16 |
Paths | 9 |
Total Lines | 55 |
Code Lines | 30 |
Lines | 4 |
Ratio | 7.27 % |
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 |
||
67 | public static function fromDataset(array $data, int $i, int $k, $sample = true, float $meanX = null, float $meanY = null) |
||
68 | { |
||
69 | if (empty($data)) { |
||
70 | throw InvalidArgumentException::arrayCantBeEmpty(); |
||
71 | } |
||
72 | |||
73 | $n = count($data); |
||
74 | if ($sample && $n === 1) { |
||
75 | throw InvalidArgumentException::arraySizeToSmall(2); |
||
76 | } |
||
77 | |||
78 | if ($i < 0 || $k < 0 || $i >= $n || $k >= $n) { |
||
79 | throw new \Exception("Given indices i and k do not match with the dimensionality of data"); |
||
80 | } |
||
81 | |||
82 | if ($meanX === null || $meanY === null) { |
||
83 | $x = array_column($data, $i); |
||
84 | $y = array_column($data, $k); |
||
85 | |||
86 | $meanX = Mean::arithmetic($x); |
||
87 | $meanY = Mean::arithmetic($y); |
||
88 | $sum = 0.0; |
||
89 | View Code Duplication | foreach ($x as $index => $xi) { |
|
90 | $yi = $y[$index]; |
||
91 | $sum += ($xi - $meanX) * ($yi - $meanY); |
||
92 | } |
||
93 | } else { |
||
94 | // In the case, whole dataset given along with dimension indices, i and k, |
||
95 | // we would like to avoid getting column data with array_column and operate |
||
96 | // over this extra copy of column data for memory efficiency purposes. |
||
97 | // |
||
98 | // Instead we traverse through the whole data and get what we actually need |
||
99 | // without copying the data. This way, memory use will be reduced |
||
100 | // with a slight cost of CPU utilization. |
||
101 | $sum = 0.0; |
||
102 | foreach ($data as $row) { |
||
103 | $val = []; |
||
104 | foreach ($row as $index => $col) { |
||
105 | if ($index == $i) { |
||
106 | $val[0] = $col - $meanX; |
||
107 | } |
||
108 | if ($index == $k) { |
||
109 | $val[1] = $col - $meanY; |
||
110 | } |
||
111 | } |
||
112 | $sum += $val[0] * $val[1]; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if ($sample) { |
||
117 | --$n; |
||
118 | } |
||
119 | |||
120 | return $sum / $n; |
||
121 | } |
||
122 | |||
156 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.