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 |
||
30 | class TableLoad |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * loadTableFromArray |
||
35 | * |
||
36 | * @param string $table name of table to load without prefix |
||
37 | * @param array $data array of rows to insert |
||
38 | * Each element of the outer array represents a single table row. |
||
39 | * Each row is an associative array in 'column' => 'value' format. |
||
40 | * |
||
41 | * @return int number of rows inserted |
||
42 | */ |
||
43 | public static function loadTableFromArray($table, $data) |
||
44 | { |
||
45 | $db = \Xoops::getInstance()->db(); |
||
46 | $count = 0; |
||
47 | $db->beginTransaction(); |
||
48 | foreach ($data as $row) { |
||
49 | $count += $db->insertPrefix($table, $row); |
||
50 | } |
||
51 | $db->commit(); |
||
52 | return $count; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * loadTableFromYamlFile |
||
57 | * |
||
58 | * @param string $table name of table to load without prefix |
||
59 | * @param string $yamlFile name of file containing data dump in YAML format |
||
60 | * |
||
61 | * @return int number of rows inserted |
||
62 | */ |
||
63 | View Code Duplication | public static function loadTableFromYamlFile($table, $yamlFile) |
|
64 | { |
||
65 | $count = 0; |
||
66 | |||
67 | $data = Yaml::loadWrapped($yamlFile); // work with phpmyadmin YAML dumps |
||
68 | if (false !== $data) { |
||
69 | $count = static::loadTableFromArray($table, $data); |
||
|
|||
70 | } |
||
71 | |||
72 | return $count; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * truncateTable - empty a database table |
||
77 | * |
||
78 | * @param string $table name of table to truncate |
||
79 | * |
||
80 | * @return int number of affected rows |
||
81 | */ |
||
82 | public static function truncateTable($table) |
||
90 | |||
91 | /** |
||
92 | * countRows - get count of rows in a table |
||
93 | * |
||
94 | * @param string $table name of table to count |
||
95 | * @param CriteriaElement $criteria optional criteria |
||
96 | * |
||
97 | * @return int number of rows |
||
98 | */ |
||
99 | 1 | public static function countRows($table, CriteriaElement $criteria = null) |
|
100 | { |
||
101 | 1 | $db = \Xoops::getInstance()->db(); |
|
102 | 1 | $qb = $db->createXoopsQueryBuilder(); |
|
103 | 1 | $qb->select('COUNT(*)') |
|
104 | 1 | ->fromPrefix($table, ''); |
|
105 | 1 | if (isset($criteria)) { |
|
106 | $qb = $criteria->renderQb($qb, ''); |
||
107 | } |
||
108 | 1 | $result = $qb->execute(); |
|
109 | 1 | $count = $result->fetchColumn(); |
|
110 | 1 | return (int)$count; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * extractRows - get rows, all or a subset, from a table as an array |
||
115 | * |
||
116 | * @param string $table name of table to read |
||
117 | * @param CriteriaElement $criteria optional criteria |
||
118 | * @param string[] $skipColumns do not include these columns in the extract |
||
119 | * |
||
120 | * @return array of table rows |
||
121 | */ |
||
122 | public static function extractRows($table, CriteriaElement $criteria = null, $skipColumns = array()) |
||
123 | { |
||
124 | $db = \Xoops::getInstance()->db(); |
||
125 | $qb = $db->createXoopsQueryBuilder(); |
||
126 | $qb->select('*')->fromPrefix($table, ''); |
||
127 | if (isset($criteria)) { |
||
128 | $qb = $criteria->renderQb($qb, ''); |
||
129 | } |
||
130 | $result = $qb->execute(); |
||
131 | $rows = $result->fetchAll(); |
||
132 | |||
133 | if (!empty($skipColumns)) { |
||
134 | foreach ($rows as $index => $row) { |
||
135 | foreach ($skipColumns as $column) { |
||
136 | unset($rows[$index][$column]); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return $rows; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Save table data to a YAML file |
||
146 | * |
||
147 | * @param string $table name of table to load without prefix |
||
148 | * @param string $yamlFile name of file containing data dump in YAML format |
||
149 | * @param CriteriaElement $criteria optional criteria |
||
150 | * @param string[] $skipColumns do not include columns in this list |
||
151 | * |
||
152 | * @return bool true on success, false on error |
||
153 | */ |
||
154 | View Code Duplication | public static function saveTableToYamlFile($table, $yamlFile, $criteria = null, $skipColumns = array()) |
|
162 | } |
||
163 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.