| Total Complexity | 48 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Portfolio 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 Portfolio, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Portfolio |
||
| 31 | { |
||
| 32 | public const TYPE_ITEM = 1; |
||
| 33 | public const TYPE_COMMENT = 2; |
||
| 34 | |||
| 35 | public const VISIBILITY_HIDDEN = 0; |
||
| 36 | public const VISIBILITY_VISIBLE = 1; |
||
| 37 | public const VISIBILITY_HIDDEN_EXCEPT_TEACHER = 2; |
||
| 38 | public const VISIBILITY_PER_USER = 3; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @ORM\Column(name="id", type="integer") |
||
| 42 | * @ORM\Id |
||
| 43 | * @ORM\GeneratedValue |
||
| 44 | */ |
||
| 45 | protected ?int $id; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @ORM\Column(name="title", type="string", length=255) |
||
| 49 | */ |
||
| 50 | protected string $title; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @ORM\Column(name="content", type="text") |
||
| 54 | */ |
||
| 55 | protected string $content; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User") |
||
| 59 | * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") |
||
| 60 | */ |
||
| 61 | protected User $user; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course") |
||
| 65 | * @ORM\JoinColumn(name="c_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 66 | */ |
||
| 67 | protected ?Course $course = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session") |
||
| 71 | * @ORM\JoinColumn(name="session_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 72 | */ |
||
| 73 | protected ?Session $session = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @ORM\Column(name="creation_date", type="datetime") |
||
| 77 | */ |
||
| 78 | protected DateTime $creationDate; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @ORM\Column(name="update_date", type="datetime") |
||
| 82 | */ |
||
| 83 | protected DateTime $updateDate; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @ORM\Column(name="visibility", type="smallint", options={"default": 1}) |
||
| 87 | */ |
||
| 88 | protected int $visibility = self::VISIBILITY_VISIBLE; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\PortfolioCategory", inversedBy="items") |
||
| 92 | * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 93 | */ |
||
| 94 | protected ?PortfolioCategory $category; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\PortfolioComment", mappedBy="item") |
||
| 98 | */ |
||
| 99 | private Collection $comments; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @ORM\Column(name="origin", type="integer", nullable=true) |
||
| 103 | */ |
||
| 104 | private ?int $origin = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @ORM\Column(name="origin_type", type="integer", nullable=true) |
||
| 108 | */ |
||
| 109 | private ?int $originType = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @ORM\Column(name="score", type="float", nullable=true) |
||
| 113 | */ |
||
| 114 | private ?float $score = null; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @ORM\Column(name="is_highlighted", type="boolean", options={"default": false}) |
||
| 118 | */ |
||
| 119 | private bool $isHighlighted = false; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @ORM\Column(name="is_template", type="boolean", options={"default": false}) |
||
| 123 | */ |
||
| 124 | private bool $isTemplate = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Portfolio", inversedBy="duplicates") |
||
| 128 | * ORM\JoinColumn(name="duplicated_from", onDelete="SET NULL") |
||
| 129 | */ |
||
| 130 | private ?Portfolio $duplicatedFrom = null; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var Collection<int, Portfolio> |
||
| 134 | * ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Portfolio", mappedBy="duplicatedFrom") |
||
| 135 | */ |
||
| 136 | private Collection $duplicates; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Portfolio constructor. |
||
| 140 | */ |
||
| 141 | public function __construct() |
||
| 142 | { |
||
| 143 | $this->category = null; |
||
| 144 | $this->comments = new ArrayCollection(); |
||
| 145 | $this->duplicates = new ArrayCollection(); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function setUser(User $user): Portfolio |
||
| 149 | { |
||
| 150 | $this->user = $user; |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getUser(): User |
||
| 156 | { |
||
| 157 | return $this->user; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function setCourse(?Course $course = null): Portfolio |
||
| 161 | { |
||
| 162 | $this->course = $course; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getCourse(): ?Course |
||
| 168 | { |
||
| 169 | return $this->course; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function getSession(): ?Session |
||
| 173 | { |
||
| 174 | return $this->session; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function setSession(?Session $session = null): Portfolio |
||
| 178 | { |
||
| 179 | $this->session = $session; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function setTitle(string $title): Portfolio |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getTitle(bool $stripTags = false): string |
||
| 192 | { |
||
| 193 | if ($stripTags) { |
||
| 194 | return strip_tags($this->title); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this->title; |
||
| 198 | } |
||
| 199 | |||
| 200 | public function setContent(string $content): Portfolio |
||
| 201 | { |
||
| 202 | $this->content = $content; |
||
| 203 | |||
| 204 | return $this; |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getContent(): string |
||
| 208 | { |
||
| 209 | return $this->content; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function setCreationDate(DateTime $creationDate): Portfolio |
||
| 213 | { |
||
| 214 | $this->creationDate = $creationDate; |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function getCreationDate(): DateTime |
||
| 220 | { |
||
| 221 | return $this->creationDate; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function setUpdateDate(DateTime $updateDate): Portfolio |
||
| 225 | { |
||
| 226 | $this->updateDate = $updateDate; |
||
| 227 | |||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function getUpdateDate(): DateTime |
||
| 232 | { |
||
| 233 | return $this->updateDate; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function getId(): ?int |
||
| 237 | { |
||
| 238 | return $this->id; |
||
| 239 | } |
||
| 240 | |||
| 241 | public function setVisibility(int $visibility): Portfolio |
||
| 242 | { |
||
| 243 | $this->visibility = $visibility; |
||
| 244 | |||
| 245 | return $this; |
||
| 246 | } |
||
| 247 | |||
| 248 | public function getVisibility(): int |
||
| 249 | { |
||
| 250 | return $this->visibility; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function getCategory(): ?PortfolioCategory |
||
| 254 | { |
||
| 255 | return $this->category; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function setCategory(?PortfolioCategory $category = null): Portfolio |
||
| 259 | { |
||
| 260 | $this->category = $category; |
||
| 261 | |||
| 262 | return $this; |
||
| 263 | } |
||
| 264 | |||
| 265 | public function getComments(): Collection |
||
| 266 | { |
||
| 267 | return $this->comments; |
||
| 268 | } |
||
| 269 | |||
| 270 | public function getLastComments(int $number = 3, bool $avoidPerUserVisibility = false): Collection |
||
| 271 | { |
||
| 272 | $criteria = Criteria::create(); |
||
| 273 | $criteria |
||
| 274 | ->orderBy(['date' => 'DESC']) |
||
| 275 | ->setMaxResults($number); |
||
| 276 | |||
| 277 | if ($avoidPerUserVisibility) { |
||
| 278 | $criteria->where( |
||
| 279 | Criteria::expr()->neq('visibility', PortfolioComment::VISIBILITY_PER_USER) |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | return $this->comments->matching($criteria); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function getOrigin(): ?int |
||
| 287 | { |
||
| 288 | return $this->origin; |
||
| 289 | } |
||
| 290 | |||
| 291 | public function setOrigin(?int $origin): Portfolio |
||
| 292 | { |
||
| 293 | $this->origin = $origin; |
||
| 294 | |||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | public function getOriginType(): ?int |
||
| 299 | { |
||
| 300 | return $this->originType; |
||
| 301 | } |
||
| 302 | |||
| 303 | public function setOriginType(?int $originType): Portfolio |
||
| 304 | { |
||
| 305 | $this->originType = $originType; |
||
| 306 | |||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | public function getExcerpt(int $count = 380): string |
||
| 311 | { |
||
| 312 | return api_get_short_text_from_html($this->content, $count); |
||
| 313 | } |
||
| 314 | |||
| 315 | public function getScore(): ?float |
||
| 316 | { |
||
| 317 | return $this->score; |
||
| 318 | } |
||
| 319 | |||
| 320 | public function setScore(?float $score): void |
||
| 321 | { |
||
| 322 | $this->score = $score; |
||
| 323 | } |
||
| 324 | |||
| 325 | public function isHighlighted(): bool |
||
| 326 | { |
||
| 327 | return $this->isHighlighted; |
||
| 328 | } |
||
| 329 | |||
| 330 | public function setIsHighlighted(bool $isHighlighted): Portfolio |
||
| 331 | { |
||
| 332 | $this->isHighlighted = $isHighlighted; |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function isTemplate(): bool |
||
| 338 | { |
||
| 339 | return $this->isTemplate; |
||
| 340 | } |
||
| 341 | |||
| 342 | public function setIsTemplate(bool $isTemplate): Portfolio |
||
| 343 | { |
||
| 344 | $this->isTemplate = $isTemplate; |
||
| 345 | |||
| 346 | return $this; |
||
| 347 | } |
||
| 348 | |||
| 349 | public function getDuplicatedFrom(): ?Portfolio |
||
| 350 | { |
||
| 351 | return $this->duplicatedFrom; |
||
| 352 | } |
||
| 353 | |||
| 354 | public function setDuplicatedFrom(?Portfolio $duplicatedFrom): Portfolio |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return Collection<int, Portfolio> |
||
| 363 | */ |
||
| 364 | public function getDuplicates(): Collection |
||
| 365 | { |
||
| 366 | return $this->duplicates; |
||
| 367 | } |
||
| 368 | |||
| 369 | public function addDuplicate(Portfolio $duplicate): Portfolio |
||
| 370 | { |
||
| 371 | if (!$this->duplicates->contains($duplicate)) { |
||
| 372 | $this->duplicates->add($duplicate); |
||
| 373 | $duplicate->setDuplicatedFrom($this); |
||
| 374 | } |
||
| 375 | |||
| 376 | return $this; |
||
| 377 | } |
||
| 378 | |||
| 379 | public function removeDuplicate(Portfolio $duplicate): Portfolio |
||
| 380 | { |
||
| 381 | if ($this->duplicates->removeElement($duplicate)) { |
||
| 382 | // set the owning side to null (unless already changed) |
||
| 383 | if ($duplicate->getDuplicatedFrom() === $this) { |
||
| 384 | $duplicate->setDuplicatedFrom(null); |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | public function hasDuplicates(): bool |
||
| 392 | { |
||
| 393 | return $this->duplicates->count() > 0; |
||
| 394 | } |
||
| 395 | |||
| 396 | public function isDuplicated(): bool |
||
| 397 | { |
||
| 398 | return null !== $this->duplicatedFrom; |
||
| 399 | } |
||
| 400 | |||
| 401 | public function isDuplicatedInSession(Session $session): bool |
||
| 404 | } |
||
| 405 | |||
| 406 | public function isDuplicatedInSessionId(int $sessionId): bool |
||
| 407 | { |
||
| 408 | return $this->duplicates->exists(fn($key, Portfolio $duplicated): bool => $duplicated->session && $duplicated->session->getId() === $sessionId); |
||
| 409 | } |
||
| 410 | } |
||
| 411 |