Conditions | 9 |
Paths | 7 |
Total Lines | 54 |
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 |
||
84 | public function onResourceDelete(GetResponseForExceptionEvent $event) |
||
85 | { |
||
86 | $exception = $event->getException(); |
||
87 | if (!$exception instanceof ForeignKeyConstraintViolationException) { |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | if (!$event->isMasterRequest()) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | $eventRequest = $event->getRequest(); |
||
96 | $requestAttributes = $eventRequest->attributes; |
||
97 | $originalRoute = $requestAttributes->get('_route'); |
||
98 | |||
99 | if (!$this->isMethodDelete($eventRequest) || |
||
100 | !$this->isSyliusRoute($originalRoute) || |
||
101 | !$this->isAdminSection($requestAttributes->get('_sylius', [])) |
||
102 | ) { |
||
103 | return; |
||
104 | } |
||
105 | |||
106 | $resourceName = $this->getResourceNameFromRoute($originalRoute); |
||
107 | |||
108 | $message = $this->translator->trans('sylius.resource.delete_error', ['%resource%' => $resourceName], 'flashes'); |
||
109 | |||
110 | if (!$this->isHtmlRequest($eventRequest)) { |
||
111 | $event->setResponse( |
||
112 | $this->viewHandler->handle(View::create([ |
||
113 | 'error' => [ |
||
114 | 'code' => $exception->getSQLState(), |
||
115 | 'message' => $message, |
||
116 | ], |
||
117 | ], Response::HTTP_METHOD_NOT_ALLOWED)) |
||
118 | ); |
||
119 | |||
120 | return; |
||
121 | } |
||
122 | |||
123 | if (null === $requestAttributes->get('_controller')) { |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $this->session->getBag('flashes')->add('error', $message); |
||
|
|||
128 | |||
129 | $referrer = $eventRequest->headers->get('referer'); |
||
130 | if (null !== $referrer) { |
||
131 | $event->setResponse(new RedirectResponse($referrer)); |
||
132 | |||
133 | return; |
||
134 | } |
||
135 | |||
136 | $event->setResponse($this->createRedirectResponse($originalRoute, ResourceActions::INDEX)); |
||
137 | } |
||
138 | |||
206 |
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: