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 |
||
| 6 | class SharedMemory |
||
| 7 | { |
||
| 8 | /** @var int **/ |
||
| 9 | private $pid; |
||
| 10 | |||
| 11 | /** @var int **/ |
||
| 12 | private $key; |
||
| 13 | |||
| 14 | /** @var int **/ |
||
| 15 | private $segmentId; |
||
| 16 | |||
| 17 | /** @const string **/ |
||
| 18 | const TMP_FILE_PREFIX = 'snidel_shm_'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param int $pid |
||
| 22 | */ |
||
| 23 | public function __construct($pid) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * create or open shared memory block |
||
| 31 | * |
||
| 32 | * @param int $length |
||
| 33 | * @throws Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 34 | */ |
||
| 35 | public function open($length = 0) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * write data into shared memory block |
||
| 47 | * |
||
| 48 | * @param string $data |
||
| 49 | * @throws Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 50 | */ |
||
| 51 | public function write($data) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * read data from shared memory block |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | * @throws Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 64 | */ |
||
| 65 | public function read() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * delete shared memory block |
||
| 77 | * |
||
| 78 | * @throws Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 79 | */ |
||
| 80 | public function delete() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * cloase shared memory block |
||
| 89 | * |
||
| 90 | * @param bool $removeTmpFile |
||
| 91 | */ |
||
| 92 | public function close($removeTmpFile = false) |
||
| 101 | |||
| 102 | public function exists() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * generate IPC key |
||
| 110 | * |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | View Code Duplication | private function generateKey($pid) |
|
| 122 | } |
||
| 123 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.