Total Complexity | 54 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CatForm 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.
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 CatForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class CatForm extends FormValidator |
||
10 | { |
||
11 | public const TYPE_ADD = 1; |
||
12 | public const TYPE_EDIT = 2; |
||
13 | public const TYPE_MOVE = 3; |
||
14 | public const TYPE_SELECT_COURSE = 4; |
||
15 | /** @var Category */ |
||
16 | private $category_object; |
||
17 | |||
18 | /** |
||
19 | * CatForm constructor. |
||
20 | * Builds a form containing form items based on a given parameter. |
||
21 | * |
||
22 | * @param string $form_type 1=add, 2=edit,3=move,4=browse |
||
23 | * @param string $category_object |
||
24 | * @param string $form_name |
||
25 | * @param string $method |
||
26 | * @param null $action |
||
|
|||
27 | */ |
||
28 | public function __construct( |
||
29 | $form_type, |
||
30 | $category_object, |
||
31 | $form_name, |
||
32 | $method = 'post', |
||
33 | $action = null |
||
34 | ) { |
||
35 | parent::__construct($form_name, $method, $action); |
||
36 | $this->form_type = $form_type; |
||
37 | if (isset($category_object)) { |
||
38 | $this->category_object = $category_object; |
||
39 | } |
||
40 | |||
41 | switch ($this->form_type) { |
||
42 | case self::TYPE_EDIT: |
||
43 | $this->build_editing_form(); |
||
44 | break; |
||
45 | case self::TYPE_ADD: |
||
46 | $this->build_add_form(); |
||
47 | break; |
||
48 | case self::TYPE_MOVE: |
||
49 | $this->build_move_form(); |
||
50 | break; |
||
51 | case self::TYPE_SELECT_COURSE: |
||
52 | $this->build_select_course_form(); |
||
53 | break; |
||
54 | } |
||
55 | |||
56 | $this->setDefaults(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * This function will build a move form that will allow the user to move a category to |
||
61 | * a another. |
||
62 | */ |
||
63 | protected function build_move_form() |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * This function builds an 'add category form, if parent id is 0, it will only |
||
92 | * show courses. |
||
93 | */ |
||
94 | protected function build_add_form() |
||
95 | { |
||
96 | // check if we are a root category |
||
97 | // if so, you can only choose between courses |
||
98 | if ('0' == $this->category_object->get_parent_id()) { |
||
99 | $this->setDefaults( |
||
100 | [ |
||
101 | 'select_course' => $this->category_object->get_course_code(), |
||
102 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
103 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
104 | ] |
||
105 | ); |
||
106 | } else { |
||
107 | $this->setDefaults( |
||
108 | [ |
||
109 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
110 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
111 | ] |
||
112 | ); |
||
113 | $this->addElement( |
||
114 | 'hidden', |
||
115 | 'course_code', |
||
116 | $this->category_object->get_course_code() |
||
117 | ); |
||
118 | } |
||
119 | $this->build_basic_form(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Builds an form to edit a category. |
||
124 | */ |
||
125 | protected function build_editing_form() |
||
126 | { |
||
127 | $skills = $this->category_object->getSkillsForSelect(); |
||
128 | |||
129 | $course_code = api_get_course_id(); |
||
130 | $session_id = api_get_session_id(); |
||
131 | |||
132 | $test_cats = Category::load( |
||
133 | null, |
||
134 | null, |
||
135 | $course_code, |
||
136 | null, |
||
137 | null, |
||
138 | $session_id, |
||
139 | false |
||
140 | ); |
||
141 | |||
142 | $links = null; |
||
143 | if (isset($test_cats[0])) { |
||
144 | $links = $test_cats[0]->get_links(); |
||
145 | } |
||
146 | $grade_model_id = $this->category_object->get_grade_model_id(); |
||
147 | |||
148 | if (empty($links)) { |
||
149 | $grade_model_id = 0; |
||
150 | } |
||
151 | |||
152 | $category_name = $this->category_object->get_name(); |
||
153 | |||
154 | // The main course category: |
||
155 | if (isset($this->category_object) && 0 == $this->category_object->get_parent_id()) { |
||
156 | if (empty($category_name)) { |
||
157 | $category_name = $course_code; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $this->setDefaults( |
||
162 | [ |
||
163 | 'name' => $category_name, |
||
164 | 'description' => $this->category_object->get_description(), |
||
165 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
166 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
167 | 'grade_model_id' => $grade_model_id, |
||
168 | 'skills' => $skills, |
||
169 | 'weight' => $this->category_object->get_weight(), |
||
170 | 'visible' => $this->category_object->is_visible(), |
||
171 | 'certif_min_score' => $this->category_object->getCertificateMinScore(), |
||
172 | 'generate_certificates' => $this->category_object->getGenerateCertificates(), |
||
173 | 'is_requirement' => $this->category_object->getIsRequirement(), |
||
174 | ] |
||
175 | ); |
||
176 | |||
177 | $this->addElement('hidden', 'hid_id', $this->category_object->get_id()); |
||
178 | $this->addElement( |
||
179 | 'hidden', |
||
180 | 'course_code', |
||
181 | $this->category_object->get_course_code() |
||
182 | ); |
||
183 | $this->build_basic_form(); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * This function builds an 'select course' form in the add category process, |
||
188 | * if parent id is 0, it will only show courses. |
||
189 | */ |
||
190 | protected function build_select_course_form() |
||
210 | } |
||
211 | |||
212 | private function build_basic_form() |
||
213 | { |
||
479 |