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 JsonadmController extends Controller |
||
29 | { |
||
30 | use AuthorizesRequests; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Deletes the resource object or a list of resource objects |
||
35 | * |
||
36 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
37 | * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output |
||
38 | */ |
||
39 | View Code Duplication | public function deleteAction( ServerRequestInterface $request ) |
|
40 | { |
||
41 | if( config( 'shop.authorize', true ) ) { |
||
42 | $this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] ); |
||
43 | } |
||
44 | |||
45 | return $this->createAdmin()->delete( $request, new Response() ); |
||
46 | } |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Returns the requested resource object or list of resource objects |
||
51 | * |
||
52 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
53 | * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output |
||
54 | */ |
||
55 | View Code Duplication | public function getAction( ServerRequestInterface $request ) |
|
56 | { |
||
57 | if( config( 'shop.authorize', true ) ) { |
||
58 | $this->authorize( 'admin', [JsonadmController::class, ['admin', 'api', 'editor']] ); |
||
59 | } |
||
60 | |||
61 | return $this->createAdmin()->get( $request, new Response() ); |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Updates a resource object or a list of resource objects |
||
67 | * |
||
68 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
69 | * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output |
||
70 | */ |
||
71 | View Code Duplication | public function patchAction( ServerRequestInterface $request ) |
|
72 | { |
||
73 | if( config( 'shop.authorize', true ) ) { |
||
74 | $this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] ); |
||
75 | } |
||
76 | |||
77 | return $this->createAdmin()->patch( $request, new Response() ); |
||
78 | } |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Creates a new resource object or a list of resource objects |
||
83 | * |
||
84 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
85 | * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output |
||
86 | */ |
||
87 | View Code Duplication | public function postAction( ServerRequestInterface $request ) |
|
88 | { |
||
89 | if( config( 'shop.authorize', true ) ) { |
||
90 | $this->authorize( 'admin', [JsonadmController::class, ['admin', 'api']] ); |
||
91 | } |
||
92 | |||
93 | return $this->createAdmin()->post( $request, new Response() ); |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Creates or updates a single resource object |
||
99 | * |
||
100 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
101 | * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output |
||
102 | */ |
||
103 | View Code Duplication | public function putAction( ServerRequestInterface $request ) |
|
111 | |||
112 | |||
113 | /** |
||
114 | * Returns the available HTTP verbs and the resource URLs |
||
115 | * |
||
116 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
117 | * @return \Psr\Http\Message\ResponseInterface Response object containing the generated output |
||
118 | */ |
||
119 | View Code Duplication | public function optionsAction( ServerRequestInterface $request ) |
|
127 | |||
128 | |||
129 | /** |
||
130 | * Returns the JsonAdm client |
||
131 | * |
||
132 | * @return \Aimeos\Admin\JsonAdm\Iface JsonAdm client |
||
133 | */ |
||
134 | protected function createAdmin() |
||
150 | } |
||
151 |