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:
Complex classes like RestController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RestController, and based on these observations, apply Extract Interface, too.
| 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(){ |
||
| 46 | |||
| 47 | public function isValid($action){ |
||
| 55 | |||
| 56 | public function onInvalidControl(){ |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Realise the connection to the server |
||
| 62 | * To override in derived classes to define your own authentication |
||
| 63 | */ |
||
| 64 | public function connect(){ |
||
| 67 | |||
| 68 | public function initialize(){ |
||
| 78 | |||
| 79 | public function finalize(){ |
||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | public function _errorHandler($e){ |
||
| 93 | |||
| 94 | public function _setResponseCode($value){ |
||
| 97 | |||
| 98 | protected function connectDb($config){ |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Updates $instance with $values |
||
| 107 | * To eventually be redefined in derived classes |
||
| 108 | * @param object $instance the instance to update |
||
| 109 | * @param array|null $values |
||
| 110 | */ |
||
| 111 | protected function _setValuesToObject($instance,$values=null){ |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns all objects for the resource $model |
||
| 120 | * @route("cache"=>false) |
||
| 121 | */ |
||
| 122 | public function index() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Default route for requiring a single object |
||
| 129 | * @route("{id}","methods"=>["get","options"]) |
||
| 130 | */ |
||
| 131 | public function getById($id){ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns a list of objects from the server |
||
| 137 | * @param string $condition the sql Where part |
||
| 138 | * @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
||
| 139 | * @param boolean $useCache |
||
| 140 | */ |
||
| 141 | public function get($condition="1=1",$included=false,$useCache=false){ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the first object corresponding to the $keyValues |
||
| 156 | * @param string $keyValues primary key(s) value(s) or condition |
||
| 157 | * @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
||
| 158 | * @param boolean $useCache if true then response is cached |
||
| 159 | */ |
||
| 160 | public function getOne($keyValues,$included=false,$useCache=false){ |
||
| 174 | |||
| 175 | private function getIncluded($included){ |
||
| 181 | |||
| 182 | public function _format($arrayMessage){ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $member |
||
| 188 | * @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
||
| 189 | * @param boolean $useCache |
||
| 190 | * @throws \Exception |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function getOneToMany($member,$included=false,$useCache=false){ |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $member |
||
| 205 | * @param boolean|string $included if true, loads associate members with associations, if string, example : client.*,commands |
||
| 206 | * @param boolean $useCache |
||
| 207 | * @throws \Exception |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function getManyToMany($member,$included=false,$useCache=false){ |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Update an instance of $model selected by the primary key $keyValues |
||
| 222 | * Require members values in $_POST array |
||
| 223 | * @param array $keyValues |
||
| 224 | * @authorization |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function update(...$keyValues){ |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Insert a new instance of $model |
||
| 244 | * Require members values in $_POST array |
||
| 245 | * @authorization |
||
| 246 | */ |
||
| 247 | public function add(){ |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Delete the instance of $model selected by the primary key $keyValues |
||
| 266 | * Requires an authorization with access token |
||
| 267 | * @param array $keyValues |
||
| 268 | * @route("methods"=>["delete"]) |
||
| 269 | * @authorization |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function delete(...$keyValues){ |
|
| 285 | } |
||
| 286 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: