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 CallableClass 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 CallableClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class CallableClass extends RequestPlugin |
||
| 28 | { |
||
| 29 | use \Jaxon\Utils\Traits\Config; |
||
| 30 | use \Jaxon\Utils\Traits\Manager; |
||
| 31 | use \Jaxon\Utils\Traits\Validator; |
||
| 32 | use \Jaxon\Utils\Traits\Translator; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The classes of the registered callable objects |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $aClassOptions = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The registered callable objects |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $aCallableObjects = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The value of the class parameter of the incoming Jaxon request |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $sRequestedClass = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The value of the method parameter of the incoming Jaxon request |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $sRequestedMethod = null; |
||
| 61 | |||
| 62 | View Code Duplication | public function __construct() |
|
|
|
|||
| 63 | { |
||
| 64 | if(!empty($_GET['jxncls'])) |
||
| 65 | { |
||
| 66 | $this->sRequestedClass = $_GET['jxncls']; |
||
| 67 | } |
||
| 68 | if(!empty($_GET['jxnmthd'])) |
||
| 69 | { |
||
| 70 | $this->sRequestedMethod = $_GET['jxnmthd']; |
||
| 71 | } |
||
| 72 | if(!empty($_POST['jxncls'])) |
||
| 73 | { |
||
| 74 | $this->sRequestedClass = $_POST['jxncls']; |
||
| 75 | } |
||
| 76 | if(!empty($_POST['jxnmthd'])) |
||
| 77 | { |
||
| 78 | $this->sRequestedMethod = $_POST['jxnmthd']; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return the name of this plugin |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getName() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Register a callable class |
||
| 94 | * |
||
| 95 | * @param string $sType The type of request handler being registered |
||
| 96 | * @param string $sClassName The name of the class being registered |
||
| 97 | * @param array|string $aOptions The associated options |
||
| 98 | * |
||
| 99 | * @return boolean |
||
| 100 | */ |
||
| 101 | public function register($sType, $sClassName, $aOptions) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Find a callable object by class name |
||
| 125 | * |
||
| 126 | * @param string $sClassName The class name of the callable object |
||
| 127 | * |
||
| 128 | * @return object |
||
| 129 | */ |
||
| 130 | public function getCallableObject($sClassName) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Find a user registered callable object by class name |
||
| 181 | * |
||
| 182 | * @param string $sClassName The class name of the callable object |
||
| 183 | * |
||
| 184 | * @return object |
||
| 185 | */ |
||
| 186 | public function getRegisteredObject($sClassName) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Create callable objects for all registered namespaces |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | private function createCallableObjects() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Generate a hash for the registered callable objects |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function generateHash() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Generate client side javascript code for the registered callable objects |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getScript() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Check if this plugin can process the incoming Jaxon request |
||
| 267 | * |
||
| 268 | * @return boolean |
||
| 269 | */ |
||
| 270 | public function canProcessRequest() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Process the incoming Jaxon request |
||
| 290 | * |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function processRequest() |
|
| 315 | } |
||
| 316 |
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.