Complex classes like Post_Class 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 Post_Class, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Post_Class extends Singleton_Util { |
||
24 | /** |
||
25 | * Le nom du modèle |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $model_name = 'post_model'; |
||
30 | |||
31 | /** |
||
32 | * Le type du post |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $post_type = 'post'; |
||
37 | |||
38 | /** |
||
39 | * La clé principale pour post_meta |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $meta_key = '_wpeo_post'; |
||
44 | |||
45 | /** |
||
46 | * Le nom pour le resgister post type |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $post_type_name = 'posts'; |
||
51 | |||
52 | /** |
||
53 | * Utiles pour récupérer la clé unique |
||
54 | * |
||
55 | * @todo Rien à faire ici |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $identifier_helper = 'post'; |
||
59 | |||
60 | /** |
||
61 | * Fonction de callback après avoir récupérer le modèle en mode GET. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $after_get_function = array(); |
||
66 | |||
67 | /** |
||
68 | * Fonction de callback avant d'insérer les données en mode POST. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $before_post_function = array(); |
||
73 | |||
74 | /** |
||
75 | * Fonction de callback avant de dispacher les données en mode POST. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $before_model_post_function = array(); |
||
80 | |||
81 | /** |
||
82 | * Fonction de callback après avoir inséré les données en mode POST. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $after_post_function = array(); |
||
87 | |||
88 | /** |
||
89 | * Fonction de callback avant de mêttre à jour les données en mode PUT. |
||
90 | * |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $before_put_function = array(); |
||
94 | |||
95 | /** |
||
96 | * Fonction de callback avant de dispatcher les données en mode PUT. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $before_model_put_function = array(); |
||
101 | |||
102 | /** |
||
103 | * Fonction de callback après avoir mis à jour les données en mode PUT. |
||
104 | * |
||
105 | * @var array |
||
106 | */ |
||
107 | protected $after_put_function = array(); |
||
108 | |||
109 | /** |
||
110 | * Appelle l'action "init" de WordPress |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | protected function construct() { |
||
117 | |||
118 | /** |
||
119 | * Initialise le post type selon $name et $name_singular. |
||
120 | * Initialise la taxonomy si elle existe. |
||
121 | * |
||
122 | * @see register_post_type |
||
123 | * @return boolean |
||
124 | * |
||
125 | * @since 1.0.0.0 |
||
126 | * @version 1.3.0.0 |
||
127 | */ |
||
128 | public function init_post_type() { |
||
144 | |||
145 | /** |
||
146 | * Permet de récupérer le schéma avec les données du modèle par défault. |
||
147 | * |
||
148 | * @since 1.0.0.0 |
||
149 | * @version 1.3.0.0 |
||
150 | * |
||
151 | * @return Object |
||
152 | */ |
||
153 | public function get_schema() { |
||
158 | |||
159 | /** |
||
160 | * Récupères les données selon le modèle définis. |
||
161 | * |
||
162 | * @since 1.0.0.0 |
||
163 | * @version 1.3.0.0 |
||
164 | * |
||
165 | * @param array $args Les paramètres de get_comments @https://codex.wordpress.org/Function_Reference/WP_Query. |
||
166 | * @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée. |
||
167 | * |
||
168 | * @return Object |
||
169 | * |
||
170 | * @todo: ligne 128 - Temporaire |
||
171 | */ |
||
172 | public function get( $args = array( |
||
239 | |||
240 | /** |
||
241 | * Appelle la méthode update. |
||
242 | * |
||
243 | * @since 1.0.0.0 |
||
244 | * @version 1.3.0.0 |
||
245 | * |
||
246 | * @param Array $data Les données. |
||
247 | * @return Array $data Les données |
||
248 | */ |
||
249 | public function create( $data ) { |
||
252 | |||
253 | /** |
||
254 | * Insère ou met à jour les données dans la base de donnée. |
||
255 | * |
||
256 | * @since 1.0.0.0 |
||
257 | * @version 1.3.0.0 |
||
258 | * |
||
259 | * @param Array $data Les données a insérer ou à mêttre à jour. |
||
260 | * @return Object L'objet construit grâce au modèle. |
||
261 | */ |
||
262 | public function update( $data ) { |
||
331 | |||
332 | /** |
||
333 | * Recherche dans les meta value. |
||
334 | * |
||
335 | * @since 1.0.0.0 |
||
336 | * @version 1.3.0.0 |
||
337 | * |
||
338 | * @param string $search Le terme de la recherche. |
||
339 | * @param array $array La définition de la recherche. |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | public function search( $search, $array ) { |
||
379 | |||
380 | /** |
||
381 | * Retourne le post type |
||
382 | * |
||
383 | * @since 1.0.0.0 |
||
384 | * @version 1.3.0.0 |
||
385 | * |
||
386 | * @return string Le post type |
||
387 | */ |
||
388 | public function get_post_type() { |
||
391 | |||
392 | /** |
||
393 | * Utile uniquement pour DigiRisk. |
||
394 | * |
||
395 | * @since 1.0.0.0 |
||
396 | * @version 1.3.0.0 |
||
397 | * |
||
398 | * @return string L'identifiant des commentaires pour DigiRisk. |
||
399 | */ |
||
400 | public function get_identifier_helper() { |
||
403 | |||
404 | /** |
||
405 | * Récupères les ID des taxonomies lié à ce post. |
||
406 | * |
||
407 | * @param object $data L'objet courant. |
||
408 | * @return object L'objet avec les taxonomies. |
||
409 | * |
||
410 | * @since 1.0.0.0 |
||
411 | * @version 1.3.0.0 |
||
412 | */ |
||
413 | private function get_taxonomies_id( $data ) { |
||
425 | |||
426 | /** |
||
427 | * Sauvegardes les taxonomies |
||
428 | * |
||
429 | * @param object $data L'objet avec les taxonomies à sauvegarder. |
||
430 | */ |
||
431 | private function save_taxonomies( $data ) { |
||
440 | |||
441 | /** |
||
442 | * Permet de changer le modèle en dur. |
||
443 | * |
||
444 | * @param string $model_name Le nom du modèle. |
||
445 | * |
||
446 | * @since 1.0.0.0 |
||
447 | * @version 1.3.6.0 |
||
448 | */ |
||
449 | public function set_model( $model_name ) { |
||
452 | } |
||
453 | } // End if(). |
||
454 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.