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  | 
            ||
| 21 | class InMemory implements Readable, Modifiable, Searchable, Aggregational, Queryable { | 
            ||
| 22 | |||
| 23 | /**  | 
            ||
| 24 | * The in-memory data.  | 
            ||
| 25 | *  | 
            ||
| 26 | * @var array  | 
            ||
| 27 | */  | 
            ||
| 28 | protected $data;  | 
            ||
| 29 | |||
| 30 | /**  | 
            ||
| 31 | * Filters results in-memory.  | 
            ||
| 32 | *  | 
            ||
| 33 | * @var Filterer  | 
            ||
| 34 | */  | 
            ||
| 35 | protected $filterer;  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Sorts results in-memory.  | 
            ||
| 39 | *  | 
            ||
| 40 | * @var Sorter  | 
            ||
| 41 | */  | 
            ||
| 42 | protected $sorter;  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * Create a new in-memory storage interface with the given data.  | 
            ||
| 46 | *  | 
            ||
| 47 | * @param array $data [optional]  | 
            ||
| 48 | */  | 
            ||
| 49 | 	public function __construct(array $data = array()) { | 
            ||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * Limit the given data to the given length and offset.  | 
            ||
| 57 | *  | 
            ||
| 58 | * @param array $data  | 
            ||
| 59 | * @param int $limit [optional]  | 
            ||
| 60 | * @param int $offset [optional]  | 
            ||
| 61 | * @return array  | 
            ||
| 62 | */  | 
            ||
| 63 | 	protected static function limit(array $data, $limit = 0, $offset = 0) { | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * Retrieve resource data using the given criteria.  | 
            ||
| 69 | *  | 
            ||
| 70 | * Returns an array of associative arrays.  | 
            ||
| 71 | *  | 
            ||
| 72 | * @param string $resource  | 
            ||
| 73 | * @param array $filter [optional]  | 
            ||
| 74 | * @param array|string $order [optional]  | 
            ||
| 75 | * @param int $limit [optional]  | 
            ||
| 76 | * @param int $offset [optional]  | 
            ||
| 77 | * @return array  | 
            ||
| 78 | */  | 
            ||
| 79 | 	public function read($resource, array $filter = array(), $order = array(), $limit = 0, $offset = 0) { | 
            ||
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * Retrieve specific fields of a resource.  | 
            ||
| 95 | *  | 
            ||
| 96 | * Returns an array of associative arrays.  | 
            ||
| 97 | *  | 
            ||
| 98 | * @param string $resource  | 
            ||
| 99 | * @param array|string $fields  | 
            ||
| 100 | * @param array $filter [optional]  | 
            ||
| 101 | * @param array|string $order [optional]  | 
            ||
| 102 | * @param int $limit [optional]  | 
            ||
| 103 | * @param int $offset [optional]  | 
            ||
| 104 | * @return array  | 
            ||
| 105 | */  | 
            ||
| 106 | 	public function listing($resource, $fields, array $filter = array(), $order = array(), $limit = 0, $offset = 0) { | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Count the given resource with an optional filter.  | 
            ||
| 132 | *  | 
            ||
| 133 | * @param string $resource  | 
            ||
| 134 | * @param array $filter [optional]  | 
            ||
| 135 | * @return int  | 
            ||
| 136 | */  | 
            ||
| 137 | View Code Duplication | 	public function count($resource, array $filter = array()) { | 
            |
| 144 | |||
| 145 | /**  | 
            ||
| 146 | * Create resource instances in the data store.  | 
            ||
| 147 | *  | 
            ||
| 148 | * @param string $resource  | 
            ||
| 149 | * @param array $data  | 
            ||
| 150 | * @return bool  | 
            ||
| 151 | */  | 
            ||
| 152 | 	public function create($resource, $data) { | 
            ||
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * Update resource instances in the data store.  | 
            ||
| 164 | *  | 
            ||
| 165 | * @param string $resource  | 
            ||
| 166 | * @param array $data  | 
            ||
| 167 | * @param array $filter [optional]  | 
            ||
| 168 | * @param int $limit [optional]  | 
            ||
| 169 | * @return int|bool  | 
            ||
| 170 | */  | 
            ||
| 171 | 	public function update($resource, $data, array $filter = array(), $limit = 0) { | 
            ||
| 195 | |||
| 196 | /**  | 
            ||
| 197 | * Delete resource instances from the data store.  | 
            ||
| 198 | *  | 
            ||
| 199 | * @param string $resource  | 
            ||
| 200 | * @param array $filter [optional]  | 
            ||
| 201 | * @param int $limit [optional]  | 
            ||
| 202 | * @return int|bool  | 
            ||
| 203 | */  | 
            ||
| 204 | View Code Duplication | 	public function delete($resource, array $filter = array(), $limit = null) { | 
            |
| 211 | |||
| 212 | /**  | 
            ||
| 213 | * Search for resource data with fields that match the given query and  | 
            ||
| 214 | * criteria.  | 
            ||
| 215 | *  | 
            ||
| 216 | * @param string $resource  | 
            ||
| 217 | * @param string $query  | 
            ||
| 218 | * @param array|string $fields  | 
            ||
| 219 | * @param array $filter [optional]  | 
            ||
| 220 | * @param array|string $order [optional]  | 
            ||
| 221 | * @param int $limit [optional]  | 
            ||
| 222 | * @param int $offset [optional]  | 
            ||
| 223 | * @return array  | 
            ||
| 224 | */  | 
            ||
| 225 | 	public function search($resource, $query, $fields, array $filter = array(), $order = array(), $limit = null, $offset = 0) { | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 | * Retrieve the distinct values of the given resource's field.  | 
            ||
| 244 | *  | 
            ||
| 245 | * Returns a flat array of values.  | 
            ||
| 246 | *  | 
            ||
| 247 | * @param string $resource  | 
            ||
| 248 | * @param string $field  | 
            ||
| 249 | * @param array $filter [optional]  | 
            ||
| 250 | * @param array $order [optional]  | 
            ||
| 251 | * @param int $limit [optional]  | 
            ||
| 252 | * @param int $offset [optional]  | 
            ||
| 253 | * @return array  | 
            ||
| 254 | */  | 
            ||
| 255 | 	public function distinct($resource, $field, array $filter = array(), $order = array(), $limit = 0, $offset = 0) { | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Execute the given query.  | 
            ||
| 269 | *  | 
            ||
| 270 | * @param Query $query  | 
            ||
| 271 | * @return Result  | 
            ||
| 272 | */  | 
            ||
| 273 | public function execute(Query $query)  | 
            ||
| 310 | |||
| 311 | /**  | 
            ||
| 312 | * Open a query on the given resource.  | 
            ||
| 313 | *  | 
            ||
| 314 | * @param string $resource  | 
            ||
| 315 | * @param array $fields [optional]  | 
            ||
| 316 | * @return Query\Builder  | 
            ||
| 317 | */  | 
            ||
| 318 | public function query($resource, $fields = array())  | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Retrieve the error that occured with the last operation.  | 
            ||
| 325 | *  | 
            ||
| 326 | * Returns false if there was no error.  | 
            ||
| 327 | *  | 
            ||
| 328 | * @return string|bool  | 
            ||
| 329 | */  | 
            ||
| 330 | 	public function error() { | 
            ||
| 333 | |||
| 334 | }  | 
            ||
| 335 | 
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.