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) |
||
53 | |||
54 | /** |
||
55 | * @param array $array |
||
56 | * |
||
57 | * @return Matrix |
||
58 | */ |
||
59 | public static function fromFlatArray(array $array) |
||
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | */ |
||
72 | public function toArray() |
||
76 | |||
77 | /** |
||
78 | * @return int |
||
79 | */ |
||
80 | public function getRows() |
||
84 | |||
85 | /** |
||
86 | * @return int |
||
87 | */ |
||
88 | public function getColumns() |
||
92 | |||
93 | /** |
||
94 | * @param $column |
||
95 | * |
||
96 | * @return array |
||
97 | * |
||
98 | * @throws MatrixException |
||
99 | */ |
||
100 | public function getColumnValues($column) |
||
113 | |||
114 | /** |
||
115 | * @return float|int |
||
116 | * |
||
117 | * @throws MatrixException |
||
118 | */ |
||
119 | public function getDeterminant() |
||
146 | |||
147 | /** |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function isSquare() |
||
154 | |||
155 | /** |
||
156 | * @return Matrix |
||
157 | */ |
||
158 | public function transpose() |
||
169 | |||
170 | /** |
||
171 | * @param Matrix $matrix |
||
172 | * |
||
173 | * @return Matrix |
||
174 | * |
||
175 | * @throws InvalidArgumentException |
||
176 | */ |
||
177 | public function multiply(Matrix $matrix) |
||
196 | |||
197 | /** |
||
198 | * @param $value |
||
199 | * |
||
200 | * @return Matrix |
||
201 | */ |
||
202 | public function divideByScalar($value) |
||
213 | |||
214 | /** |
||
215 | * @return Matrix |
||
216 | * |
||
217 | * @throws MatrixException |
||
218 | */ |
||
219 | public function inverse() |
||
241 | |||
242 | /** |
||
243 | * @param int $row |
||
244 | * @param int $column |
||
245 | * |
||
246 | * @return Matrix |
||
247 | */ |
||
248 | public function crossOut(int $row, int $column) |
||
267 | } |
||
268 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.