| Conditions | 10 |
| Paths | 22 |
| Total Lines | 67 |
| Lines | 10 |
| Ratio | 14.93 % |
| 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 |
||
| 31 | public function run(RequestInterface $request, ResponseInterface $response) |
||
| 32 | { |
||
| 33 | $params = $request->getParams(); |
||
|
|
|||
| 34 | |||
| 35 | View Code Duplication | if ( |
|
| 36 | !isset($params['attachment_id']) || |
||
| 37 | !isset($params['obj_id']) || |
||
| 38 | !isset($params['obj_type']) || |
||
| 39 | !isset($params['group']) |
||
| 40 | ) { |
||
| 41 | $this->setSuccess(false); |
||
| 42 | |||
| 43 | return $response; |
||
| 44 | } |
||
| 45 | |||
| 46 | $attachmentId = $params['attachment_id']; |
||
| 47 | $objId = $params['obj_id']; |
||
| 48 | $objType = $params['obj_type']; |
||
| 49 | $group = $params['group']; |
||
| 50 | |||
| 51 | // Try loading the object |
||
| 52 | try { |
||
| 53 | $obj = $this->modelFactory()->create($objType)->load($objId); |
||
| 54 | } catch (Exception $e) { |
||
| 55 | $this->setSuccess(false); |
||
| 56 | |||
| 57 | return $response; |
||
| 58 | } |
||
| 59 | |||
| 60 | $joinProto = $this->modelFactory()->create(Join::class); |
||
| 61 | if (!$joinProto->source()->tableExists()) { |
||
| 62 | $joinProto->source()->createTable(); |
||
| 63 | } |
||
| 64 | |||
| 65 | $loader = new CollectionLoader([ |
||
| 66 | 'logger' => $this->logger, |
||
| 67 | 'factory' => $this->modelFactory() |
||
| 68 | ]); |
||
| 69 | $loader |
||
| 70 | ->setModel($joinProto) |
||
| 71 | ->addFilter('object_type', $objType) |
||
| 72 | ->addFilter('object_id', $objId) |
||
| 73 | ->addFilter('attachment_id', $attachmentId) |
||
| 74 | ->addFilter('group', $group); |
||
| 75 | |||
| 76 | $existingJoins = $loader->load(); |
||
| 77 | |||
| 78 | // Should be just one, tho. |
||
| 79 | foreach ($existingJoins as $joinModel) { |
||
| 80 | $joinModel->delete(); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Try loading the attachment |
||
| 84 | try { |
||
| 85 | $attachment = $this->modelFactory()->create(Attachment::class)->load($attachmentId); |
||
| 86 | if ($attachment['id'] !== null) { |
||
| 87 | $attachment->delete(); |
||
| 88 | } |
||
| 89 | } catch (Exception $error) { |
||
| 90 | $this->setSuccess(false); |
||
| 91 | return $response; |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->setSuccess(true); |
||
| 95 | |||
| 96 | return $response; |
||
| 97 | } |
||
| 98 | } |
||
| 99 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: