Complex classes like Game 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 Game, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Game |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var integer $id |
||
| 34 | * |
||
| 35 | * @ORM\Column(name="id", type="integer") |
||
| 36 | * @ORM\Id |
||
| 37 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 38 | */ |
||
| 39 | private $id; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string $name |
||
| 43 | * |
||
| 44 | * @ORM\Column(name="name", type="string", length=32) |
||
| 45 | * @Assert\NotBlank(message="game.assert.name.needed") |
||
| 46 | */ |
||
| 47 | private $name; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean $steamCmd |
||
| 51 | * |
||
| 52 | * @ORM\Column(name="steamCmd", type="boolean") |
||
| 53 | */ |
||
| 54 | private $steamCmd = false; // default value |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var boolean $source |
||
| 58 | * |
||
| 59 | * @ORM\Column(name="source", type="boolean") |
||
| 60 | */ |
||
| 61 | private $source = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string $launchName |
||
| 65 | * |
||
| 66 | * @ORM\Column(name="launchName", type="string", length=24) |
||
| 67 | * @Assert\NotBlank(message="game.assert.launchName") |
||
| 68 | */ |
||
| 69 | private $launchName; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string $bin |
||
| 73 | * |
||
| 74 | * @ORM\Column(name="bin", type="string", length=24) |
||
| 75 | * @Assert\NotBlank(message="game.assert.bin") |
||
| 76 | */ |
||
| 77 | private $bin; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var integer $appId |
||
| 81 | * |
||
| 82 | * @ORM\Column(name="appId", type="integer", nullable=true) |
||
| 83 | */ |
||
| 84 | protected $appId; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string $appMod |
||
| 88 | * |
||
| 89 | * @ORM\Column(name="appMod", type="string", length=20, nullable=true) |
||
| 90 | */ |
||
| 91 | protected $appMod; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string $map |
||
| 95 | * |
||
| 96 | * @ORM\Column(name="map", type="string", length=40, nullable=true) |
||
| 97 | */ |
||
| 98 | private $map; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var boolean $available |
||
| 102 | * |
||
| 103 | * @ORM\Column(name="available", type="boolean") |
||
| 104 | * @Assert\NotNull(message="game.assert.available") |
||
| 105 | */ |
||
| 106 | private $available = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string $binDir |
||
| 110 | * |
||
| 111 | * @ORM\Column(name="binDir", type="string", length=20, nullable=true) |
||
| 112 | */ |
||
| 113 | private $binDir; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var string $cfgPath |
||
| 117 | * |
||
| 118 | * @ORM\Column(name="cfgPath", type="string", length=255, nullable=true) |
||
| 119 | */ |
||
| 120 | protected $cfgPath; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string $cfgPath |
||
| 124 | * |
||
| 125 | * @ORM\Column(name="cfgPath", type="string", length=255, nullable=true) |
||
| 126 | */ |
||
| 127 | protected $cfgPath; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @ORM\Column(name="sourceImagesMaps", type="string", length=255, nullable=true) |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | private $sourceImagesMaps; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var \Doctrine\Common\Collections\ArrayCollection $gameServers |
||
| 137 | * |
||
| 138 | * @ORM\OneToMany(targetEntity="DP\GameServer\GameServerBundle\Entity\GameServer", mappedBy="game", cascade={"remove"}) |
||
| 139 | */ |
||
| 140 | private $gameServers; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var \Doctrine\Common\Collections\ArrayCollection $plugins |
||
| 144 | * |
||
| 145 | * @ORM\ManyToMany(targetEntity="DP\Core\GameBundle\Entity\Plugin", inversedBy="games") |
||
| 146 | * @ORM\JoinTable(name="game_plugin", |
||
| 147 | * joinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")}, |
||
| 148 | * inverseJoinColumns={@ORM\JoinColumn(name="plugin_id", referencedColumnName="id")} |
||
| 149 | * ) |
||
| 150 | */ |
||
| 151 | private $plugins; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @ORM\Column(name="type", type="string", length=32) |
||
| 155 | * @Dictionary(name="game_type", message="game.assert.type") |
||
| 156 | */ |
||
| 157 | private $type; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @ORM\Column(name="configTemplate", type="text", nullable=true) |
||
| 161 | */ |
||
| 162 | private $configTemplate; |
||
| 163 | |||
| 164 | |||
| 165 | public function __construct() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get id |
||
| 172 | * |
||
| 173 | * @return integer |
||
| 174 | */ |
||
| 175 | public function getId() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Set name |
||
| 182 | * |
||
| 183 | * @param string $name |
||
| 184 | */ |
||
| 185 | public function setName($name) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get name |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function getName() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set launchName |
||
| 202 | * |
||
| 203 | * @param string $launchName |
||
| 204 | */ |
||
| 205 | public function setLaunchName($launchName) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get launchName |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getLaunchName() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set bin |
||
| 222 | * |
||
| 223 | * @param string $bin |
||
| 224 | */ |
||
| 225 | public function setBin($bin) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get bin |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getBin() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set map |
||
| 242 | * |
||
| 243 | * @param string $map |
||
| 244 | */ |
||
| 245 | public function setMap($map) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get map |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getMap() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set available |
||
| 262 | * |
||
| 263 | * @param boolean $available |
||
| 264 | */ |
||
| 265 | public function setAvailable($available) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get available |
||
| 272 | * |
||
| 273 | * @return boolean |
||
| 274 | */ |
||
| 275 | public function getAvailable() |
||
| 279 | |||
| 280 | public function __toString() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Set binary directory |
||
| 287 | * |
||
| 288 | * @param string $binDir |
||
| 289 | */ |
||
| 290 | public function setBinDir($binDir) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get binary directory |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getBinDir() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Set cfg path |
||
| 311 | * |
||
| 312 | * @param string $cfgPath |
||
| 313 | */ |
||
| 314 | public function setCfgPath($cfgPath) |
||
| 318 | |||
| 319 | /** |
||
| 320 | |||
| 321 | * Get cfg path |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function getCfgPath() |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * Set source of images maps |
||
| 333 | * |
||
| 334 | * @param string $sourceImagesMaps |
||
| 335 | */ |
||
| 336 | public function setSourceImagesMaps($sourceImagesMaps) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get source of images maps |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getSourceImagesMaps() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Add plugins |
||
| 353 | * |
||
| 354 | * @param $plugin \Doctrine\Common\Collections\ArrayCollection |
||
| 355 | */ |
||
| 356 | public function addPlugin(\DP\Core\GameBundle\Entity\Plugin $plugin) |
||
| 364 | |||
| 365 | public function removePlugin(Plugin $plugin) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set plugin list |
||
| 372 | * |
||
| 373 | * @param array $plugins |
||
| 374 | */ |
||
| 375 | public function setPlugins(array $plugins = array()) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get plugins |
||
| 382 | * |
||
| 383 | * @return \Doctrine\Common\Collections\ArrayCollection |
||
| 384 | */ |
||
| 385 | public function getPlugins() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set game type (steam or minecraft) |
||
| 392 | * |
||
| 393 | * @param string $type |
||
| 394 | */ |
||
| 395 | public function setType($type) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string Game type |
||
| 402 | */ |
||
| 403 | public function getType() |
||
| 407 | |||
| 408 | public function isBukkit() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Set the server config file template |
||
| 415 | * @param string|null $configTemplate |
||
| 416 | */ |
||
| 417 | public function setConfigTemplate($configTemplate) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the server config file template |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getConfigTemplate() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Set appId |
||
| 434 | * |
||
| 435 | * @param integer $appId |
||
| 436 | * @return Game |
||
| 437 | */ |
||
| 438 | public function setAppId($appId) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get appId |
||
| 447 | * |
||
| 448 | * @return integer |
||
| 449 | */ |
||
| 450 | public function getAppId() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Set appMod |
||
| 457 | * |
||
| 458 | * @param string $appMod |
||
| 459 | * @return Game |
||
| 460 | */ |
||
| 461 | public function setAppMod($appMod) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get appMod |
||
| 470 | * |
||
| 471 | * @return integer |
||
| 472 | */ |
||
| 473 | public function getAppMod() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Set source |
||
| 480 | * |
||
| 481 | * @param boolean $source |
||
| 482 | */ |
||
| 483 | public function setSource($source) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get source |
||
| 490 | * |
||
| 491 | * @return boolean |
||
| 492 | */ |
||
| 493 | public function isSource() |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Get source |
||
| 501 | * |
||
| 502 | * @return boolean |
||
| 503 | */ |
||
| 504 | public function getSource() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Add gameServers |
||
| 511 | * |
||
| 512 | * @param \DP\GameServer\GameServerBundle\Entity\GameServer $gameServers |
||
| 513 | * @return Game |
||
| 514 | */ |
||
| 515 | public function addGameServer(\DP\GameServer\GameServerBundle\Entity\GameServer $gameServers) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Remove gameServers |
||
| 524 | * |
||
| 525 | * @param \DP\GameServer\GameServerBundle\Entity\GameServer $gameServers |
||
| 526 | */ |
||
| 527 | public function removeGameServer(\DP\GameServer\GameServerBundle\Entity\GameServer $gameServers) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get gameServers |
||
| 534 | * |
||
| 535 | * @return \Doctrine\Common\Collections\Collection |
||
| 536 | */ |
||
| 537 | public function getGameServers() |
||
| 541 | |||
| 542 | public function validateAppId(ExecutionContextInterface $context) |
||
| 556 | } |
||
| 557 |