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 |
||
| 33 | class Breaker |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * $cache. |
||
| 37 | * |
||
| 38 | * @var Cache |
||
| 39 | */ |
||
| 40 | protected $store; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * $config. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $config; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * $handler. |
||
| 51 | * |
||
| 52 | * @var HandlerInterface |
||
| 53 | */ |
||
| 54 | protected $handler; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * $dispatcher. |
||
| 58 | * |
||
| 59 | * @var EventDispatcherInterface |
||
| 60 | */ |
||
| 61 | protected $dispatcher; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Constructor. |
||
| 65 | * |
||
| 66 | * @param string $name |
||
| 67 | * @param array $config |
||
| 68 | * @param Cache $store |
||
| 69 | * @param Handler $handler |
||
| 70 | * @param EventDispatcherInterface $dispatcher |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * protect. |
||
| 101 | * |
||
| 102 | * @param \Closure $closure |
||
| 103 | * @throw \Exception |
||
| 104 | * |
||
| 105 | * @return mixed |
||
| 106 | */ |
||
| 107 | public function protect(\Closure $closure) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * addListener. |
||
| 144 | * |
||
| 145 | * @param string $eventName |
||
| 146 | * @param \Closure|array $listener |
||
| 147 | */ |
||
| 148 | public function addListener($eventName, $listener) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * isClosed. |
||
| 155 | * |
||
| 156 | * @param Circuit $circuit |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | View Code Duplication | protected function isClosed($circuit) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * isOpen. |
||
| 173 | * |
||
| 174 | * @param Circuit $circuit |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | View Code Duplication | protected function isOpen($circuit) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * isHalfOpen. |
||
| 191 | * |
||
| 192 | * @param Circuit $circuit |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | View Code Duplication | protected function isHalfOpen($circuit) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * success. |
||
| 209 | * |
||
| 210 | * @param Circuit $circuit |
||
| 211 | */ |
||
| 212 | protected function success($circuit) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * failure. |
||
| 222 | * |
||
| 223 | * @param Circuit $circuit |
||
| 224 | */ |
||
| 225 | protected function failure(Circuit $circuit) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * loadCircuit. |
||
| 236 | * |
||
| 237 | * @param string $name |
||
| 238 | * |
||
| 239 | * @return Circuit |
||
| 240 | */ |
||
| 241 | protected function loadCircuit($name) |
||
| 253 | |||
| 254 | protected function writeToStore(Circuit $circuit) |
||
| 258 | } |
||
| 259 |
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: