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) |
||
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) |
|
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) |
|
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()) |
||
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.