Complex classes like ProductClass 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ProductClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ProductClass extends \Eccube\Entity\AbstractEntity |
||
| 28 | { |
||
| 29 | private $price01_inc_tax = null; |
||
| 30 | private $price02_inc_tax = null; |
||
| 31 | private $tax_rate = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * 商品規格名を含めた商品名を返す. |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public function formatedProductName() |
||
| 39 | { |
||
| 40 | $productName = $this->getProduct()->getName(); |
||
| 41 | if ($this->hasClassCategory1()) { |
||
| 42 | $productName .= ' - '.$this->getClassCategory1()->getName(); |
||
| 43 | } |
||
| 44 | if ($this->hasClassCategory2()) { |
||
| 45 | $productName .= ' - '.$this->getClassCategory2()->getName(); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $productName; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Is Enable |
||
| 53 | * |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | 80 | public function isEnable() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Set price01 IncTax |
||
| 63 | * |
||
| 64 | * @param string $price01_inc_tax |
||
| 65 | * |
||
| 66 | * @return ProductClass |
||
| 67 | */ |
||
| 68 | 460 | public function setPrice01IncTax($price01_inc_tax) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get price01 IncTax |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 7 | public function getPrice01IncTax() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set price02 IncTax |
||
| 87 | * |
||
| 88 | * |
||
| 89 | * @return ProductClass |
||
| 90 | */ |
||
| 91 | 460 | public function setPrice02IncTax($price02_inc_tax) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get price02 IncTax |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 101 | public function getPrice02IncTax() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Get StockFind |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | 48 | public function getStockFind() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Set tax_rate |
||
| 124 | * |
||
| 125 | * @param string $tax_rate |
||
| 126 | * |
||
| 127 | * @return ProductClass |
||
| 128 | */ |
||
| 129 | 41 | public function setTaxRate($tax_rate) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get tax_rate |
||
| 138 | * |
||
| 139 | * @return boolean |
||
| 140 | */ |
||
| 141 | 46 | public function getTaxRate() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Has ClassCategory1 |
||
| 148 | * |
||
| 149 | * @return boolean |
||
| 150 | */ |
||
| 151 | 196 | public function hasClassCategory1() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Has ClassCategory1 |
||
| 158 | * |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | 196 | public function hasClassCategory2() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @var int |
||
| 168 | * |
||
| 169 | * @ORM\Column(name="id", type="integer", options={"unsigned":true}) |
||
| 170 | * @ORM\Id |
||
| 171 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
| 172 | */ |
||
| 173 | private $id; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var string|null |
||
| 177 | * |
||
| 178 | * @ORM\Column(name="product_code", type="string", length=255, nullable=true) |
||
| 179 | */ |
||
| 180 | private $code; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var string|null |
||
| 184 | * |
||
| 185 | * @ORM\Column(name="stock", type="decimal", precision=10, scale=0, nullable=true) |
||
| 186 | */ |
||
| 187 | private $stock; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var boolean |
||
| 191 | * |
||
| 192 | * @ORM\Column(name="stock_unlimited", type="boolean", options={"default":false}) |
||
| 193 | */ |
||
| 194 | private $stock_unlimited = false; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @var string|null |
||
| 198 | * |
||
| 199 | * @ORM\Column(name="sale_limit", type="decimal", precision=10, scale=0, nullable=true, options={"unsigned":true}) |
||
| 200 | */ |
||
| 201 | private $sale_limit; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var string|null |
||
| 205 | * |
||
| 206 | * @ORM\Column(name="price01", type="decimal", precision=12, scale=2, nullable=true, options={"unsigned":true}) |
||
| 207 | */ |
||
| 208 | private $price01; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @var string |
||
| 212 | * |
||
| 213 | * @ORM\Column(name="price02", type="decimal", precision=12, scale=2, options={"unsigned":true}) |
||
| 214 | */ |
||
| 215 | private $price02; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @var string|null |
||
| 219 | * |
||
| 220 | * @ORM\Column(name="delivery_fee", type="decimal", precision=12, scale=2, nullable=true, options={"unsigned":true}) |
||
| 221 | */ |
||
| 222 | private $delivery_fee; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @var boolean |
||
| 226 | * |
||
| 227 | * @ORM\Column(name="visible", type="boolean", options={"default":true}) |
||
| 228 | */ |
||
| 229 | private $visible; |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @var \DateTime |
||
| 233 | * |
||
| 234 | * @ORM\Column(name="create_date", type="datetimetz") |
||
| 235 | */ |
||
| 236 | private $create_date; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @var \DateTime |
||
| 240 | * |
||
| 241 | * @ORM\Column(name="update_date", type="datetimetz") |
||
| 242 | */ |
||
| 243 | private $update_date; |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @var string|null |
||
| 247 | * |
||
| 248 | * @ORM\Column(name="currency_code", type="string", nullable=true) |
||
| 249 | */ |
||
| 250 | private $currency_code; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @var string |
||
| 254 | * |
||
| 255 | * @ORM\Column(name="point_rate", type="decimal", precision=10, scale=0, options={"unsigned":true}, nullable=true) |
||
| 256 | */ |
||
| 257 | private $point_rate; |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @var \Eccube\Entity\ProductStock |
||
| 261 | * |
||
| 262 | * @ORM\OneToOne(targetEntity="Eccube\Entity\ProductStock", mappedBy="ProductClass", cascade={"persist","remove"}) |
||
| 263 | */ |
||
| 264 | private $ProductStock; |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @var \Eccube\Entity\TaxRule |
||
| 268 | * |
||
| 269 | * @ORM\OneToOne(targetEntity="Eccube\Entity\TaxRule", mappedBy="ProductClass", cascade={"persist","remove"}) |
||
| 270 | */ |
||
| 271 | private $TaxRule; |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @var \Eccube\Entity\Product |
||
| 275 | * |
||
| 276 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\Product", inversedBy="ProductClasses") |
||
| 277 | * @ORM\JoinColumns({ |
||
| 278 | * @ORM\JoinColumn(name="product_id", referencedColumnName="id") |
||
| 279 | * }) |
||
| 280 | */ |
||
| 281 | private $Product; |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @var \Eccube\Entity\Master\SaleType |
||
| 285 | * |
||
| 286 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\Master\SaleType") |
||
| 287 | * @ORM\JoinColumns({ |
||
| 288 | * @ORM\JoinColumn(name="sale_type_id", referencedColumnName="id") |
||
| 289 | * }) |
||
| 290 | */ |
||
| 291 | private $SaleType; |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @var \Eccube\Entity\ClassCategory |
||
| 295 | * |
||
| 296 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\ClassCategory") |
||
| 297 | * @ORM\JoinColumns({ |
||
| 298 | * @ORM\JoinColumn(name="class_category_id1", referencedColumnName="id", nullable=true) |
||
| 299 | * }) |
||
| 300 | */ |
||
| 301 | private $ClassCategory1; |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @var \Eccube\Entity\ClassCategory |
||
| 305 | * |
||
| 306 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\ClassCategory") |
||
| 307 | * @ORM\JoinColumns({ |
||
| 308 | * @ORM\JoinColumn(name="class_category_id2", referencedColumnName="id", nullable=true) |
||
| 309 | * }) |
||
| 310 | */ |
||
| 311 | private $ClassCategory2; |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @var \Eccube\Entity\DeliveryDuration |
||
| 315 | * |
||
| 316 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\DeliveryDuration") |
||
| 317 | * @ORM\JoinColumns({ |
||
| 318 | * @ORM\JoinColumn(name="delivery_duration_id", referencedColumnName="id") |
||
| 319 | * }) |
||
| 320 | */ |
||
| 321 | private $DeliveryDuration; |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @var \Eccube\Entity\Member |
||
| 325 | * |
||
| 326 | * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member") |
||
| 327 | * @ORM\JoinColumns({ |
||
| 328 | * @ORM\JoinColumn(name="creator_id", referencedColumnName="id") |
||
| 329 | * }) |
||
| 330 | */ |
||
| 331 | private $Creator; |
||
| 332 | |||
| 333 | 3 | public function __clone() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Get id. |
||
| 340 | * |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | 431 | public function getId() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Set code. |
||
| 350 | * |
||
| 351 | * @param string|null $code |
||
| 352 | * |
||
| 353 | * @return ProductClass |
||
| 354 | */ |
||
| 355 | 403 | public function setCode($code = null) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get code. |
||
| 364 | * |
||
| 365 | * @return string|null |
||
| 366 | */ |
||
| 367 | 297 | public function getCode() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Set stock. |
||
| 374 | * |
||
| 375 | * @param string|null $stock |
||
| 376 | * |
||
| 377 | * @return ProductClass |
||
| 378 | */ |
||
| 379 | 424 | public function setStock($stock = null) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Get stock. |
||
| 388 | * |
||
| 389 | * @return string|null |
||
| 390 | */ |
||
| 391 | 275 | public function getStock() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Set stockUnlimited. |
||
| 398 | * |
||
| 399 | * @param boolean $stockUnlimited |
||
| 400 | * |
||
| 401 | * @return ProductClass |
||
| 402 | */ |
||
| 403 | 406 | public function setStockUnlimited($stockUnlimited) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get stockUnlimited. |
||
| 412 | * |
||
| 413 | * @return boolean |
||
| 414 | */ |
||
| 415 | 307 | public function isStockUnlimited() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Set saleLimit. |
||
| 422 | * |
||
| 423 | * @param string|null $saleLimit |
||
| 424 | * |
||
| 425 | * @return ProductClass |
||
| 426 | */ |
||
| 427 | 33 | public function setSaleLimit($saleLimit = null) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Get saleLimit. |
||
| 436 | * |
||
| 437 | * @return string|null |
||
| 438 | */ |
||
| 439 | 126 | public function getSaleLimit() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Set price01. |
||
| 446 | * |
||
| 447 | * @param string|null $price01 |
||
| 448 | * |
||
| 449 | * @return ProductClass |
||
| 450 | */ |
||
| 451 | 44 | public function setPrice01($price01 = null) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Get price01. |
||
| 460 | * |
||
| 461 | * @return string|null |
||
| 462 | */ |
||
| 463 | 478 | public function getPrice01() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Set price02. |
||
| 470 | * |
||
| 471 | * @param string $price02 |
||
| 472 | * |
||
| 473 | * @return ProductClass |
||
| 474 | */ |
||
| 475 | 426 | public function setPrice02($price02) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get price02. |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | 478 | public function getPrice02() |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Set deliveryFee. |
||
| 494 | * |
||
| 495 | * @param string|null $deliveryFee |
||
| 496 | * |
||
| 497 | * @return ProductClass |
||
| 498 | */ |
||
| 499 | 25 | public function setDeliveryFee($deliveryFee = null) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Get deliveryFee. |
||
| 508 | * |
||
| 509 | * @return string|null |
||
| 510 | */ |
||
| 511 | 45 | public function getDeliveryFee() |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @return boolean |
||
| 518 | */ |
||
| 519 | 228 | public function isVisible() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * @param boolean $visible |
||
| 526 | * |
||
| 527 | * @return ProductClass |
||
| 528 | */ |
||
| 529 | 411 | public function setVisible($visible) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Set createDate. |
||
| 538 | * |
||
| 539 | * @param \DateTime $createDate |
||
| 540 | * |
||
| 541 | * @return ProductClass |
||
| 542 | */ |
||
| 543 | 405 | public function setCreateDate($createDate) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Get createDate. |
||
| 552 | * |
||
| 553 | * @return \DateTime |
||
| 554 | */ |
||
| 555 | public function getCreateDate() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Set updateDate. |
||
| 562 | * |
||
| 563 | * @param \DateTime $updateDate |
||
| 564 | * |
||
| 565 | * @return ProductClass |
||
| 566 | */ |
||
| 567 | 409 | public function setUpdateDate($updateDate) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Get updateDate. |
||
| 576 | * |
||
| 577 | * @return \DateTime |
||
| 578 | */ |
||
| 579 | public function getUpdateDate() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Get currencyCode. |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | public function getCurrencyCode() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Set currencyCode. |
||
| 596 | * |
||
| 597 | * @param string|null $currencyCode |
||
| 598 | * |
||
| 599 | * @return $this |
||
| 600 | */ |
||
| 601 | 405 | public function setCurrencyCode($currencyCode = null) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Set productStock. |
||
| 610 | * |
||
| 611 | * @param \Eccube\Entity\ProductStock|null $productStock |
||
| 612 | * |
||
| 613 | * @return ProductClass |
||
| 614 | */ |
||
| 615 | 406 | public function setProductStock(\Eccube\Entity\ProductStock $productStock = null) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Get productStock. |
||
| 624 | * |
||
| 625 | * @return \Eccube\Entity\ProductStock|null |
||
| 626 | */ |
||
| 627 | 35 | public function getProductStock() |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Set taxRule. |
||
| 634 | * |
||
| 635 | * @param \Eccube\Entity\TaxRule|null $taxRule |
||
| 636 | * |
||
| 637 | * @return ProductClass |
||
| 638 | */ |
||
| 639 | 10 | public function setTaxRule(\Eccube\Entity\TaxRule $taxRule = null) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get taxRule. |
||
| 648 | * |
||
| 649 | * @return \Eccube\Entity\TaxRule|null |
||
| 650 | */ |
||
| 651 | 22 | public function getTaxRule() |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Set product. |
||
| 658 | * |
||
| 659 | * @param \Eccube\Entity\Product|null $product |
||
| 660 | * |
||
| 661 | * @return ProductClass |
||
| 662 | */ |
||
| 663 | 410 | public function setProduct(\Eccube\Entity\Product $product = null) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Get product. |
||
| 672 | * |
||
| 673 | * @return \Eccube\Entity\Product|null |
||
| 674 | */ |
||
| 675 | 460 | public function getProduct() |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Set saleType. |
||
| 682 | * |
||
| 683 | * @param \Eccube\Entity\Master\SaleType|null $saleType |
||
| 684 | * |
||
| 685 | * @return ProductClass |
||
| 686 | */ |
||
| 687 | 405 | public function setSaleType(\Eccube\Entity\Master\SaleType $saleType = null) |
|
| 693 | |||
| 694 | /** |
||
| 695 | * Get saleType. |
||
| 696 | * |
||
| 697 | * @return \Eccube\Entity\Master\SaleType|null |
||
| 698 | */ |
||
| 699 | 148 | public function getSaleType() |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Set classCategory1. |
||
| 706 | * |
||
| 707 | * @param \Eccube\Entity\ClassCategory|null $classCategory1 |
||
| 708 | * |
||
| 709 | * @return ProductClass |
||
| 710 | */ |
||
| 711 | 408 | public function setClassCategory1(\Eccube\Entity\ClassCategory $classCategory1 = null) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Get classCategory1. |
||
| 720 | * |
||
| 721 | * @return \Eccube\Entity\ClassCategory|null |
||
| 722 | */ |
||
| 723 | 321 | public function getClassCategory1() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Set classCategory2. |
||
| 730 | * |
||
| 731 | * @param \Eccube\Entity\ClassCategory|null $classCategory2 |
||
| 732 | * |
||
| 733 | * @return ProductClass |
||
| 734 | */ |
||
| 735 | 276 | public function setClassCategory2(\Eccube\Entity\ClassCategory $classCategory2 = null) |
|
| 741 | |||
| 742 | /** |
||
| 743 | * Get classCategory2. |
||
| 744 | * |
||
| 745 | * @return \Eccube\Entity\ClassCategory|null |
||
| 746 | */ |
||
| 747 | 261 | public function getClassCategory2() |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Set deliveryDuration. |
||
| 754 | * |
||
| 755 | * @param \Eccube\Entity\DeliveryDuration|null $deliveryDuration |
||
| 756 | * |
||
| 757 | * @return ProductClass |
||
| 758 | */ |
||
| 759 | 394 | public function setDeliveryDuration(\Eccube\Entity\DeliveryDuration $deliveryDuration = null) |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Get deliveryDuration. |
||
| 768 | * |
||
| 769 | * @return \Eccube\Entity\DeliveryDuration|null |
||
| 770 | */ |
||
| 771 | 96 | public function getDeliveryDuration() |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Set creator. |
||
| 778 | * |
||
| 779 | * @param \Eccube\Entity\Member|null $creator |
||
| 780 | * |
||
| 781 | * @return ProductClass |
||
| 782 | */ |
||
| 783 | 409 | public function setCreator(\Eccube\Entity\Member $creator = null) |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Get creator. |
||
| 792 | * |
||
| 793 | * @return \Eccube\Entity\Member|null |
||
| 794 | */ |
||
| 795 | public function getCreator() |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Set pointRate |
||
| 802 | * |
||
| 803 | * @param string $pointRate |
||
| 804 | * |
||
| 805 | * @return ProductClass |
||
| 806 | */ |
||
| 807 | public function setPointRate($pointRate) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Get pointRate |
||
| 816 | * |
||
| 817 | * @return string |
||
| 818 | */ |
||
| 819 | public function getPointRate() |
||
| 823 | } |
||
| 824 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.