| Total Complexity | 42 |
| Total Lines | 446 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ResourceNode 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 ResourceNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ResourceNode |
||
| 25 | { |
||
| 26 | use TimestampableEntity; |
||
| 27 | |||
| 28 | public const PATH_SEPARATOR = '`'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @ORM\Id |
||
| 32 | * @ORM\Column(type="integer") |
||
| 33 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 34 | */ |
||
| 35 | protected $id; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @Assert\NotBlank() |
||
| 39 | * |
||
| 40 | * @Gedmo\TreePathSource |
||
| 41 | * @ORM\Column(name="slug", type="string", length=255, nullable=true) |
||
| 42 | */ |
||
| 43 | protected $slug; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes") |
||
| 47 | * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false) |
||
| 48 | */ |
||
| 49 | protected $resourceType; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode", |
||
| 53 | * cascade={"remove"}) |
||
| 54 | */ |
||
| 55 | protected $resourceLinks; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ResourceFile |
||
| 59 | * |
||
| 60 | * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", orphanRemoval=true) |
||
| 61 | * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 62 | */ |
||
| 63 | protected $resourceFile; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @ORM\ManyToOne( |
||
| 67 | * targetEntity="Chamilo\UserBundle\Entity\User", |
||
| 68 | * inversedBy="resourceNodes", |
||
| 69 | * cascade={"persist"} |
||
| 70 | * ) |
||
| 71 | * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) |
||
| 72 | */ |
||
| 73 | protected $creator; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @Gedmo\TreeParent |
||
| 77 | * |
||
| 78 | * @ORM\ManyToOne( |
||
| 79 | * targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", |
||
| 80 | * inversedBy="children" |
||
| 81 | * ) |
||
| 82 | * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")}) |
||
| 83 | */ |
||
| 84 | protected $parent; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @Gedmo\TreeLevel |
||
| 88 | * |
||
| 89 | * @ORM\Column(name="level", type="integer", nullable=true) |
||
| 90 | */ |
||
| 91 | protected $level; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var ResourceNode[] |
||
| 95 | * |
||
| 96 | * @ORM\OneToMany( |
||
| 97 | * targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", |
||
| 98 | * mappedBy="parent" |
||
| 99 | * ) |
||
| 100 | * @ORM\OrderBy({"id" = "ASC"}) |
||
| 101 | */ |
||
| 102 | protected $children; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @Gedmo\TreePath(separator="`") |
||
| 106 | * |
||
| 107 | * @ORM\Column(name="path", type="string", length=3000, nullable=true) |
||
| 108 | */ |
||
| 109 | protected $path; |
||
| 110 | |||
| 111 | //protected $pathForCreationLog = ''; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor. |
||
| 115 | */ |
||
| 116 | public function __construct() |
||
| 117 | { |
||
| 118 | $this->children = new ArrayCollection(); |
||
|
|
|||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function __toString() |
||
| 125 | { |
||
| 126 | return (string) $this->getPathForDisplay(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the resource id. |
||
| 131 | * |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | public function getId() |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param int $id |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function setId($id) |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the resource creator. |
||
| 153 | * |
||
| 154 | * @return User |
||
| 155 | */ |
||
| 156 | public function getCreator() |
||
| 157 | { |
||
| 158 | return $this->creator; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sets the resource creator. |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setCreator(User $creator) |
||
| 167 | { |
||
| 168 | $this->creator = $creator; |
||
| 169 | |||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the children resource instances. |
||
| 175 | * |
||
| 176 | * @return ArrayCollection |
||
| 177 | */ |
||
| 178 | public function getChildren() |
||
| 179 | { |
||
| 180 | return $this->children; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets the parent resource. |
||
| 185 | * |
||
| 186 | * @param ResourceNode $parent |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setParent(ResourceNode $parent = null) |
||
| 191 | { |
||
| 192 | $this->parent = $parent; |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the parent resource. |
||
| 199 | * |
||
| 200 | * @return ResourceNode |
||
| 201 | */ |
||
| 202 | public function getParent() |
||
| 203 | { |
||
| 204 | return $this->parent; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return the lvl value of the resource in the tree. |
||
| 209 | * |
||
| 210 | * @return int |
||
| 211 | */ |
||
| 212 | public function getLevel() |
||
| 213 | { |
||
| 214 | return $this->level; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the "raw" path of the resource |
||
| 219 | * (the path merge names and ids of all items). |
||
| 220 | * Eg.: "Root-1/subdir-2/file.txt-3/". |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getPath() |
||
| 225 | { |
||
| 226 | return $this->path; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the path cleaned from its ids. |
||
| 231 | * Eg.: "Root/subdir/file.txt". |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getPathForDisplay() |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getPathForDisplayRemoveBase(string $base) |
||
| 244 | { |
||
| 245 | $path = str_replace($base, '', $this->path); |
||
| 246 | |||
| 247 | return self::convertPathForDisplay($path); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | public function getSlug() |
||
| 254 | { |
||
| 255 | return $this->slug; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param mixed $slug |
||
| 260 | * |
||
| 261 | * @return ResourceNode |
||
| 262 | */ |
||
| 263 | public function setSlug($slug) |
||
| 264 | { |
||
| 265 | if (strpos(self::PATH_SEPARATOR, $slug) !== false) { |
||
| 266 | throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.'); |
||
| 267 | } |
||
| 268 | |||
| 269 | $this->slug = $slug; |
||
| 270 | |||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Convert a path for display: remove ids. |
||
| 276 | * |
||
| 277 | * @param string $path |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function convertPathForDisplay($path) |
||
| 282 | { |
||
| 283 | /*$pathForDisplay = preg_replace( |
||
| 284 | '/-\d+'.self::PATH_SEPARATOR.'/', |
||
| 285 | ' / ', |
||
| 286 | $path |
||
| 287 | ); |
||
| 288 | if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) { |
||
| 289 | $pathForDisplay = substr_replace($pathForDisplay, '', -3); |
||
| 290 | } |
||
| 291 | */ |
||
| 292 | $pathForDisplay = preg_replace( |
||
| 293 | '/-\d+'.self::PATH_SEPARATOR.'/', |
||
| 294 | '/', |
||
| 295 | $path |
||
| 296 | ); |
||
| 297 | |||
| 298 | if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) { |
||
| 299 | $pathForDisplay = substr_replace($pathForDisplay, '', -1); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $pathForDisplay; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * This is required for logging the resource path at the creation. |
||
| 307 | * Do not use this function otherwise. |
||
| 308 | */ |
||
| 309 | public function setPathForCreationLog($path) |
||
| 310 | { |
||
| 311 | $this->pathForCreationLog = $path; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * This is required for logging the resource path at the creation. |
||
| 316 | * Do not use this function otherwise. |
||
| 317 | * |
||
| 318 | * @return type |
||
| 319 | */ |
||
| 320 | public function getPathForCreationLog() |
||
| 321 | { |
||
| 322 | return $this->pathForCreationLog; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return ResourceType |
||
| 327 | */ |
||
| 328 | public function getResourceType() |
||
| 329 | { |
||
| 330 | return $this->resourceType; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param ResourceType $resourceType |
||
| 335 | * |
||
| 336 | * @return ResourceNode |
||
| 337 | */ |
||
| 338 | public function setResourceType($resourceType) |
||
| 339 | { |
||
| 340 | $this->resourceType = $resourceType; |
||
| 341 | |||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return ArrayCollection|ResourceLink[] |
||
| 347 | */ |
||
| 348 | public function getResourceLinks() |
||
| 349 | { |
||
| 350 | return $this->resourceLinks; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param mixed $resourceLinks |
||
| 355 | * |
||
| 356 | * @return ResourceNode |
||
| 357 | */ |
||
| 358 | public function setResourceLinks($resourceLinks) |
||
| 359 | { |
||
| 360 | $this->resourceLinks = $resourceLinks; |
||
| 361 | |||
| 362 | return $this; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param Session $session |
||
| 367 | * |
||
| 368 | * @return ArrayCollection |
||
| 369 | */ |
||
| 370 | public function hasSession(Session $session = null) |
||
| 371 | { |
||
| 372 | $links = $this->getResourceLinks(); |
||
| 373 | $criteria = Criteria::create(); |
||
| 374 | |||
| 375 | $criteria->andWhere( |
||
| 376 | Criteria::expr()->eq('session', $session) |
||
| 377 | ); |
||
| 378 | |||
| 379 | $result = $links->matching($criteria); |
||
| 380 | |||
| 381 | return $result; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function hasResourceFile() |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return ResourceFile |
||
| 394 | */ |
||
| 395 | public function getResourceFile(): ?ResourceFile |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | public function hasEditableContent() |
||
| 404 | { |
||
| 405 | if ($this->hasResourceFile()) { |
||
| 406 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 407 | if (strpos($mimeType, 'text') !== false) { |
||
| 408 | return true; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | return false; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | public function isResourceFileAnImage() |
||
| 419 | { |
||
| 420 | if ($this->hasResourceFile()) { |
||
| 421 | $mimeType = $this->getResourceFile()->getMimeType(); |
||
| 422 | if (strpos($mimeType, 'image') !== false) { |
||
| 423 | return true; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | return false; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function isResourceFileAVideo() |
||
| 443 | } |
||
| 444 | |||
| 445 | public function setResourceFile(ResourceFile $resourceFile): ResourceNode |
||
| 446 | { |
||
| 447 | $this->resourceFile = $resourceFile; |
||
| 448 | |||
| 449 | return $this; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function getIcon() |
||
| 470 | } |
||
| 471 | } |
||
| 472 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..