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 | /** @var int **/ |
||
| 5 | private $pid; |
||
| 6 | |||
| 7 | /** @var int **/ |
||
| 8 | private $key; |
||
| 9 | |||
| 10 | /** @var int **/ |
||
| 11 | private $segmentId; |
||
| 12 | |||
| 13 | /** @const string **/ |
||
| 14 | const TMP_FILE_PREFIX = 'snidel_shm_'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param int $pid |
||
| 18 | */ |
||
| 19 | public function __construct($pid) |
||
| 20 | { |
||
| 21 | $this->pid = $pid; |
||
| 22 | $this->key = $this->generateKey($pid); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * create or open shared memory block |
||
| 27 | * |
||
| 28 | * @param int $length |
||
| 29 | * @throws Snidel_Exception_SharedMemoryControlException |
||
| 30 | */ |
||
| 31 | public function open($length = 0) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * write data into shared memory block |
||
| 43 | * |
||
| 44 | * @param string $data |
||
| 45 | * @throws Snidel_Exception_SharedMemoryControlException |
||
| 46 | */ |
||
| 47 | public function write($data) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * read data from shared memory block |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | * @throws Snidel_Exception_SharedMemoryControlException |
||
| 60 | */ |
||
| 61 | public function read() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * delete shared memory block |
||
| 73 | * |
||
| 74 | * @throws Snidel_Exception_SharedMemoryControlException |
||
| 75 | */ |
||
| 76 | public function delete() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * cloase shared memory block |
||
| 85 | * |
||
| 86 | * @param bool $removeTmpFile |
||
| 87 | */ |
||
| 88 | public function close($removeTmpFile = false) |
||
| 97 | |||
| 98 | public function exists() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * generate IPC key |
||
| 106 | * |
||
| 107 | * @return int |
||
| 108 | */ |
||
| 109 | View Code Duplication | private function generateKey($pid) |
|
| 118 | } |
||
| 119 |
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.