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 |
||
28 | class CallableClass extends RequestPlugin |
||
29 | { |
||
30 | use \Jaxon\Utils\Traits\Manager; |
||
31 | use \Jaxon\Utils\Traits\Validator; |
||
32 | use \Jaxon\Utils\Traits\Translator; |
||
33 | |||
34 | /** |
||
35 | * The callable repository |
||
36 | * |
||
37 | * @var CallableRepository |
||
38 | */ |
||
39 | protected $repository = null; |
||
40 | |||
41 | /** |
||
42 | * The value of the class parameter of the incoming Jaxon request |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $sRequestedClass = null; |
||
47 | |||
48 | /** |
||
49 | * The value of the method parameter of the incoming Jaxon request |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $sRequestedMethod = null; |
||
54 | |||
55 | /** |
||
56 | * The class constructor |
||
57 | * |
||
58 | * @param CallableRepository $repository |
||
59 | */ |
||
60 | public function __construct(CallableRepository $repository) |
||
61 | { |
||
62 | $this->repository = $repository; |
||
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) |
||
125 | |||
126 | /** |
||
127 | * Generate a hash for the registered callable objects |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function generateHash() |
||
135 | |||
136 | /** |
||
137 | * Generate client side javascript code for the registered callable objects |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getScript() |
||
145 | |||
146 | /** |
||
147 | * Check if this plugin can process the incoming Jaxon request |
||
148 | * |
||
149 | * @return boolean |
||
150 | */ |
||
151 | public function canProcessRequest() |
||
167 | |||
168 | /** |
||
169 | * Process the incoming Jaxon request |
||
170 | * |
||
171 | * @return boolean |
||
172 | */ |
||
173 | public function processRequest() |
||
195 | } |
||
196 |
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.