Conditions | 10 |
Paths | 39 |
Total Lines | 59 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
53 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) |
||
54 | { |
||
55 | $view = $this->getView(); |
||
56 | |||
57 | try |
||
58 | { |
||
59 | $relId = $view->param( 'relatedid' ); |
||
60 | $body = (string) $request->getBody(); |
||
61 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
62 | |||
63 | if( $relId === '' || $relId === null ) |
||
64 | { |
||
65 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
66 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
||
67 | } |
||
68 | |||
69 | if( !is_array( $payload->data ) ) { |
||
70 | $payload->data = [$payload->data]; |
||
71 | } |
||
72 | |||
73 | foreach( $payload->data as $entry ) |
||
74 | { |
||
75 | if( !isset( $entry->id ) ) { |
||
76 | throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Type (ID) is missing' ) ); |
||
77 | } |
||
78 | |||
79 | $this->controller->setAddress( $entry->id, null ); |
||
80 | } |
||
81 | } |
||
82 | else |
||
83 | { |
||
84 | $this->controller->setAddress( $relId, null ); |
||
85 | } |
||
86 | |||
87 | |||
88 | $view->items = $this->controller->get(); |
||
89 | $view->total = 1; |
||
90 | |||
91 | $status = 200; |
||
92 | } |
||
93 | catch( \Aimeos\MShop\Exception $e ) |
||
94 | { |
||
95 | $status = 404; |
||
96 | $view->errors = array( array( |
||
97 | 'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
||
98 | 'detail' => $e->getTraceAsString(), |
||
99 | ) ); |
||
100 | } |
||
101 | catch( \Exception $e ) |
||
102 | { |
||
103 | $status = 500; |
||
104 | $view->errors = array( array( |
||
105 | 'title' => $e->getMessage(), |
||
106 | 'detail' => $e->getTraceAsString(), |
||
107 | ) ); |
||
108 | } |
||
109 | |||
110 | return $this->render( $response, $view, $status ); |
||
111 | } |
||
112 | |||
195 |