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 |
||
13 | class ArrayHelper extends \yii\helpers\ArrayHelper |
||
14 | { |
||
15 | public static function make_sub($array, $v_key, $k_key = null) |
||
29 | |||
30 | /** |
||
31 | * Parses data, exploding the string by comma, trying to create array. |
||
32 | * |
||
33 | * @param $string |
||
34 | * @param string $delimiter |
||
35 | * @return array |
||
36 | */ |
||
37 | View Code Duplication | public static function csplit($string, $delimiter = ',') |
|
52 | |||
53 | View Code Duplication | public static function ksplit($string, $delimiter = ',') |
|
68 | |||
69 | /** |
||
70 | * Retrieves the values of an array elements or object properties with the given key or property name. |
||
71 | * Uses [[getValue]] method. |
||
72 | * |
||
73 | * @param array|object $array array or object to extract value from |
||
74 | * @param array [string] $keys keys names of the array elements, or properties of the object |
||
75 | * @return array |
||
76 | * @see getValue() |
||
77 | */ |
||
78 | public static function getValues($array, $keys = []) |
||
87 | } |
||
88 |
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.