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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 40 | class Point |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var CurveFp |
||
| 44 | */ |
||
| 45 | private $curve; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var GmpMath |
||
| 49 | */ |
||
| 50 | private $adapter; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ModularArithmetic |
||
| 54 | */ |
||
| 55 | private $modAdapter; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \GMP |
||
| 59 | */ |
||
| 60 | private $x; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \GMP |
||
| 64 | */ |
||
| 65 | private $y; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \GMP |
||
| 69 | */ |
||
| 70 | private $order; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | private $infinity = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Initialize a new instance |
||
| 79 | * |
||
| 80 | * @param GmpMath $adapter |
||
| 81 | * @param CurveFp $curve |
||
| 82 | * @param \GMP $x |
||
| 83 | * @param \GMP $y |
||
| 84 | * @param \GMP $order |
||
| 85 | * @param bool $infinity |
||
| 86 | * |
||
| 87 | * @throws \RuntimeException when either the curve does not contain the given coordinates or |
||
| 88 | * when order is not null and P(x, y) * order is not equal to infinity. |
||
| 89 | */ |
||
| 90 | public function __construct(GmpMath $adapter, CurveFp $curve, \GMP $x, \GMP $y, \GMP $order = null, $infinity = false) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return GmpMath |
||
| 113 | */ |
||
| 114 | public function getAdapter() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | * @see \Jose\Component\Core\Util\Ecc\Point::isInfinity() |
||
| 122 | */ |
||
| 123 | public function isInfinity() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritDoc} |
||
| 130 | * @see \Jose\Component\Core\Util\Ecc\Point::getCurve() |
||
| 131 | */ |
||
| 132 | public function getCurve() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritDoc} |
||
| 139 | * @see \Jose\Component\Core\Util\Ecc\Point::getOrder() |
||
| 140 | */ |
||
| 141 | public function getOrder() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritDoc} |
||
| 148 | * @see \Jose\Component\Core\Util\Ecc\Point::getX() |
||
| 149 | */ |
||
| 150 | public function getX() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritDoc} |
||
| 157 | * @see \Jose\Component\Core\Util\Ecc\Point::getY() |
||
| 158 | */ |
||
| 159 | public function getY() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritDoc} |
||
| 166 | * @see \Jose\Component\Core\Util\Ecc\Point::add() |
||
| 167 | * @return self |
||
| 168 | */ |
||
| 169 | public function add(Point $addend) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritDoc} |
||
| 214 | * @see \Jose\Component\Core\Util\Ecc\Point::cmp() |
||
| 215 | */ |
||
| 216 | public function cmp(Point $other) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritDoc} |
||
| 241 | * @see \Jose\Component\Core\Util\Ecc\Point::equals() |
||
| 242 | */ |
||
| 243 | public function equals(Point $other) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritDoc} |
||
| 250 | * @see \Jose\Component\Core\Util\Ecc\Point::mul() |
||
| 251 | */ |
||
| 252 | public function mul(\GMP $n) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param Point $a |
||
| 294 | * @param Point $b |
||
| 295 | * @param int $cond |
||
| 296 | */ |
||
| 297 | private function cswap(self $a, self $b, $cond) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param $a |
||
| 307 | * @param $b |
||
| 308 | * @param $cond |
||
| 309 | */ |
||
| 310 | public function cswapValue(& $a, & $b, $cond) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * |
||
| 335 | */ |
||
| 336 | private function validate() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * {@inheritDoc} |
||
| 345 | * @see \Jose\Component\Core\Util\Ecc\Point::getDouble() |
||
| 346 | * @return self |
||
| 347 | */ |
||
| 348 | public function getDouble() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * {@inheritDoc} |
||
| 380 | * @see \Jose\Component\Core\Util\Ecc\Point::__toString() |
||
| 381 | */ |
||
| 382 | public function __toString() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function __debugInfo() |
||
| 411 | } |
||
| 412 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.