Total Complexity | 40 |
Total Lines | 306 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like Trick 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 Trick, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Trick extends AbstractAppEntity |
||
20 | { |
||
21 | |||
22 | const NUMBER_OF_DISPLAYED_TRICKS = 10; |
||
23 | |||
24 | /** |
||
25 | * @ORM\Id() |
||
26 | * @ORM\GeneratedValue() |
||
27 | * @ORM\Column(type="integer") |
||
28 | */ |
||
29 | private $id; |
||
30 | |||
31 | /** |
||
32 | * @ORM\Column(type="string", length=255) |
||
33 | * @Gedmo\Versioned |
||
34 | * @Assert\Length( |
||
35 | * min=5, |
||
36 | * max=255, |
||
37 | * minMessage = "Title must be at least {{ limit }} characters", |
||
38 | * maxMessage = "Title can not exceed {{ limit }} characters" |
||
39 | * ) |
||
40 | */ |
||
41 | private $name; |
||
42 | |||
43 | /** |
||
44 | * @ORM\Column(type="text") |
||
45 | * @Gedmo\Versioned |
||
46 | */ |
||
47 | private $text; |
||
48 | |||
49 | /** |
||
50 | * @ORM\Column(type="string", length=255, unique=true) |
||
51 | * @Gedmo\Slug(fields={"name"}) |
||
52 | */ |
||
53 | private $slug; |
||
54 | |||
55 | /** |
||
56 | * @ORM\Column(type="datetime") |
||
57 | * @Gedmo\Timestampable(on="create") |
||
58 | */ |
||
59 | private $createdAt; |
||
60 | |||
61 | /** |
||
62 | * @ORM\Column(type="datetime") |
||
63 | * @Gedmo\Timestampable(on="update") |
||
64 | */ |
||
65 | private $editedAt; |
||
66 | |||
67 | /** |
||
68 | * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="tricks") |
||
69 | * @ORM\JoinColumn(nullable=false) |
||
70 | */ |
||
71 | private $category; |
||
72 | |||
73 | /** |
||
74 | * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="trick", cascade={"persist"}) |
||
75 | */ |
||
76 | private $tags; |
||
77 | |||
78 | /** |
||
79 | * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="trick", cascade={"remove", "persist"}, orphanRemoval=true) |
||
80 | * @ORM\OrderBy({"createdAt" = "DESC"}) |
||
81 | * |
||
82 | */ |
||
83 | private $comments; |
||
84 | |||
85 | /** |
||
86 | * @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="trick", cascade={"remove", "persist"}, orphanRemoval=true) |
||
87 | * @Assert\Valid |
||
88 | */ |
||
89 | private $images; |
||
90 | |||
91 | /** |
||
92 | * @ORM\OneToMany(targetEntity="App\Entity\Video", mappedBy="trick", cascade={"remove", "persist"}, orphanRemoval=true) |
||
93 | * @Assert\Valid |
||
94 | */ |
||
95 | private $videos; |
||
96 | |||
97 | public function __construct() |
||
98 | { |
||
99 | $this->tags = new ArrayCollection(); |
||
100 | $this->comments = new ArrayCollection(); |
||
101 | $this->images = new ArrayCollection(); |
||
102 | $this->videos = new ArrayCollection(); |
||
103 | } |
||
104 | |||
105 | public function getId(): ?int |
||
106 | { |
||
107 | return $this->id; |
||
108 | } |
||
109 | |||
110 | public function getName(): ?string |
||
111 | { |
||
112 | return $this->name; |
||
113 | } |
||
114 | |||
115 | public function setName(string $name): self |
||
116 | { |
||
117 | $this->name = $name; |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | public function getText(): ?string |
||
123 | { |
||
124 | return $this->text; |
||
125 | } |
||
126 | |||
127 | public function setText(string $text): self |
||
128 | { |
||
129 | $this->text = $text; |
||
130 | |||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | public function getSlug(): ?string |
||
135 | { |
||
136 | return $this->slug; |
||
137 | } |
||
138 | |||
139 | public function setSlug(string $slug): self |
||
140 | { |
||
141 | $this->slug = $slug; |
||
142 | |||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | public function getCreatedAt(): ?DateTimeInterface |
||
147 | { |
||
148 | return $this->createdAt; |
||
149 | } |
||
150 | |||
151 | public function setCreatedAt(DateTimeInterface $createdAt): self |
||
152 | { |
||
153 | $this->createdAt = $createdAt; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | public function getEditedAt(): ?DateTimeInterface |
||
159 | { |
||
160 | return $this->editedAt; |
||
161 | } |
||
162 | |||
163 | public function setEditedAt(DateTimeInterface $editedAt): self |
||
164 | { |
||
165 | $this->editedAt = $editedAt; |
||
166 | |||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | public function getCategory(): ?Category |
||
171 | { |
||
172 | return $this->category; |
||
173 | } |
||
174 | |||
175 | public function setCategory(?Category $category): self |
||
176 | { |
||
177 | $this->category = $category; |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | public function __toString() |
||
183 | { |
||
184 | return $this->name; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return Collection|Tag[] |
||
189 | */ |
||
190 | public function getTags(): Collection |
||
191 | { |
||
192 | return $this->tags; |
||
193 | } |
||
194 | |||
195 | public function getTagsJson() |
||
200 | |||
201 | } |
||
202 | |||
203 | public function addTag(Tag $tag): self |
||
204 | { |
||
205 | if (!$this->tags->contains($tag)) { |
||
206 | $this->tags[] = $tag; |
||
207 | $tag->addTrick($this); |
||
208 | } |
||
209 | |||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | public function removeTag(Tag $tag): self |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return Collection|Comment[] |
||
225 | */ |
||
226 | public function getComments(): Collection |
||
227 | { |
||
228 | return $this->comments; |
||
229 | } |
||
230 | |||
231 | public function addComment(Comment $comment): self |
||
232 | { |
||
233 | if (!$this->comments->contains($comment)) { |
||
234 | $this->comments[] = $comment; |
||
235 | $comment->setTrick($this); |
||
236 | } |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | public function removeComment(Comment $comment): self |
||
242 | { |
||
243 | if ($this->comments->contains($comment)) { |
||
244 | $this->comments->removeElement($comment); |
||
245 | // set the owning side to null (unless already changed) |
||
246 | if ($comment->getTrick() === $this) { |
||
247 | $comment->setTrick(null); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return Collection|Image[] |
||
256 | */ |
||
257 | public function getImages(): Collection |
||
258 | { |
||
259 | return $this->images; |
||
260 | } |
||
261 | |||
262 | public function addImage(Image $image): self |
||
263 | { |
||
264 | if (!$this->images->contains($image)) { |
||
265 | $this->images[] = $image; |
||
266 | $image->setTrick($this); |
||
267 | } |
||
268 | |||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | public function removeImage(Image $image): self |
||
273 | { |
||
274 | if ($this->images->contains($image)) { |
||
275 | $this->images->removeElement($image); |
||
276 | // set the owning side to null (unless already changed) |
||
277 | if ($image->getTrick() === $this) { |
||
278 | $image->setTrick(null); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | return $this; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Get the primary images associated with the trick |
||
287 | * @return Collection|null |
||
288 | */ |
||
289 | public function getPrimaryImages(): ?Collection |
||
293 | }); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @return Collection|Video[] |
||
298 | */ |
||
299 | public function getVideos(): Collection |
||
300 | { |
||
301 | return $this->videos; |
||
302 | } |
||
303 | |||
304 | public function addVideo(Video $video): self |
||
305 | { |
||
306 | if (!$this->videos->contains($video)) { |
||
307 | $this->videos[] = $video; |
||
308 | $video->setTrick($this); |
||
309 | } |
||
310 | |||
312 | } |
||
313 | |||
314 | public function removeVideo(Video $video): self |
||
315 | { |
||
316 | if ($this->videos->contains($video)) { |
||
325 | } |
||
326 | } |
||
327 |