Conditions | 9 |
Paths | 42 |
Total Lines | 56 |
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 |
||
59 | public function __invoke(Request $request): Response |
||
60 | { |
||
61 | $validationResults = []; |
||
62 | $commandRequests = []; |
||
63 | $commandsToExecute = []; |
||
64 | $token = $request->attributes->get('token'); |
||
65 | |||
66 | foreach ($request->request->all() as $item) { |
||
67 | $item['token'] = $token; |
||
68 | $commandRequests[] = $this->provideCommandRequest($item); |
||
69 | } |
||
70 | |||
71 | foreach ($commandRequests as $commandRequest) { |
||
72 | $validationResult = $this->validator->validate($commandRequest); |
||
73 | |||
74 | if (0 === count($validationResult)) { |
||
75 | $commandsToExecute[] = $commandRequest->getCommand(); |
||
76 | } |
||
77 | |||
78 | $validationResults[] = $validationResult; |
||
79 | } |
||
80 | |||
81 | if (!$this->isValid($validationResults)) { |
||
82 | /** @var ValidationErrorView $errorMessage */ |
||
83 | $errorMessage = new $this->validationErrorViewClass(); |
||
84 | |||
85 | $errorMessage->code = Response::HTTP_BAD_REQUEST; |
||
86 | $errorMessage->message = 'Validation failed'; |
||
87 | |||
88 | /** @var ConstraintViolationInterface $result */ |
||
89 | foreach ($validationResults as $validationResult) { |
||
90 | $iteration = 0; |
||
|
|||
91 | $errors = []; |
||
92 | |||
93 | foreach ($validationResult as $result) { |
||
94 | $errors[$result->getPropertyPath()][] = $result->getMessage(); |
||
95 | } |
||
96 | |||
97 | $errorMessage->errors[] = $errors; |
||
98 | } |
||
99 | |||
100 | return $this->viewHandler->handle(View::create($errorMessage, Response::HTTP_BAD_REQUEST)); |
||
101 | } |
||
102 | |||
103 | foreach ($commandsToExecute as $commandToExecute) { |
||
104 | $this->bus->handle($commandToExecute); |
||
105 | } |
||
106 | |||
107 | try { |
||
108 | return $this->viewHandler->handle( |
||
109 | View::create($this->cartQuery->getOneByToken($token), Response::HTTP_CREATED) |
||
110 | ); |
||
111 | } catch (\InvalidArgumentException $exception) { |
||
112 | throw new BadRequestHttpException($exception->getMessage()); |
||
113 | } |
||
114 | } |
||
115 | |||
149 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.