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:
| 1 | <?php |
||
| 19 | abstract class AbstractServer implements ServerInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var integer $installationStatus |
||
| 23 | * |
||
| 24 | * @ORM\Column(name="installation_status", type="integer", nullable=true) |
||
| 25 | */ |
||
| 26 | protected $installationStatus; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var boolean $alreadyInstalled Used by the creation process |
||
| 30 | */ |
||
| 31 | protected $alreadyInstalled; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array $core |
||
| 35 | * |
||
| 36 | * @ORM\Column(name="core", type="simple_array", nullable=true) |
||
| 37 | */ |
||
| 38 | protected $core = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string $dir |
||
| 42 | * |
||
| 43 | * @ORM\Column(name="dir", type="string", length=64) |
||
| 44 | */ |
||
| 45 | protected $dir; |
||
| 46 | |||
| 47 | |||
| 48 | /** {@inheritdoc} */ |
||
| 49 | abstract public function getName(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set whether is already already installed |
||
| 53 | * |
||
| 54 | * @param boolean $alreadyInstalled |
||
| 55 | * |
||
| 56 | * @return AbstractServer |
||
| 57 | */ |
||
| 58 | public function setAlreadyInstalled($alreadyInstalled) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Is already installed ? (from form) |
||
| 71 | * |
||
| 72 | * @return boolean |
||
| 73 | */ |
||
| 74 | public function isAlreadyInstalled() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function setInstallationStatus($installationStatus) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function getInstallationStatus() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function isInstallationEnded() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | public function getMachine() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | public function deleteServer() |
||
| 120 | |||
| 121 | public function deleteInstallDir() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Try to retrieve the most recent percentage in $installLog |
||
| 135 | * |
||
| 136 | * @param string $installLog Will seperate log by lines |
||
| 137 | * @return null|string |
||
| 138 | */ |
||
| 139 | protected function getPercentFromInstallLog($installLog) |
||
| 158 | |||
| 159 | /** {@inheritdoc} */ |
||
| 160 | public function getFullName() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var array $core |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function setCore(array $core = array()) |
||
| 175 | |||
| 176 | /** {@inheritdoc} */ |
||
| 177 | public function getCore() |
||
| 181 | |||
| 182 | /** {@inheritdoc} */ |
||
| 183 | public function setDir($dir) |
||
| 187 | |||
| 188 | /** {@inheritdoc} */ |
||
| 189 | public function getDir() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get absolute path of server installation directory |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getAbsoluteDir() |
||
| 203 | |||
| 204 | public static function loadValidatorMetadata(ClassMetadata $metadata) |
||
| 208 | |||
| 209 | public function validateServer(ExecutionContextInterface $context) |
||
| 234 | } |
||
| 235 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: