Complex classes like Entry 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 Entry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Entry implements \JsonSerializable |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @ORM\Id |
||
| 18 | * @ORM\Column(type="integer"); |
||
| 19 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 20 | */ |
||
| 21 | protected $id; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @ORM\ManyToOne(targetEntity="Game", cascade={"persist","remove"}) |
||
| 25 | * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 26 | **/ |
||
| 27 | protected $game; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @ORM\ManyToOne(targetEntity="PlaygroundUser\Entity\User") |
||
| 31 | * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="CASCADE") |
||
| 32 | **/ |
||
| 33 | protected $user; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @ORM\Column(type="boolean", nullable=true) |
||
| 37 | */ |
||
| 38 | protected $active = 1; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Has this entry been paid |
||
| 42 | * @ORM\Column(type="boolean", nullable=true) |
||
| 43 | */ |
||
| 44 | protected $paid = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The amount paid for this entry |
||
| 48 | * @ORM\Column(name="paid_amount", type="integer", nullable=true) |
||
| 49 | */ |
||
| 50 | protected $paidAmount = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * An entry marked as bonus means that it's not taken into account |
||
| 54 | * in the limit calculation. It's offered, for example, when you share the game to a friend. |
||
| 55 | * |
||
| 56 | * @ORM\Column(type="boolean", nullable=true) |
||
| 57 | */ |
||
| 58 | protected $bonus = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ORM\Column(type="integer", nullable=true) |
||
| 62 | */ |
||
| 63 | protected $points; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 67 | */ |
||
| 68 | protected $ip; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 72 | */ |
||
| 73 | protected $geoloc; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @ORM\Column(name="anonymous_id", type="string", length=255, nullable=true) |
||
| 77 | */ |
||
| 78 | protected $anonymousId; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * This column hosts an id chosen by the admin in case of a game with "anonymous" participation : |
||
| 82 | * This is a value chosen from the playerData (generally "email"). |
||
| 83 | * |
||
| 84 | * @ORM\Column(name="anonymous_identifier", type="string", length=255, nullable=true) |
||
| 85 | */ |
||
| 86 | protected $anonymousIdentifier; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @ORM\Column(name="player_data", type="text", nullable=true) |
||
| 90 | */ |
||
| 91 | protected $playerData; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @ORM\Column(name="social_shares", type="text", nullable=true) |
||
| 95 | */ |
||
| 96 | protected $socialShares; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Is this entry a winning one ? |
||
| 100 | * @ORM\Column(type="boolean", nullable=true) |
||
| 101 | */ |
||
| 102 | protected $winner = 0; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Is there a termsOptin on this entry ? |
||
| 106 | * @ORM\Column(name="terms_optin", type="boolean", nullable=true) |
||
| 107 | */ |
||
| 108 | protected $termsOptin; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Is this entry eligible to draw ? |
||
| 112 | * @ORM\Column(type="boolean", nullable=true) |
||
| 113 | */ |
||
| 114 | protected $drawable = 0; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @ORM\Column(type="datetime") |
||
| 118 | */ |
||
| 119 | protected $created_at; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The step in the play (in a quiz for example : the nth question) |
||
| 123 | * @ORM\Column(type="integer", nullable=true) |
||
| 124 | */ |
||
| 125 | protected $step = 0; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @ORM\Column(type="datetime") |
||
| 129 | */ |
||
| 130 | protected $updated_at; |
||
| 131 | |||
| 132 | public function __construct() |
||
| 135 | |||
| 136 | /** @PrePersist */ |
||
| 137 | public function createChrono() |
||
| 142 | |||
| 143 | /** @PreUpdate */ |
||
| 144 | public function updateChrono() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * |
||
| 151 | * @return the $id |
||
| 152 | */ |
||
| 153 | public function getId() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * |
||
| 160 | * @param field_type $id |
||
| 161 | */ |
||
| 162 | public function setId($id) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return the $game |
||
| 171 | */ |
||
| 172 | public function getGame() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param field_type $game |
||
| 179 | */ |
||
| 180 | public function setGame($game) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return the $user |
||
| 189 | */ |
||
| 190 | public function getUser() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param field_type $user |
||
| 197 | */ |
||
| 198 | public function setUser($user) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return integer $points |
||
| 207 | */ |
||
| 208 | public function getPoints() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param integer $points |
||
| 215 | */ |
||
| 216 | public function setPoints($points) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return the $ip |
||
| 225 | */ |
||
| 226 | public function getIp() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param field_type $ip |
||
| 233 | */ |
||
| 234 | public function setIp($ip) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return the $geoloc |
||
| 243 | */ |
||
| 244 | public function getGeoloc() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param field_type $geoloc |
||
| 251 | */ |
||
| 252 | public function setGeoloc($geoloc) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return the $anonymousId |
||
| 261 | */ |
||
| 262 | public function getAnonymousId() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param field_type $anonymousId |
||
| 269 | */ |
||
| 270 | public function setAnonymousId($anonymousId) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return the $anonymousIdentifier |
||
| 279 | */ |
||
| 280 | public function getAnonymousIdentifier() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param field_type $anonymousIdentifier |
||
| 287 | */ |
||
| 288 | public function setAnonymousIdentifier($anonymousIdentifier) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return the $playerData |
||
| 295 | */ |
||
| 296 | public function getPlayerData() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param field_type $playerData |
||
| 303 | */ |
||
| 304 | public function setPlayerData($playerData) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return the $socialShares |
||
| 313 | */ |
||
| 314 | public function getSocialShares() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param field_type $socialShares |
||
| 321 | */ |
||
| 322 | public function setSocialShares($socialShares) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return boolean active |
||
| 329 | */ |
||
| 330 | public function getActive() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param boolean $active |
||
| 337 | */ |
||
| 338 | public function setActive($active) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return boolean paid |
||
| 347 | */ |
||
| 348 | public function getPaid() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param boolean $paid |
||
| 355 | */ |
||
| 356 | public function setPaid($paid) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return integer paidAmount |
||
| 365 | */ |
||
| 366 | public function getPaidAmount() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param integer $paidAmount |
||
| 373 | */ |
||
| 374 | public function setPaidAmount($paidAmount) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return integer $step |
||
| 383 | */ |
||
| 384 | public function getStep() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param number $step |
||
| 391 | */ |
||
| 392 | public function setStep($step) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return integer bonus |
||
| 399 | */ |
||
| 400 | public function getBonus() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param integer $bonus |
||
| 407 | */ |
||
| 408 | public function setBonus($bonus) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return integer status |
||
| 417 | */ |
||
| 418 | public function getWinner() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param integer $winner |
||
| 425 | */ |
||
| 426 | public function setWinner($winner) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return integer $drawable |
||
| 435 | */ |
||
| 436 | public function getDrawable() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param number $drawable |
||
| 443 | */ |
||
| 444 | public function setDrawable($drawable) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return the termsOptin |
||
| 453 | */ |
||
| 454 | public function getTermsOptin() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param field_type $termsOptin |
||
| 461 | */ |
||
| 462 | public function setTermsOptin($termsOptin) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return \DateTime $created_at |
||
| 471 | */ |
||
| 472 | public function getCreatedAt() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param \DateTime $created_at |
||
| 479 | */ |
||
| 480 | public function setCreatedAt($created_at) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return \DateTime $updated_at |
||
| 489 | */ |
||
| 490 | public function getUpdatedAt() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param \DateTime $updated_at |
||
| 497 | */ |
||
| 498 | public function setUpdatedAt($updated_at) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Convert the object to an array. |
||
| 507 | * |
||
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | public function getArrayCopy() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Convert the object to json. |
||
| 517 | * |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | public function jsonSerialize() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Populate from an array. |
||
| 527 | * |
||
| 528 | * @param array $data |
||
| 529 | */ |
||
| 530 | public function populate($data = array()) |
||
| 533 | } |
||
| 534 |
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.