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 LUDecomposition 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 LUDecomposition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class LUDecomposition |
||
38 | { |
||
39 | /** |
||
40 | * Decomposition storage |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $LU = []; |
||
45 | |||
46 | /** |
||
47 | * Row dimension. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | private $m; |
||
52 | |||
53 | /** |
||
54 | * Column dimension. |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | private $n; |
||
59 | |||
60 | /** |
||
61 | * Pivot sign. |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | private $pivsign; |
||
66 | |||
67 | /** |
||
68 | * Internal storage of pivot vector. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $piv = []; |
||
73 | |||
74 | /** |
||
75 | * Constructs Structure to access L, U and piv. |
||
76 | * |
||
77 | * @param Matrix $A Rectangular matrix |
||
78 | * |
||
79 | * @throws MatrixException |
||
80 | */ |
||
81 | public function __construct(Matrix $A) |
||
147 | |||
148 | /** |
||
149 | * Get lower triangular factor. |
||
150 | * |
||
151 | * @return Matrix Lower triangular factor |
||
152 | */ |
||
153 | public function getL(): Matrix |
||
170 | |||
171 | /** |
||
172 | * Get upper triangular factor. |
||
173 | * |
||
174 | * @return Matrix Upper triangular factor |
||
175 | */ |
||
176 | public function getU(): Matrix |
||
191 | |||
192 | /** |
||
193 | * Return pivot permutation vector. |
||
194 | * |
||
195 | * @return array Pivot vector |
||
196 | */ |
||
197 | public function getPivot(): array |
||
201 | |||
202 | /** |
||
203 | * Alias for getPivot |
||
204 | * |
||
205 | * @see getPivot |
||
206 | */ |
||
207 | public function getDoublePivot() |
||
211 | |||
212 | /** |
||
213 | * Is the matrix nonsingular? |
||
214 | * |
||
215 | * @return bool true if U, and hence A, is nonsingular. |
||
216 | */ |
||
217 | public function isNonsingular(): bool |
||
227 | |||
228 | public function det(): float |
||
237 | |||
238 | /** |
||
239 | * Solve A*X = B |
||
240 | * |
||
241 | * @param Matrix $B A Matrix with as many rows as A and any number of columns. |
||
242 | * |
||
243 | * @return array X so that L*U*X = B(piv,:) |
||
244 | * |
||
245 | * @throws MatrixException |
||
246 | */ |
||
247 | public function solve(Matrix $B): array |
||
284 | |||
285 | protected function getSubMatrix(array $matrix, array $RL, int $j0, int $jF): array |
||
299 | } |
||
300 |
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.