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