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 |
||
| 38 | class ReplyMessage |
||
| 39 | { |
||
| 40 | const NULL = "_null"; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $commandIdentifier; |
||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $success; |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $resultType; |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $resultRevision; |
||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $serializedResult; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var mixed |
||
| 65 | */ |
||
| 66 | private $result; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructs a message containing a reply to the command with given <code>commandIdentifier</code>, containing |
||
| 70 | * either given <code>returnValue</code> or <code>error</code>, which uses the given <code>serializer</code> to |
||
| 71 | * deserialize its contents. |
||
| 72 | * |
||
| 73 | * @param string $commandIdentifier The identifier of the command to which the message is a reply |
||
| 74 | * @param SerializerInterface $serializer The serializer to serialize the message contents with |
||
| 75 | * @param mixed $returnValue The return value of command process |
||
| 76 | * @param bool $success <code>true</code> if the returnValue represents the completion of the command <code>false</code> otherwise |
||
| 77 | */ |
||
| 78 | 4 | public function __construct( |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Returns the returnValue of the command processing. If {@link #isSuccess()} return <code>false</code>, this |
||
| 106 | * method returns <code>null</code>. This method also returns <code>null</code> if response processing returned |
||
| 107 | * a <code>null</code> value. |
||
| 108 | * |
||
| 109 | * @return mixed The return value of command processing |
||
| 110 | */ |
||
| 111 | 4 | public function getReturnValue() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the error of the command processing. If {@link #isSuccess()} return <code>true</code>, this |
||
| 122 | * method returns <code>null</code>. |
||
| 123 | * |
||
| 124 | * @return \Exception The exception thrown during command processing |
||
| 125 | */ |
||
| 126 | 1 | public function getError() |
|
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Whether the reply message represents a successfully executed command. In this case, successful means that the |
||
| 138 | * command's execution did not result in an exception. |
||
| 139 | * |
||
| 140 | * @return boolean <code>true</code> if this reply contains a return value, <code>false</code> if it contains an error. |
||
| 141 | */ |
||
| 142 | 4 | public function isSuccess() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 4 | public function getCommandIdentifier() |
|
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 4 | public function toBytes() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param SerializerInterface $serializer The serialize to deserialize message contents with |
||
| 181 | * @param mixed $data |
||
| 182 | * @return self |
||
| 183 | */ |
||
| 184 | 4 | public static function fromBytes( |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param mixed $raw |
||
| 209 | * @param int $offset |
||
| 210 | * @param mixed $data |
||
| 211 | * @param string $name |
||
| 212 | */ |
||
| 213 | 4 | View Code Duplication | private static function read(&$raw, &$offset, $data, $name) |
| 230 | } |
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.