Total Complexity | 44 |
Total Lines | 376 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CQuizQuestion 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 CQuizQuestion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | #[ORM\Table(name: 'c_quiz_question')] |
||
23 | #[ORM\Index(name: 'position', columns: ['position'])] |
||
24 | #[ORM\Entity(repositoryClass: CQuizQuestionRepository::class)] |
||
25 | class CQuizQuestion extends AbstractResource implements ResourceInterface, Stringable |
||
26 | { |
||
27 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
28 | #[ORM\Id] |
||
29 | #[ORM\GeneratedValue] |
||
30 | protected ?int $iid = null; |
||
31 | |||
32 | #[Assert\NotBlank] |
||
33 | #[ORM\Column(name: 'question', type: 'text', nullable: false)] |
||
34 | protected string $question; |
||
35 | |||
36 | #[ORM\Column(name: 'description', type: 'text', nullable: true)] |
||
37 | protected ?string $description = null; |
||
38 | |||
39 | #[ORM\Column(name: 'ponderation', type: 'float', precision: 6, scale: 2, nullable: false, options: ['default' => 0])] |
||
40 | protected float $ponderation; |
||
41 | |||
42 | #[ORM\Column(name: 'position', type: 'integer', nullable: false)] |
||
43 | protected int $position; |
||
44 | |||
45 | #[ORM\Column(name: 'type', type: 'integer', nullable: false)] |
||
46 | protected int $type; |
||
47 | |||
48 | #[ORM\Column(name: 'picture', type: 'string', length: 50, nullable: true)] |
||
49 | protected ?string $picture = null; |
||
50 | |||
51 | #[ORM\Column(name: 'level', type: 'integer', nullable: false)] |
||
52 | protected int $level; |
||
53 | |||
54 | #[ORM\Column(name: 'feedback', type: 'text', nullable: true)] |
||
55 | protected ?string $feedback = null; |
||
56 | |||
57 | #[ORM\Column(name: 'extra', type: 'string', length: 255, nullable: true)] |
||
58 | protected ?string $extra = null; |
||
59 | |||
60 | #[ORM\Column(name: 'question_code', type: 'string', length: 10, nullable: true)] |
||
61 | protected ?string $questionCode = null; |
||
62 | |||
63 | /** |
||
64 | * @var Collection|CQuizQuestionCategory[] |
||
65 | */ |
||
66 | #[ORM\JoinTable(name: 'c_quiz_question_rel_category')] |
||
67 | #[ORM\JoinColumn(name: 'question_id', referencedColumnName: 'iid')] |
||
68 | #[ORM\InverseJoinColumn(name: 'category_id', referencedColumnName: 'iid')] |
||
69 | #[ORM\ManyToMany(targetEntity: CQuizQuestionCategory::class, inversedBy: 'questions')] |
||
70 | protected Collection $categories; |
||
71 | |||
72 | /** |
||
73 | * @var Collection|CQuizRelQuestion[] |
||
74 | */ |
||
75 | #[ORM\OneToMany(targetEntity: CQuizRelQuestion::class, mappedBy: 'question', cascade: ['persist'])] |
||
76 | protected Collection $relQuizzes; |
||
77 | |||
78 | /** |
||
79 | * @var Collection|CQuizAnswer[] |
||
80 | */ |
||
81 | #[ORM\OneToMany(targetEntity: CQuizAnswer::class, mappedBy: 'question', cascade: ['persist'])] |
||
82 | protected Collection $answers; |
||
83 | |||
84 | /** |
||
85 | * @var Collection|CQuizQuestionOption[] |
||
86 | */ |
||
87 | #[ORM\OneToMany(targetEntity: CQuizQuestionOption::class, mappedBy: 'question', cascade: ['persist'])] |
||
88 | protected Collection $options; |
||
89 | |||
90 | #[ORM\Column(name: 'mandatory', type: 'integer')] |
||
91 | protected int $mandatory; |
||
92 | |||
93 | #[ORM\Column(name: 'duration', type: 'integer', nullable: true)] |
||
94 | protected ?int $duration = null; |
||
95 | |||
96 | #[ORM\Column(name: 'parent_media_id', type: 'integer', nullable: true)] |
||
97 | protected ?int $parentMediaId = null; |
||
98 | |||
99 | public function __construct() |
||
100 | { |
||
101 | $this->categories = new ArrayCollection(); |
||
102 | $this->relQuizzes = new ArrayCollection(); |
||
103 | $this->answers = new ArrayCollection(); |
||
104 | $this->options = new ArrayCollection(); |
||
105 | $this->ponderation = 0.0; |
||
106 | $this->mandatory = 0; |
||
107 | } |
||
108 | |||
109 | public function __toString(): string |
||
110 | { |
||
111 | return $this->getQuestion(); |
||
112 | } |
||
113 | |||
114 | public function addCategory(CQuizQuestionCategory $category): void |
||
115 | { |
||
116 | if ($this->categories->contains($category)) { |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | $this->categories->add($category); |
||
121 | $category->addQuestion($this); |
||
122 | } |
||
123 | |||
124 | public function updateCategory(CQuizQuestionCategory $category): void |
||
125 | { |
||
126 | if (0 === $this->categories->count()) { |
||
127 | $this->addCategory($category); |
||
128 | } |
||
129 | |||
130 | if ($this->categories->contains($category)) { |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | foreach ($this->categories as $item) { |
||
135 | $this->categories->removeElement($item); |
||
136 | } |
||
137 | |||
138 | $this->addCategory($category); |
||
139 | } |
||
140 | |||
141 | public function removeCategory(CQuizQuestionCategory $category): void |
||
142 | { |
||
143 | if (!$this->categories->contains($category)) { |
||
144 | return; |
||
145 | } |
||
146 | |||
147 | $this->categories->removeElement($category); |
||
148 | $category->removeQuestion($this); |
||
149 | } |
||
150 | |||
151 | public function setQuestion(string $question): self |
||
152 | { |
||
153 | $this->question = $question; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | public function getQuestion(): string |
||
159 | { |
||
160 | return $this->question; |
||
161 | } |
||
162 | |||
163 | public function setDescription(?string $description): self |
||
168 | } |
||
169 | |||
170 | public function getDescription(): ?string |
||
171 | { |
||
172 | return $this->description; |
||
173 | } |
||
174 | |||
175 | public function setPonderation(float $ponderation): self |
||
176 | { |
||
177 | $this->ponderation = $ponderation; |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Get ponderation. |
||
184 | * |
||
185 | * @return float |
||
186 | */ |
||
187 | public function getPonderation() |
||
188 | { |
||
189 | return $this->ponderation; |
||
190 | } |
||
191 | |||
192 | public function setPosition(int $position): self |
||
193 | { |
||
194 | $this->position = $position; |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Get position. |
||
201 | * |
||
202 | * @return int |
||
203 | */ |
||
204 | public function getPosition() |
||
205 | { |
||
206 | return $this->position; |
||
207 | } |
||
208 | |||
209 | public function setType(int $type): self |
||
210 | { |
||
211 | $this->type = $type; |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get type. |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | public function getType() |
||
222 | { |
||
223 | return $this->type; |
||
224 | } |
||
225 | |||
226 | public function setPicture(string $picture): self |
||
227 | { |
||
228 | $this->picture = $picture; |
||
229 | |||
230 | return $this; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Get picture. |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getPicture() |
||
239 | { |
||
240 | return $this->picture; |
||
241 | } |
||
242 | |||
243 | public function setLevel(int $level): self |
||
244 | { |
||
245 | $this->level = $level; |
||
246 | |||
247 | return $this; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Get level. |
||
252 | * |
||
253 | * @return int |
||
254 | */ |
||
255 | public function getLevel() |
||
256 | { |
||
257 | return $this->level; |
||
258 | } |
||
259 | |||
260 | public function setExtra(?string $extra): self |
||
261 | { |
||
262 | $this->extra = $extra; |
||
263 | |||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Get extra. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getExtra() |
||
273 | { |
||
274 | return $this->extra; |
||
275 | } |
||
276 | |||
277 | public function setQuestionCode(string $questionCode): self |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get questionCode. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public function getQuestionCode() |
||
290 | { |
||
291 | return $this->questionCode; |
||
292 | } |
||
293 | |||
294 | public function getFeedback(): ?string |
||
297 | } |
||
298 | |||
299 | public function setFeedback(?string $feedback): self |
||
300 | { |
||
301 | $this->feedback = $feedback; |
||
302 | |||
303 | return $this; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @return CQuizQuestionCategory[]|Collection |
||
308 | */ |
||
309 | public function getCategories(): array|Collection |
||
310 | { |
||
311 | return $this->categories; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @return CQuizRelQuestion[]|Collection |
||
316 | */ |
||
317 | public function getRelQuizzes(): array|Collection |
||
318 | { |
||
319 | return $this->relQuizzes; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @return CQuizAnswer[]|Collection |
||
324 | */ |
||
325 | public function getAnswers(): array|Collection |
||
326 | { |
||
327 | return $this->answers; |
||
328 | } |
||
329 | |||
330 | public function getMandatory(): int |
||
331 | { |
||
332 | return $this->mandatory; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @return CQuizQuestionOption[]|Collection |
||
337 | */ |
||
338 | public function getOptions(): array|Collection |
||
339 | { |
||
340 | return $this->options; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param CQuizQuestionOption[]|Collection $options |
||
345 | */ |
||
346 | public function setOptions(array|Collection $options): self |
||
347 | { |
||
348 | $this->options = $options; |
||
349 | |||
350 | return $this; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Get iid. |
||
355 | */ |
||
356 | public function getIid(): ?int |
||
357 | { |
||
358 | return $this->iid; |
||
359 | } |
||
360 | |||
361 | public function getDuration(): ?int |
||
362 | { |
||
363 | return $this->duration; |
||
364 | } |
||
365 | |||
366 | public function setDuration(?int $duration): self |
||
371 | } |
||
372 | |||
373 | public function getParentMediaId(): ?int |
||
374 | { |
||
375 | return $this->parentMediaId; |
||
376 | } |
||
377 | |||
378 | public function setParentMediaId(?int $parentMediaId): self |
||
379 | { |
||
380 | $this->parentMediaId = $parentMediaId; |
||
381 | |||
382 | return $this; |
||
383 | } |
||
384 | |||
385 | public function getResourceIdentifier(): int|Uuid |
||
388 | } |
||
389 | |||
390 | public function getResourceName(): string |
||
391 | { |
||
392 | return $this->getQuestion(); |
||
393 | } |
||
394 | |||
395 | public function setResourceName(string $name): self |
||
398 | } |
||
399 | } |
||
400 |