Total Complexity | 40 |
Total Lines | 234 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like GenericGFPoly often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GenericGFPoly, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | final class GenericGFPoly{ |
||
28 | |||
29 | private array $coefficients; |
||
30 | |||
31 | /** |
||
32 | * @param array $coefficients array coefficients as ints representing elements of GF(size), arranged |
||
33 | * from most significant (highest-power term) coefficient to least significant |
||
34 | * @param int|null $degree |
||
35 | * |
||
36 | * @throws \InvalidArgumentException if argument is null or empty, or if leading coefficient is 0 and this is not a |
||
37 | * constant polynomial (that is, it is not the monomial "0") |
||
38 | */ |
||
39 | public function __construct(array $coefficients, int $degree = null){ |
||
40 | $degree ??= 0; |
||
41 | |||
42 | if(empty($coefficients)){ |
||
43 | throw new InvalidArgumentException('arg $coefficients is empty'); |
||
44 | } |
||
45 | |||
46 | if($degree < 0){ |
||
47 | throw new InvalidArgumentException('negative degree'); |
||
48 | } |
||
49 | |||
50 | $coefficientsLength = count($coefficients); |
||
51 | |||
52 | // Leading term must be non-zero for anything except the constant polynomial "0" |
||
53 | $firstNonZero = 0; |
||
54 | |||
55 | while($firstNonZero < $coefficientsLength && $coefficients[$firstNonZero] === 0){ |
||
56 | $firstNonZero++; |
||
57 | } |
||
58 | |||
59 | if($firstNonZero === $coefficientsLength){ |
||
60 | $this->coefficients = [0]; |
||
61 | } |
||
62 | else{ |
||
63 | $this->coefficients = array_fill(0, $coefficientsLength - $firstNonZero + $degree, 0); |
||
64 | |||
65 | for($i = 0; $i < $coefficientsLength - $firstNonZero; $i++){ |
||
66 | $this->coefficients[$i] = $coefficients[$i + $firstNonZero]; |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return int $coefficient of x^degree term in this polynomial |
||
73 | */ |
||
74 | public function getCoefficient(int $degree):int{ |
||
75 | return $this->coefficients[count($this->coefficients) - 1 - $degree]; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return int[] |
||
80 | */ |
||
81 | public function getCoefficients():array{ |
||
82 | return $this->coefficients; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return int $degree of this polynomial |
||
87 | */ |
||
88 | public function getDegree():int{ |
||
89 | return count($this->coefficients) - 1; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return bool true if this polynomial is the monomial "0" |
||
94 | */ |
||
95 | public function isZero():bool{ |
||
96 | return $this->coefficients[0] === 0; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return int evaluation of this polynomial at a given point |
||
101 | */ |
||
102 | public function evaluateAt(int $a):int{ |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | */ |
||
122 | public function multiply(GenericGFPoly $other):self{ |
||
123 | |||
124 | if($this->isZero() || $other->isZero()){ |
||
125 | return new self([0]); |
||
126 | } |
||
127 | |||
128 | $product = array_fill(0, count($this->coefficients) + count($other->coefficients) - 1, 0); |
||
129 | |||
130 | foreach($this->coefficients as $i => $aCoeff){ |
||
131 | foreach($other->coefficients as $j => $bCoeff){ |
||
132 | $product[$i + $j] ^= GF256::multiply($aCoeff, $bCoeff); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return new self($product); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return \chillerlan\QRCode\Common\GenericGFPoly[] [quotient, remainder] |
||
141 | */ |
||
142 | public function divide(GenericGFPoly $other):array{ |
||
143 | |||
144 | if($other->isZero()){ |
||
145 | throw new InvalidArgumentException('Division by 0'); |
||
146 | } |
||
147 | |||
148 | $quotient = new self([0]); |
||
149 | $remainder = clone $this; |
||
150 | |||
151 | $denominatorLeadingTerm = $other->getCoefficient($other->getDegree()); |
||
152 | $inverseDenominatorLeadingTerm = GF256::inverse($denominatorLeadingTerm); |
||
153 | |||
154 | while($remainder->getDegree() >= $other->getDegree() && !$remainder->isZero()){ |
||
155 | $scale = GF256::multiply($remainder->getCoefficient($remainder->getDegree()), $inverseDenominatorLeadingTerm); |
||
156 | $diff = $remainder->getDegree() - $other->getDegree(); |
||
157 | $quotient = $quotient->addOrSubtract(GF256::buildMonomial($diff, $scale)); |
||
158 | $remainder = $remainder->addOrSubtract($other->multiplyByMonomial($diff, $scale)); |
||
159 | } |
||
160 | |||
161 | return [$quotient, $remainder]; |
||
162 | |||
163 | } |
||
164 | |||
165 | /** |
||
166 | * |
||
167 | */ |
||
168 | public function multiplyInt(int $scalar):self{ |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * |
||
189 | */ |
||
190 | public function multiplyByMonomial(int $degree, int $coefficient):self{ |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * |
||
211 | */ |
||
212 | public function mod(GenericGFPoly $other):self{ |
||
213 | |||
214 | if(count($this->coefficients) - count($other->coefficients) < 0){ |
||
215 | return $this; |
||
216 | } |
||
217 | |||
218 | $ratio = GF256::log($this->coefficients[0]) - GF256::log($other->coefficients[0]); |
||
219 | |||
220 | foreach($other->coefficients as $i => $c){ |
||
221 | $this->coefficients[$i] ^= GF256::exp(GF256::log($c) + $ratio); |
||
222 | } |
||
223 | |||
224 | return (new self($this->coefficients))->mod($other); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * |
||
229 | */ |
||
230 | public function addOrSubtract(GenericGFPoly $other):self{ |
||
261 | } |
||
262 | |||
263 | } |
||
264 |