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 |
||
| 16 | class UDP extends Generic |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Hostname |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $host; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Port |
||
| 26 | * @var integer |
||
| 27 | */ |
||
| 28 | protected $port; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Listener mode? |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | protected $listenerMode = true; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Default port |
||
| 38 | * @var integer |
||
| 39 | */ |
||
| 40 | protected $defaultPort; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Reuse? |
||
| 44 | * @var boolean |
||
| 45 | */ |
||
| 46 | protected $reuse = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Ports map |
||
| 50 | * @var array [portNumber => Connection] |
||
| 51 | */ |
||
| 52 | protected $portsMap = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Sets default port |
||
| 56 | * @param integer $port Port |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | public function setDefaultPort($port) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Send UDP packet |
||
| 66 | * @param string $data Data |
||
| 67 | * @param integer $flags Flags |
||
| 68 | * @param string $host Host |
||
| 69 | * @param integer $port Port |
||
| 70 | * @return integer |
||
| 71 | */ |
||
| 72 | public function sendTo($data, $flags, $host, $port) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Unassigns addr |
||
| 79 | * @param string $addr Address |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function unassignAddr($addr) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets reuse |
||
| 89 | * @param integer $reuse Port |
||
|
|
|||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function setReuse($reuse = true) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Bind given addreess |
||
| 99 | * @return boolean Success. |
||
| 100 | */ |
||
| 101 | public function bindSocket() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Called when socket is bound |
||
| 143 | * @return boolean Success |
||
| 144 | */ |
||
| 145 | protected function onBound() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Enable socket events |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function enable() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Called when we got UDP packet |
||
| 184 | * @param resource $stream Descriptor |
||
| 185 | * @param integer $events Events |
||
| 186 | * @param mixed $arg Attached variable |
||
| 187 | * @return boolean Success. |
||
| 188 | */ |
||
| 189 | public function onReadUdp($stream = null, $events = 0, $arg = null) |
||
| 236 | } |
||
| 237 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.