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 |
||
8 | class DictsUtils |
||
9 | { |
||
10 | /** |
||
11 | * Dicts 에서 특정 key를 가진 값들을 모두 얻기 |
||
12 | * |
||
13 | * $dicts = $db->sqlDicts('select * from tb_book'); |
||
14 | * $b_ids = DictsUtils::extractValuesByKey($dicts, 'id'); |
||
15 | * |
||
16 | * @param $dicts |
||
17 | * @param $key |
||
18 | * @return array |
||
19 | */ |
||
20 | public static function extractValuesByKey($dicts, $key) |
||
28 | |||
29 | /** |
||
30 | * Dicts 에서 특정 key 를 기준으로 재배치(key 기준으로 하나밖에 없을떄) |
||
31 | * |
||
32 | * $dicts = $db->sqlDicts('select * from cpdp_books'); |
||
33 | * $cpdp_books_by_bid = DictsUtils::alignByKey($dicts, 'b_id'); |
||
34 | * $cpdp_books_111011110 = $tb_book_property_by_bid['111011110']; |
||
35 | * |
||
36 | * $cpdp_books_111011110 => ['id' => '123123', 'b_id'=>'111011110', 'title' => '바람과 함께 사라지다'] |
||
37 | * |
||
38 | * @param $dicts |
||
39 | * @param $key |
||
40 | * @return array |
||
41 | */ |
||
42 | public static function alignByKey($dicts, $key) |
||
50 | |||
51 | /** |
||
52 | * Dicts 에서 특정 key 를 기준으로 재배치(key 기준으로 여러개 있을떄) |
||
53 | * |
||
54 | * $dicts = $db->sqlDicts('select * from tb_book_property'); |
||
55 | * $tb_book_propertys_by_bid = DictsUtils::alignListByKey($dicts, 'b_id'); |
||
56 | * $tb_book_propertys_111011110 = $tb_book_propertys_by_bid['111011110']; |
||
57 | * |
||
58 | * $tb_book_propertys_111011110 => [ |
||
59 | * ['b_id'=>'111011110', 'key' => 'num_pages', 'vaule' => 123123], |
||
60 | * ['b_id'=>'111011110', 'key' => 'type', 'vaule' => 'pdf'], |
||
61 | * ] |
||
62 | * |
||
63 | * @param $dicts |
||
64 | * @param $key |
||
65 | * @return array |
||
66 | */ |
||
67 | public static function alignListByKey($dicts, $key) |
||
75 | |||
76 | /** |
||
77 | * Dicts 와 Dicts 끼리 join 할때 (SQL 에서 join 하지 않고 PHP에서 join) |
||
78 | * |
||
79 | * $dictA = $db->sqlDicts('select t_id, amount point from tb_point'); |
||
80 | * $dictB = $db->sqlDicts('select t_id, amount cash from tb_cash'); |
||
81 | * $dictNew = DictsUtils::join($dictA, $dictB); |
||
82 | * |
||
83 | * $dictNew => [ |
||
84 | * [t_id => '111', 'point' => 123, 'cash' => 456], |
||
85 | * [t_id => '112', 'point' => 124, 'cash' => 457], |
||
86 | * ] |
||
87 | * |
||
88 | * @param $left_dicts |
||
89 | * @param $right_dicts |
||
90 | * @param int $left_dicts_column_index_to_join |
||
91 | * @param int $right_dicts_column_index_to_join |
||
92 | * @return mixed |
||
93 | */ |
||
94 | View Code Duplication | public static function join( |
|
136 | |||
137 | public static function convertDictsToHtmlTable($dicts) |
||
159 | } |
||
160 |
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.