Total Complexity | 46 |
Total Lines | 370 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Point 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 Point, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class Point implements PointInterface |
||
43 | { |
||
44 | /** |
||
45 | * @var CurveFpInterface |
||
46 | */ |
||
47 | private $curve; |
||
48 | |||
49 | /** |
||
50 | * @var GmpMathInterface |
||
51 | */ |
||
52 | private $adapter; |
||
53 | |||
54 | /** |
||
55 | * @var ModularArithmetic |
||
56 | */ |
||
57 | private $modAdapter; |
||
58 | |||
59 | /** |
||
60 | * @var \GMP |
||
61 | */ |
||
62 | private $x; |
||
63 | |||
64 | /** |
||
65 | * @var \GMP |
||
66 | */ |
||
67 | private $y; |
||
68 | |||
69 | /** |
||
70 | * @var \GMP |
||
71 | */ |
||
72 | private $order; |
||
73 | |||
74 | /** |
||
75 | * @var bool |
||
76 | */ |
||
77 | private $infinity = false; |
||
78 | |||
79 | /** |
||
80 | * Initialize a new instance |
||
81 | * |
||
82 | * @param GmpMathInterface $adapter |
||
83 | * @param CurveFpInterface $curve |
||
84 | * @param \GMP $x |
||
85 | * @param \GMP $y |
||
86 | * @param \GMP $order |
||
87 | * @param bool $infinity |
||
88 | * |
||
89 | * @throws \RuntimeException when either the curve does not contain the given coordinates or |
||
90 | * when order is not null and P(x, y) * order is not equal to infinity. |
||
91 | */ |
||
92 | public function __construct(GmpMathInterface $adapter, CurveFpInterface $curve, \GMP $x, \GMP $y, \GMP $order = null, bool $infinity = false) |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return GmpMathInterface |
||
115 | */ |
||
116 | public function getAdapter(): GmpMathInterface |
||
117 | { |
||
118 | return $this->adapter; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritDoc} |
||
123 | * @see \Mdanter\Ecc\Primitives\PointInterface::isInfinity() |
||
124 | */ |
||
125 | public function isInfinity(): bool |
||
126 | { |
||
127 | return (bool) $this->infinity; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritDoc} |
||
132 | * @see \Mdanter\Ecc\Primitives\PointInterface::getCurve() |
||
133 | */ |
||
134 | public function getCurve(): CurveFpInterface |
||
135 | { |
||
136 | return $this->curve; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | * @see \Mdanter\Ecc\Primitives\PointInterface::getOrder() |
||
142 | */ |
||
143 | public function getOrder(): \GMP |
||
144 | { |
||
145 | return $this->order; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritDoc} |
||
150 | * @see \Mdanter\Ecc\Primitives\PointInterface::getX() |
||
151 | */ |
||
152 | public function getX(): \GMP |
||
153 | { |
||
154 | return $this->x; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * {@inheritDoc} |
||
159 | * @see \Mdanter\Ecc\Primitives\PointInterface::getY() |
||
160 | */ |
||
161 | public function getY(): \GMP |
||
162 | { |
||
163 | return $this->y; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * {@inheritDoc} |
||
168 | * @see \Mdanter\Ecc\Primitives\PointInterface::add() |
||
169 | * @return self |
||
170 | */ |
||
171 | public function add(PointInterface $addend): PointInterface |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * {@inheritDoc} |
||
216 | * @see \Mdanter\Ecc\Primitives\PointInterface::cmp() |
||
217 | */ |
||
218 | public function cmp(PointInterface $other): int |
||
219 | { |
||
220 | if ($other->isInfinity() && $this->isInfinity()) { |
||
221 | return 0; |
||
222 | } |
||
223 | |||
224 | if ($other->isInfinity() || $this->isInfinity()) { |
||
225 | return 1; |
||
226 | } |
||
227 | |||
228 | $math = $this->adapter; |
||
229 | $equal = ($math->equals($this->x, $other->getX())); |
||
230 | $equal &= ($math->equals($this->y, $other->getY())); |
||
231 | $equal &= $this->isInfinity() == $other->isInfinity(); |
||
232 | $equal &= $this->curve->equals($other->getCurve()); |
||
233 | |||
234 | if ($equal) { |
||
235 | return 0; |
||
236 | } |
||
237 | |||
238 | return 1; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritDoc} |
||
243 | * @see \Mdanter\Ecc\Primitives\PointInterface::equals() |
||
244 | */ |
||
245 | public function equals(PointInterface $other): bool |
||
246 | { |
||
247 | return $this->cmp($other) == 0; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * {@inheritDoc} |
||
252 | * @see \Mdanter\Ecc\Primitives\PointInterface::mul() |
||
253 | */ |
||
254 | public function mul(\GMP $n): PointInterface |
||
255 | { |
||
256 | if ($this->isInfinity()) { |
||
257 | return $this->curve->getInfinity(); |
||
258 | } |
||
259 | |||
260 | $zero = gmp_init(0, 10); |
||
261 | if ($this->adapter->cmp($this->order, $zero) > 0) { |
||
262 | $n = $this->adapter->mod($n, $this->order); |
||
263 | } |
||
264 | |||
265 | if ($this->adapter->equals($n, $zero)) { |
||
266 | return $this->curve->getInfinity(); |
||
267 | } |
||
268 | |||
269 | /** @var Point[] $r */ |
||
270 | $r = [ |
||
271 | $this->curve->getInfinity(), |
||
272 | clone $this |
||
273 | ]; |
||
274 | |||
275 | $k = $this->curve->getSize(); |
||
276 | $n = str_pad($this->adapter->baseConvert($this->adapter->toString($n), 10, 2), $k, '0', STR_PAD_LEFT); |
||
277 | |||
278 | for ($i = 0; $i < $k; $i++) { |
||
279 | $j = $n[$i]; |
||
280 | |||
281 | $this->cswap($r[0], $r[1], $j ^ 1, $k); |
||
282 | |||
283 | $r[0] = $r[0]->add($r[1]); |
||
284 | $r[1] = $r[1]->getDouble(); |
||
285 | |||
286 | $this->cswap($r[0], $r[1], $j ^ 1, $k); |
||
287 | } |
||
288 | |||
289 | $r[0]->validate(); |
||
290 | |||
291 | return $r[0]; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param Point $a |
||
296 | * @param Point $b |
||
297 | * @param int $cond |
||
298 | * @param int $curveSize |
||
299 | */ |
||
300 | private function cswap(self $a, self $b, int $cond, int $curveSize) |
||
301 | { |
||
302 | $this->cswapValue($a->x, $b->x, $cond, $curveSize); |
||
303 | $this->cswapValue($a->y, $b->y, $cond, $curveSize); |
||
304 | $this->cswapValue($a->order, $b->order, $cond, $curveSize); |
||
305 | $this->cswapValue($a->infinity, $b->infinity, $cond, 8); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param bool|\GMP $a |
||
310 | * @param bool|\GMP $b |
||
311 | * @param int $cond |
||
312 | * @param int $maskBitSize |
||
313 | */ |
||
314 | public function cswapValue(& $a, & $b, int $cond, int $maskBitSize) |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * |
||
337 | */ |
||
338 | private function validate() |
||
342 | } |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * {@inheritDoc} |
||
347 | * @see \Mdanter\Ecc\Primitives\PointInterface::getDouble() |
||
348 | * @return self |
||
349 | */ |
||
350 | public function getDouble(): PointInterface |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * {@inheritDoc} |
||
382 | * @see \Mdanter\Ecc\Primitives\PointInterface::__toString() |
||
383 | */ |
||
384 | public function __toString(): string |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @return array |
||
395 | */ |
||
396 | public function __debugInfo(): array |
||
412 | } |
||
413 | } |
||
414 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.