Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Matrix 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 Matrix, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Matrix |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $matrix = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $rows; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $columns; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var float |
||
| 32 | */ |
||
| 33 | private $determinant; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @throws InvalidArgumentException |
||
| 37 | */ |
||
| 38 | public function __construct(array $matrix, bool $validate = true) |
||
| 60 | |||
| 61 | public static function fromFlatArray(array $array): self |
||
| 70 | |||
| 71 | public function toArray(): array |
||
| 75 | |||
| 76 | public function toScalar(): float |
||
| 80 | |||
| 81 | public function getRows(): int |
||
| 85 | |||
| 86 | public function getColumns(): int |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @throws MatrixException |
||
| 93 | */ |
||
| 94 | public function getColumnValues($column): array |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return float|int |
||
| 105 | * |
||
| 106 | * @throws MatrixException |
||
| 107 | */ |
||
| 108 | public function getDeterminant() |
||
| 122 | |||
| 123 | public function isSquare(): bool |
||
| 127 | |||
| 128 | public function transpose(): self |
||
| 139 | |||
| 140 | public function multiply(self $matrix): self |
||
| 158 | |||
| 159 | View Code Duplication | public function divideByScalar($value): self |
|
| 170 | |||
| 171 | View Code Duplication | public function multiplyByScalar($value): self |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Element-wise addition of the matrix with another one |
||
| 185 | */ |
||
| 186 | public function add(self $other): self |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Element-wise subtracting of another matrix from this one |
||
| 193 | */ |
||
| 194 | public function subtract(self $other): self |
||
| 198 | |||
| 199 | public function inverse(): self |
||
| 211 | |||
| 212 | public function crossOut(int $row, int $column): self |
||
| 232 | |||
| 233 | public function isSingular(): bool |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Frobenius norm (Hilbert–Schmidt norm, Euclidean norm) (‖A‖F) |
||
| 240 | * Square root of the sum of the square of all elements. |
||
| 241 | * |
||
| 242 | * https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm |
||
| 243 | * |
||
| 244 | * _____________ |
||
| 245 | * /ᵐ ⁿ |
||
| 246 | * ‖A‖F = √ Σ Σ |aᵢⱼ|² |
||
| 247 | * ᵢ₌₁ ᵢ₌₁ |
||
| 248 | */ |
||
| 249 | public function frobeniusNorm(): float |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the transpose of given array |
||
| 263 | */ |
||
| 264 | public static function transposeArray(array $array): array |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns the dot product of two arrays<br> |
||
| 271 | * Matrix::dot(x, y) ==> x.y' |
||
| 272 | */ |
||
| 273 | public static function dot(array $array1, array $array2): array |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Element-wise addition or substraction depending on the given sign parameter |
||
| 283 | */ |
||
| 284 | private function _add(self $other, int $sign = 1): self |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns diagonal identity matrix of the same size of this matrix |
||
| 301 | */ |
||
| 302 | private function getIdentity(): self |
||
| 306 | } |
||
| 307 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.