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:
Complex classes like Sql often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sql, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Sql |
||
|
|||
9 | { |
||
10 | /** |
||
11 | * Protège des injections SQL |
||
12 | * @param {string} la chaîne à sécuriser |
||
13 | * |
||
14 | */ |
||
15 | public static function sanitize($value) |
||
20 | |||
21 | /** |
||
22 | * Protège des injections SQL (pour les requètes) |
||
23 | * @param {string} la chaîne à sécuriser |
||
24 | **/ |
||
25 | public static function secure($value) |
||
29 | |||
30 | /** |
||
31 | * Protège des injections SQL pour les integers |
||
32 | * @param {string} la chaîne à sécuriser |
||
33 | **/ |
||
34 | public static function secureId($value) |
||
42 | |||
43 | public static function secureListeId($value) |
||
53 | |||
54 | /** |
||
55 | * Protège des injections SQL pour les integers |
||
56 | * @param {string} la chaîne à sécuriser |
||
57 | **/ |
||
58 | public static function secureInteger($value) |
||
67 | |||
68 | /** |
||
69 | * Protège des injections SQL pour les integers |
||
70 | * @param {string} la chaîne à sécuriser |
||
71 | **/ |
||
72 | public static function secureBoolean($value) |
||
84 | |||
85 | /** |
||
86 | * Protège des injections décimales SQL en remplaçant les "," par des "." |
||
87 | * @param {Decimal} le Décimal à sécuriser |
||
88 | **/ |
||
89 | public static function secureDecimal($value) |
||
98 | |||
99 | /** |
||
100 | * Protège des injections de date SQL en remplaçant les "" par des null |
||
101 | * @param {date} lea date à sécuriser |
||
102 | **/ |
||
103 | public static function secureDate($value) |
||
114 | |||
115 | /** |
||
116 | * Protège des injections SQL (pour les requètes) |
||
117 | * @param {Array} le tableau à sécuriser |
||
118 | **/ |
||
119 | public static function secureArray($values) |
||
123 | |||
124 | public static function replaceXJoins($tabs, $orig_join, &$join = array()) |
||
137 | |||
138 | public static function replaceXFields($tab, $class = null, $option = array(), &$join = array()) |
||
158 | |||
159 | private static function filterWhere($i) |
||
163 | |||
164 | /** |
||
165 | * Convertit un tableau de conditions en un WHERE conditions |
||
166 | * Le deuxième paramètre permet de faire un HAVING à la place |
||
167 | * @uses self::filterWhere |
||
168 | */ |
||
169 | public static function parseWhere($where, $having = false, $class = null, $option = array()) |
||
198 | |||
199 | /* * ******** |
||
200 | * Filtres * |
||
201 | * ******** */ |
||
202 | |||
203 | /** |
||
204 | * Cette fonction crée un tableau de conditions LIKE à partir d'un tableau |
||
205 | */ |
||
206 | public static function conditionsFromArray($params) |
||
246 | } |
||
247 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.