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:
| 1 | <?php |
||
| 11 | class KernelPCA extends PCA |
||
| 12 | { |
||
| 13 | const KERNEL_RBF = 1; |
||
| 14 | const KERNEL_SIGMOID = 2; |
||
| 15 | const KERNEL_LAPLACIAN = 3; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Selected kernel function |
||
| 19 | * |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $kernel; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Gamma value used by the kernel |
||
| 26 | * |
||
| 27 | * @var float |
||
| 28 | */ |
||
| 29 | protected $gamma; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Original dataset used to fit KernelPCA |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $data; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * |
||
| 40 | * @param int $kernel |
||
| 41 | * @param float $totalVariance |
||
| 42 | * @param int $numFeatures |
||
| 43 | * @param float $gamma |
||
| 44 | * |
||
| 45 | * @throws \Exception |
||
| 46 | */ |
||
| 47 | public function __construct(int $kernel = self::KERNEL_RBF, $totalVariance = null, $numFeatures = null, $gamma = null) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Takes a data and returns a lower dimensional version |
||
| 61 | * of this data while preserving $totalVariance or $numFeatures. <br> |
||
| 62 | * $data is an n-by-m matrix and returned array is |
||
| 63 | * n-by-k matrix where k <= m |
||
| 64 | * |
||
| 65 | * @param array $data |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function fit(array $data) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Calculates similarity matrix by use of selected kernel function<br> |
||
| 91 | * An n-by-m matrix is given and an n-by-n matrix is returned |
||
| 92 | * |
||
| 93 | * @param array $data |
||
| 94 | * @param int $numRows |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | protected function calculateKernelMatrix(array $data, int $numRows) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Kernel matrix is centered in its original space by using the following |
||
| 118 | * conversion: |
||
| 119 | * |
||
| 120 | * K′ = K− N . K − K.N + N.K.N where N is n-by-n matrix filled with 1/n |
||
| 121 | * |
||
| 122 | * @param array $matrix |
||
| 123 | * @param int $n |
||
| 124 | */ |
||
| 125 | protected function centerMatrix(array $matrix, int $n) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns the callable kernel function |
||
| 146 | * |
||
| 147 | * @return \Closure |
||
| 148 | */ |
||
| 149 | protected function getKernel() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param array $sample |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | protected function getDistancePairs(array $sample) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param array $pairs |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | protected function projectSample(array $pairs) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Transforms the given sample to a lower dimensional vector by using |
||
| 245 | * the variables obtained during the last run of <code>fit</code>. |
||
| 246 | * |
||
| 247 | * @param array $sample |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function transform(array $sample) |
||
| 268 | } |
||
| 269 |
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.