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 |
||
| 2 | class Snidel_SharedMemory |
||
|
|
|||
| 3 | { |
||
| 4 | /** @int **/ |
||
| 5 | private $pid; |
||
| 6 | |||
| 7 | /** @int **/ |
||
| 8 | private $key; |
||
| 9 | |||
| 10 | /** @int **/ |
||
| 11 | private $segmentId; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param int $pid |
||
| 15 | */ |
||
| 16 | public function __construct($pid) |
||
| 21 | |||
| 22 | /** |
||
| 23 | * create or open shared memory block |
||
| 24 | * |
||
| 25 | * @param int $length |
||
| 26 | * @throws RuntimeException |
||
| 27 | */ |
||
| 28 | public function open($length = 0) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * write data into shared memory block |
||
| 40 | * |
||
| 41 | * @param string $data |
||
| 42 | * @throws RuntimeException |
||
| 43 | */ |
||
| 44 | public function write($data) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * read data from shared memory block |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | * @throws RuntimeException |
||
| 59 | */ |
||
| 60 | public function read() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * delete shared memory block |
||
| 72 | * |
||
| 73 | * @throws RuntimeException |
||
| 74 | */ |
||
| 75 | public function delete() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * cloase shared memory block |
||
| 84 | * |
||
| 85 | * @param bool $removeTmpFile |
||
| 86 | */ |
||
| 87 | public function close($removeTmpFile = false) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * generate IPC key |
||
| 97 | * |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | View Code Duplication | private function generateKey($pid) |
|
| 109 | } |
||
| 110 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.