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 |
||
11 | class Matrix |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $matrix = []; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $rows; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $columns; |
||
27 | |||
28 | /** |
||
29 | * @var float |
||
30 | */ |
||
31 | private $determinant; |
||
32 | |||
33 | /** |
||
34 | * @throws InvalidArgumentException |
||
35 | */ |
||
36 | public function __construct(array $matrix, bool $validate = true) |
||
37 | { |
||
38 | // When a row vector is given |
||
39 | if (!is_array($matrix[0])) { |
||
40 | $this->rows = 1; |
||
41 | $this->columns = count($matrix); |
||
42 | $matrix = [$matrix]; |
||
43 | } else { |
||
44 | $this->rows = count($matrix); |
||
45 | $this->columns = count($matrix[0]); |
||
46 | } |
||
47 | |||
48 | if ($validate) { |
||
49 | for ($i = 0; $i < $this->rows; ++$i) { |
||
50 | if (count($matrix[$i]) !== $this->columns) { |
||
51 | throw InvalidArgumentException::matrixDimensionsDidNotMatch(); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | $this->matrix = $matrix; |
||
57 | } |
||
58 | |||
59 | public static function fromFlatArray(array $array): self |
||
68 | |||
69 | public function toArray(): array |
||
73 | |||
74 | public function toScalar(): float |
||
78 | |||
79 | public function getRows(): int |
||
83 | |||
84 | public function getColumns(): int |
||
88 | |||
89 | /** |
||
90 | * @throws MatrixException |
||
91 | */ |
||
92 | public function getColumnValues($column): array |
||
100 | |||
101 | /** |
||
102 | * @return float|int |
||
103 | * |
||
104 | * @throws MatrixException |
||
105 | */ |
||
106 | public function getDeterminant() |
||
120 | |||
121 | public function isSquare(): bool |
||
125 | |||
126 | public function transpose(): self |
||
138 | |||
139 | public function multiply(self $matrix): self |
||
159 | |||
160 | View Code Duplication | public function divideByScalar($value): self |
|
171 | |||
172 | View Code Duplication | public function multiplyByScalar($value): self |
|
183 | |||
184 | /** |
||
185 | * Element-wise addition of the matrix with another one |
||
186 | */ |
||
187 | public function add(self $other): self |
||
191 | |||
192 | /** |
||
193 | * Element-wise subtracting of another matrix from this one |
||
194 | */ |
||
195 | public function subtract(self $other): self |
||
199 | |||
200 | public function inverse(): self |
||
212 | |||
213 | public function crossOut(int $row, int $column): self |
||
233 | |||
234 | public function isSingular(): bool |
||
238 | |||
239 | /** |
||
240 | * Returns the transpose of given array |
||
241 | */ |
||
242 | public static function transposeArray(array $array): array |
||
246 | |||
247 | /** |
||
248 | * Returns the dot product of two arrays<br> |
||
249 | * Matrix::dot(x, y) ==> x.y' |
||
250 | */ |
||
251 | public static function dot(array $array1, array $array2): array |
||
258 | |||
259 | /** |
||
260 | * Element-wise addition or substraction depending on the given sign parameter |
||
261 | */ |
||
262 | protected function _add(self $other, int $sign = 1): self |
||
276 | |||
277 | /** |
||
278 | * Returns diagonal identity matrix of the same size of this matrix |
||
279 | */ |
||
280 | protected function getIdentity(): self |
||
289 | } |
||
290 |
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.