| Total Complexity | 46 |
| Total Lines | 532 |
| Duplicated Lines | 2.26 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Category 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 Category, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Category extends AbstractEntity implements CategoryInterface |
||
| 20 | { |
||
| 21 | use CategoryTrait; |
||
| 22 | use Timestampable; |
||
| 23 | use SoftDeletable; |
||
| 24 | use Translatable; |
||
| 25 | |||
| 26 | protected $hydrateConversions = [ |
||
| 27 | 'internalId' => 'externalId', |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | * |
||
| 33 | * @ORM\Id |
||
| 34 | * @ORM\GeneratedValue |
||
| 35 | * @ORM\Column(type="integer") |
||
| 36 | **/ |
||
| 37 | protected $id; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * This is the internal id in the API |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | * |
||
| 44 | * @ORM\Column(type="integer", unique=true) |
||
| 45 | */ |
||
| 46 | protected $externalId; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | * |
||
| 51 | * @ORM\Column(type="string", unique=true, length=191) |
||
| 52 | */ |
||
| 53 | protected $number; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string|null |
||
| 57 | * |
||
| 58 | * @ORM\Column(nullable=true, type="string", length=191) |
||
| 59 | */ |
||
| 60 | protected $b2bGroupId; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \DateTimeImmutable|null |
||
| 64 | * |
||
| 65 | * @ORM\Column(nullable=true, type="datetime_immutable") |
||
| 66 | */ |
||
| 67 | protected $createdDate; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int|null |
||
| 71 | * |
||
| 72 | * @ORM\Column(nullable=true, type="integer") |
||
| 73 | */ |
||
| 74 | protected $customInfoLayout; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var int|null |
||
| 78 | * |
||
| 79 | * @ORM\Column(nullable=true, type="integer") |
||
| 80 | */ |
||
| 81 | protected $customListLayout; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var int|null |
||
| 85 | * |
||
| 86 | * @ORM\Column(nullable=true, type="integer") |
||
| 87 | */ |
||
| 88 | protected $defaultParentId; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var \DateTimeImmutable|null |
||
| 92 | * |
||
| 93 | * @ORM\Column(nullable=true, type="datetime_immutable") |
||
| 94 | */ |
||
| 95 | protected $editedDate; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var int|null |
||
| 99 | * |
||
| 100 | * @ORM\Column(nullable=true, type="integer") |
||
| 101 | */ |
||
| 102 | protected $infoLayout; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var int|null |
||
| 106 | * |
||
| 107 | * @ORM\Column(nullable=true, type="integer") |
||
| 108 | */ |
||
| 109 | protected $listLayout; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool|null |
||
| 113 | * |
||
| 114 | * @ORM\Column(nullable=true, type="boolean") |
||
| 115 | */ |
||
| 116 | protected $modified; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The parent 'id' really refers to the parent number |
||
| 120 | * |
||
| 121 | * @var array|null |
||
| 122 | * |
||
| 123 | * @ORM\Column(nullable=true, type="json") |
||
| 124 | */ |
||
| 125 | protected $parentIdList; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array|null |
||
| 129 | * |
||
| 130 | * @ORM\Column(nullable=true, type="json") |
||
| 131 | */ |
||
| 132 | protected $segmentIdList; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var Category[]|ArrayCollection |
||
| 136 | * |
||
| 137 | * @ORM\ManyToMany(mappedBy="parentCategories", targetEntity="Category") |
||
| 138 | */ |
||
| 139 | protected $childrenCategories; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var Category[]|ArrayCollection |
||
| 143 | * |
||
| 144 | * @ORM\JoinTable(name="ldf_category_parents") |
||
| 145 | * @ORM\ManyToMany(inversedBy="childrenCategories", targetEntity="Category") |
||
| 146 | */ |
||
| 147 | protected $parentCategories; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var Product[]|ArrayCollection |
||
| 151 | * |
||
| 152 | * @ORM\ManyToMany(mappedBy="categories", targetEntity="Product") |
||
| 153 | */ |
||
| 154 | protected $products; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var Segment[]|ArrayCollection |
||
| 158 | * |
||
| 159 | * @ORM\JoinTable(name="ldf_category_segments") |
||
| 160 | * @ORM\ManyToMany(cascade={"persist"}, inversedBy="categories", targetEntity="Segment") |
||
| 161 | */ |
||
| 162 | protected $segments; |
||
| 163 | |||
| 164 | public function __construct() |
||
| 165 | { |
||
| 166 | $this->childrenCategories = new ArrayCollection(); |
||
| 167 | $this->parentCategories = new ArrayCollection(); |
||
| 168 | $this->products = new ArrayCollection(); |
||
| 169 | $this->segments = new ArrayCollection(); |
||
| 170 | } |
||
| 171 | |||
| 172 | View Code Duplication | public function hydrate(array $data, bool $useConversions = false, $scalarsOnly = true) |
|
| 173 | { |
||
| 174 | if ($data['createdDate']) { |
||
| 175 | $data['createdDate'] = $this->getDateTimeFromJson($data['createdDate']); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($data['editedDate']) { |
||
| 179 | $data['editedDate'] = $this->getDateTimeFromJson($data['editedDate']); |
||
| 180 | } |
||
| 181 | |||
| 182 | parent::hydrate($data, $useConversions, $scalarsOnly); |
||
| 183 | } |
||
| 184 | |||
| 185 | /* |
||
| 186 | * Collection methods |
||
| 187 | */ |
||
| 188 | public function addParentCategory(CategoryInterface $category) : CategoryInterface |
||
| 189 | { |
||
| 190 | if (!$this->hasParentCategory($category)) { |
||
| 191 | $this->parentCategories->add($category); |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param CategoryInterface|int $category |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function hasParentCategory($category) : bool |
||
| 209 | }); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function removeParentCategory(CategoryInterface $category) : CategoryInterface |
||
| 213 | { |
||
| 214 | $this->parentCategories->removeElement($category); |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function clearParentCategories() : CategoryInterface |
||
| 220 | { |
||
| 221 | $this->parentCategories->clear(); |
||
| 222 | |||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | |||
| 226 | /* |
||
| 227 | * Getters / Setters |
||
| 228 | */ |
||
| 229 | /** |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | public function getId(): int |
||
| 233 | { |
||
| 234 | return (int)$this->id; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param int $id |
||
| 239 | * @return CategoryInterface |
||
| 240 | */ |
||
| 241 | public function setId(int $id) |
||
| 242 | { |
||
| 243 | $this->id = $id; |
||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | public function getExternalId(): int |
||
| 251 | { |
||
| 252 | return (int)$this->externalId; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param int $externalId |
||
| 257 | * @return CategoryInterface |
||
| 258 | */ |
||
| 259 | public function setExternalId(int $externalId) |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getNumber(): string |
||
| 269 | { |
||
| 270 | return (string)$this->number; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $number |
||
| 275 | * @return CategoryInterface |
||
| 276 | */ |
||
| 277 | public function setNumber(string $number) |
||
| 278 | { |
||
| 279 | $this->number = $number; |
||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return null|string |
||
| 285 | */ |
||
| 286 | public function getB2bGroupId() |
||
| 287 | { |
||
| 288 | return $this->b2bGroupId; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param null|string $b2bGroupId |
||
| 293 | * @return CategoryInterface |
||
| 294 | */ |
||
| 295 | public function setB2bGroupId($b2bGroupId) |
||
| 296 | { |
||
| 297 | $this->b2bGroupId = $b2bGroupId; |
||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return \DateTimeImmutable|null |
||
| 303 | */ |
||
| 304 | public function getCreatedDate() |
||
| 305 | { |
||
| 306 | return $this->createdDate; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param \DateTimeImmutable|null $createdDate |
||
| 311 | * @return CategoryInterface |
||
| 312 | */ |
||
| 313 | public function setCreatedDate($createdDate) |
||
| 314 | { |
||
| 315 | $this->createdDate = $createdDate; |
||
| 316 | return $this; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return int|null |
||
| 321 | */ |
||
| 322 | public function getCustomInfoLayout() |
||
| 323 | { |
||
| 324 | return $this->customInfoLayout; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param int|null $customInfoLayout |
||
| 329 | * @return CategoryInterface |
||
| 330 | */ |
||
| 331 | public function setCustomInfoLayout($customInfoLayout) |
||
| 332 | { |
||
| 333 | $this->customInfoLayout = $customInfoLayout; |
||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return int|null |
||
| 339 | */ |
||
| 340 | public function getCustomListLayout() |
||
| 341 | { |
||
| 342 | return $this->customListLayout; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param int|null $customListLayout |
||
| 347 | * @return CategoryInterface |
||
| 348 | */ |
||
| 349 | public function setCustomListLayout($customListLayout) |
||
| 350 | { |
||
| 351 | $this->customListLayout = $customListLayout; |
||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return int|null |
||
| 357 | */ |
||
| 358 | public function getDefaultParentId() |
||
| 359 | { |
||
| 360 | return $this->defaultParentId; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param int|null $defaultParentId |
||
| 365 | * @return CategoryInterface |
||
| 366 | */ |
||
| 367 | public function setDefaultParentId($defaultParentId) |
||
| 368 | { |
||
| 369 | $this->defaultParentId = $defaultParentId; |
||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return \DateTimeImmutable|null |
||
| 375 | */ |
||
| 376 | public function getEditedDate() |
||
| 377 | { |
||
| 378 | return $this->editedDate; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param \DateTimeImmutable|null $editedDate |
||
| 383 | * @return CategoryInterface |
||
| 384 | */ |
||
| 385 | public function setEditedDate($editedDate) |
||
| 386 | { |
||
| 387 | $this->editedDate = $editedDate; |
||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return int|null |
||
| 393 | */ |
||
| 394 | public function getInfoLayout() |
||
| 395 | { |
||
| 396 | return $this->infoLayout; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param int|null $infoLayout |
||
| 401 | * @return CategoryInterface |
||
| 402 | */ |
||
| 403 | public function setInfoLayout($infoLayout) |
||
| 404 | { |
||
| 405 | $this->infoLayout = $infoLayout; |
||
| 406 | return $this; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return int|null |
||
| 411 | */ |
||
| 412 | public function getListLayout() |
||
| 413 | { |
||
| 414 | return $this->listLayout; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param int|null $listLayout |
||
| 419 | * @return CategoryInterface |
||
| 420 | */ |
||
| 421 | public function setListLayout($listLayout) |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return bool|null |
||
| 429 | */ |
||
| 430 | public function getModified() |
||
| 431 | { |
||
| 432 | return $this->modified; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param bool|null $modified |
||
| 437 | * @return CategoryInterface |
||
| 438 | */ |
||
| 439 | public function setModified($modified) |
||
| 440 | { |
||
| 441 | $this->modified = $modified; |
||
| 442 | return $this; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return array|null |
||
| 447 | */ |
||
| 448 | public function getParentIdList() |
||
| 449 | { |
||
| 450 | return $this->parentIdList; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param array|null $parentIdList |
||
| 455 | * @return CategoryInterface |
||
| 456 | */ |
||
| 457 | public function setParentIdList($parentIdList) |
||
| 458 | { |
||
| 459 | $this->parentIdList = $parentIdList; |
||
| 460 | return $this; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return array|null |
||
| 465 | */ |
||
| 466 | public function getSegmentIdList() |
||
| 467 | { |
||
| 468 | return $this->segmentIdList; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param array|null $segmentIdList |
||
| 473 | * @return CategoryInterface |
||
| 474 | */ |
||
| 475 | public function setSegmentIdList($segmentIdList) |
||
| 476 | { |
||
| 477 | $this->segmentIdList = $segmentIdList; |
||
| 478 | return $this; |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return ArrayCollection|Category[] |
||
| 483 | */ |
||
| 484 | public function getChildrenCategories() : ArrayCollection |
||
| 485 | { |
||
| 486 | return $this->childrenCategories; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param ArrayCollection|Category[] $childrenCategories |
||
| 491 | * @return CategoryInterface |
||
| 492 | */ |
||
| 493 | public function setChildrenCategories($childrenCategories) |
||
| 494 | { |
||
| 495 | $this->childrenCategories = $childrenCategories; |
||
| 496 | return $this; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return ArrayCollection|Category[] |
||
| 501 | */ |
||
| 502 | public function getParentCategories() : ArrayCollection |
||
| 503 | { |
||
| 504 | return $this->parentCategories; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param ArrayCollection|Category[] $parentCategories |
||
| 509 | * @return CategoryInterface |
||
| 510 | */ |
||
| 511 | public function setParentCategories($parentCategories) |
||
| 512 | { |
||
| 513 | $this->parentCategories = $parentCategories; |
||
| 514 | return $this; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return Product[]|ArrayCollection |
||
| 519 | */ |
||
| 520 | public function getProducts() : ArrayCollection |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param Product[] $products |
||
| 527 | * @return CategoryInterface |
||
| 528 | */ |
||
| 529 | public function setProducts($products) |
||
| 530 | { |
||
| 531 | $this->products = $products; |
||
| 532 | return $this; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @return ArrayCollection|Segment[] |
||
| 537 | */ |
||
| 538 | public function getSegments() : ArrayCollection |
||
| 539 | { |
||
| 540 | return $this->segments; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param ArrayCollection|Segment[] $segments |
||
| 545 | * @return CategoryInterface |
||
| 546 | */ |
||
| 547 | public function setSegments($segments) |
||
| 551 | } |
||
| 552 | } |
||
| 553 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths