| Total Complexity | 42 |
| Total Lines | 361 |
| Duplicated Lines | 0 % |
| Changes | 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 | public function __construct() |
||
| 97 | { |
||
| 98 | $this->categories = new ArrayCollection(); |
||
| 99 | $this->relQuizzes = new ArrayCollection(); |
||
| 100 | $this->answers = new ArrayCollection(); |
||
| 101 | $this->options = new ArrayCollection(); |
||
| 102 | $this->ponderation = 0.0; |
||
| 103 | $this->mandatory = 0; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function __toString(): string |
||
| 107 | { |
||
| 108 | return $this->getQuestion(); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function addCategory(CQuizQuestionCategory $category): void |
||
| 112 | { |
||
| 113 | if ($this->categories->contains($category)) { |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | $this->categories->add($category); |
||
| 118 | $category->addQuestion($this); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function updateCategory(CQuizQuestionCategory $category): void |
||
| 122 | { |
||
| 123 | if (0 === $this->categories->count()) { |
||
| 124 | $this->addCategory($category); |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($this->categories->contains($category)) { |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | foreach ($this->categories as $item) { |
||
| 132 | $this->categories->removeElement($item); |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->addCategory($category); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function removeCategory(CQuizQuestionCategory $category): void |
||
| 139 | { |
||
| 140 | if (!$this->categories->contains($category)) { |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->categories->removeElement($category); |
||
| 145 | $category->removeQuestion($this); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function setQuestion(string $question): self |
||
| 149 | { |
||
| 150 | $this->question = $question; |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getQuestion(): string |
||
| 156 | { |
||
| 157 | return $this->question; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function setDescription(?string $description): self |
||
| 161 | { |
||
| 162 | $this->description = $description; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getDescription(): ?string |
||
| 168 | { |
||
| 169 | return $this->description; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function setPonderation(float $ponderation): self |
||
| 173 | { |
||
| 174 | $this->ponderation = $ponderation; |
||
| 175 | |||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get ponderation. |
||
| 181 | * |
||
| 182 | * @return float |
||
| 183 | */ |
||
| 184 | public function getPonderation() |
||
| 185 | { |
||
| 186 | return $this->ponderation; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function setPosition(int $position): self |
||
| 190 | { |
||
| 191 | $this->position = $position; |
||
| 192 | |||
| 193 | return $this; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get position. |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | public function getPosition() |
||
| 202 | { |
||
| 203 | return $this->position; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function setType(int $type): self |
||
| 207 | { |
||
| 208 | $this->type = $type; |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get type. |
||
| 215 | * |
||
| 216 | * @return int |
||
| 217 | */ |
||
| 218 | public function getType() |
||
| 219 | { |
||
| 220 | return $this->type; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function setPicture(string $picture): self |
||
| 224 | { |
||
| 225 | $this->picture = $picture; |
||
| 226 | |||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get picture. |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getPicture() |
||
| 236 | { |
||
| 237 | return $this->picture; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function setLevel(int $level): self |
||
| 241 | { |
||
| 242 | $this->level = $level; |
||
| 243 | |||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get level. |
||
| 249 | * |
||
| 250 | * @return int |
||
| 251 | */ |
||
| 252 | public function getLevel() |
||
| 253 | { |
||
| 254 | return $this->level; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function setExtra(string $extra): self |
||
| 258 | { |
||
| 259 | $this->extra = $extra; |
||
| 260 | |||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get extra. |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function getExtra() |
||
| 270 | { |
||
| 271 | return $this->extra; |
||
| 272 | } |
||
| 273 | |||
| 274 | public function setQuestionCode(string $questionCode): self |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get questionCode. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getQuestionCode() |
||
| 287 | { |
||
| 288 | return $this->questionCode; |
||
| 289 | } |
||
| 290 | |||
| 291 | public function getFeedback(): ?string |
||
| 294 | } |
||
| 295 | |||
| 296 | public function setFeedback(?string $feedback): self |
||
| 297 | { |
||
| 298 | $this->feedback = $feedback; |
||
| 299 | |||
| 300 | return $this; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return CQuizQuestionCategory[]|Collection |
||
| 305 | */ |
||
| 306 | public function getCategories(): array|Collection |
||
| 307 | { |
||
| 308 | return $this->categories; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return CQuizRelQuestion[]|Collection |
||
| 313 | */ |
||
| 314 | public function getRelQuizzes(): array|Collection |
||
| 315 | { |
||
| 316 | return $this->relQuizzes; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return CQuizAnswer[]|Collection |
||
| 321 | */ |
||
| 322 | public function getAnswers(): array|Collection |
||
| 323 | { |
||
| 324 | return $this->answers; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getMandatory(): int |
||
| 328 | { |
||
| 329 | return $this->mandatory; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return CQuizQuestionOption[]|Collection |
||
| 334 | */ |
||
| 335 | public function getOptions(): array|Collection |
||
| 336 | { |
||
| 337 | return $this->options; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param CQuizQuestionOption[]|Collection $options |
||
| 342 | */ |
||
| 343 | public function setOptions(array|Collection $options): self |
||
| 344 | { |
||
| 345 | $this->options = $options; |
||
| 346 | |||
| 347 | return $this; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get iid. |
||
| 352 | */ |
||
| 353 | public function getIid(): ?int |
||
| 354 | { |
||
| 355 | return $this->iid; |
||
| 356 | } |
||
| 357 | |||
| 358 | public function getDuration(): ?int |
||
| 359 | { |
||
| 360 | return $this->duration; |
||
| 361 | } |
||
| 362 | |||
| 363 | public function setDuration(?int $duration): self |
||
| 368 | } |
||
| 369 | |||
| 370 | public function getResourceIdentifier(): int|Uuid |
||
| 373 | } |
||
| 374 | |||
| 375 | public function getResourceName(): string |
||
| 376 | { |
||
| 377 | return $this->getQuestion(); |
||
| 378 | } |
||
| 379 | |||
| 380 | public function setResourceName(string $name): self |
||
| 383 | } |
||
| 384 | } |
||
| 385 |