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 |
||
| 17 | abstract class RestController extends Controller { |
||
| 18 | protected $config; |
||
| 19 | protected $model; |
||
| 20 | protected $contentType; |
||
| 21 | protected $restCache; |
||
| 22 | /** |
||
| 23 | * @var ResponseFormatter |
||
| 24 | */ |
||
| 25 | protected $responseFormatter; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var RestServer |
||
| 29 | */ |
||
| 30 | protected $server; |
||
| 31 | |||
| 32 | public function __construct(){ |
||
| 45 | |||
| 46 | public function isValid(){ |
||
| 54 | |||
| 55 | public function onInvalidControl(){ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Realise the connection to the server |
||
| 61 | * To override in derived classes to define your own authentication |
||
| 62 | */ |
||
| 63 | public function connect(){ |
||
| 66 | |||
| 67 | public function initialize(){ |
||
| 77 | |||
| 78 | public function finalize(){ |
||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | public function _errorHandler($e){ |
||
| 92 | |||
| 93 | public function _setResponseCode($value){ |
||
| 96 | |||
| 97 | protected function connectDb($config){ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Updates $instance with $values |
||
| 106 | * To eventually be redefined in derived classes |
||
| 107 | * @param object $instance the instance to update |
||
| 108 | * @param array|null $values |
||
| 109 | */ |
||
| 110 | protected function _setValuesToObject($instance,$values=null){ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns all objects for the resource $model |
||
| 116 | * @route("methods"=>["get"],"cache"=>true,"duration"=>1000) |
||
| 117 | */ |
||
| 118 | public function index() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a list of objects from the server |
||
| 126 | * @param string $condition the sql Where part |
||
| 127 | * @param boolean $loadManyToOne |
||
| 128 | * @param boolean $loadOneToMany |
||
| 129 | * @param boolean $useCache |
||
| 130 | */ |
||
| 131 | public function get($condition="1=1",$loadManyToOne=false,$loadOneToMany=false,$useCache=false){ |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the first object corresponding to the $keyValues |
||
| 148 | * @param string $keyValues primary key(s) value(s) or condition |
||
| 149 | * @param boolean $loadManyToOne if true then manyToOne members are loaded. |
||
| 150 | * @param boolean $loadOneToMany if true then oneToMany members are loaded. |
||
| 151 | * @param boolean $useCache if true then response is cached |
||
| 152 | */ |
||
| 153 | public function getOne($keyValues,$loadManyToOne=false,$loadOneToMany=false,$useCache=false){ |
||
| 168 | |||
| 169 | public function _format($arrayMessage){ |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $member |
||
| 175 | * @param boolean $useCache |
||
| 176 | * @throws \Exception |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function getOneToMany($member,$useCache=false){ |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $member |
||
| 191 | * @param boolean $useCache |
||
| 192 | * @throws \Exception |
||
| 193 | */ |
||
| 194 | View Code Duplication | public function getManyToMany($member,$useCache=false){ |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Update an instance of $model selected by the primary key $keyValues |
||
| 207 | * Require members values in $_POST array |
||
| 208 | * @param array $keyValues |
||
| 209 | * @authorization |
||
| 210 | */ |
||
| 211 | View Code Duplication | public function update(...$keyValues){ |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Insert a new instance of $model |
||
| 229 | * Require members values in $_POST array |
||
| 230 | * @authorization |
||
| 231 | */ |
||
| 232 | public function add(){ |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Delete the instance of $model selected by the primary key $keyValues |
||
| 251 | * Requires an authorization with access token |
||
| 252 | * @param array $keyValues |
||
| 253 | * @route("methods"=>["delete"]) |
||
| 254 | * @authorization |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function delete(...$keyValues){ |
|
| 270 | } |
||
| 271 |
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.