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 |
||
10 | class Matrix |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $matrix; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $rows; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $columns; |
||
26 | |||
27 | /** |
||
28 | * @var float |
||
29 | */ |
||
30 | private $determinant; |
||
31 | |||
32 | /** |
||
33 | * @param array $matrix |
||
34 | * @param bool $validate |
||
35 | * |
||
36 | * @throws InvalidArgumentException |
||
37 | */ |
||
38 | public function __construct(array $matrix, bool $validate = true) |
||
60 | |||
61 | /** |
||
62 | * @param array $array |
||
63 | * |
||
64 | * @return Matrix |
||
65 | */ |
||
66 | public static function fromFlatArray(array $array) |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | public function toArray() |
||
83 | |||
84 | /** |
||
85 | * @return float |
||
86 | */ |
||
87 | public function toScalar() |
||
91 | |||
92 | /** |
||
93 | * @return int |
||
94 | */ |
||
95 | public function getRows() |
||
99 | |||
100 | /** |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getColumns() |
||
107 | |||
108 | /** |
||
109 | * @param $column |
||
110 | * |
||
111 | * @return array |
||
112 | * |
||
113 | * @throws MatrixException |
||
114 | */ |
||
115 | public function getColumnValues($column) |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @return float|int |
||
127 | * |
||
128 | * @throws MatrixException |
||
129 | */ |
||
130 | public function getDeterminant() |
||
142 | |||
143 | /** |
||
144 | * @return float|int |
||
145 | * |
||
146 | * @throws MatrixException |
||
147 | */ |
||
148 | private function calculateDeterminant() |
||
167 | |||
168 | /** |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function isSquare() |
||
175 | |||
176 | /** |
||
177 | * @return Matrix |
||
178 | */ |
||
179 | public function transpose() |
||
191 | |||
192 | /** |
||
193 | * @param Matrix $matrix |
||
194 | * |
||
195 | * @return Matrix |
||
196 | * |
||
197 | * @throws InvalidArgumentException |
||
198 | */ |
||
199 | public function multiply(Matrix $matrix) |
||
219 | |||
220 | /** |
||
221 | * @param $value |
||
222 | * |
||
223 | * @return Matrix |
||
224 | */ |
||
225 | public function divideByScalar($value) |
||
236 | |||
237 | /** |
||
238 | * @param $value |
||
239 | * |
||
240 | * @return Matrix |
||
241 | */ |
||
242 | public function multiplyByScalar($value) |
||
253 | |||
254 | /** |
||
255 | * Element-wise addition of the matrix with another one |
||
256 | * |
||
257 | * @param Matrix $other |
||
258 | */ |
||
259 | public function add(Matrix $other) |
||
263 | |||
264 | /** |
||
265 | * Element-wise subtracting of another matrix from this one |
||
266 | * |
||
267 | * @param Matrix $other |
||
268 | */ |
||
269 | public function subtract(Matrix $other) |
||
273 | |||
274 | /** |
||
275 | * Element-wise addition or substraction depending on the given sign parameter |
||
276 | * |
||
277 | * @param Matrix $other |
||
278 | * @param type $sign |
||
279 | */ |
||
280 | protected function _add(Matrix $other, $sign = 1) |
||
294 | |||
295 | /** |
||
296 | * @return Matrix |
||
297 | * |
||
298 | * @throws MatrixException |
||
299 | */ |
||
300 | public function inverse() |
||
322 | |||
323 | /** |
||
324 | * @param int $row |
||
325 | * @param int $column |
||
326 | * |
||
327 | * @return Matrix |
||
328 | */ |
||
329 | public function crossOut(int $row, int $column) |
||
348 | |||
349 | /** |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function isSingular() : bool |
||
356 | |||
357 | /** |
||
358 | * Returns the transpose of given array |
||
359 | * |
||
360 | * @param array $array |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public static function transposeArray(array $array) |
||
368 | |||
369 | /** |
||
370 | * Returns the dot product of two arrays<br> |
||
371 | * Matrix::dot(x, y) ==> x.y' |
||
372 | * |
||
373 | * @param array $array1 |
||
374 | * @param array $array2 |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | public static function dot(array $array1, array $array2) |
||
385 | } |
||
386 |
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.