Conditions | 27 |
Paths | 106 |
Total Lines | 131 |
Code Lines | 94 |
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 |
||
44 | public function contextMenu( |
||
45 | Request $request, |
||
46 | CategoryRepository $categoryRepository, |
||
47 | CategoryProcessingHelper $processingHelper, |
||
48 | LocaleApiInterface $localeApi, |
||
49 | string $action = 'edit', |
||
50 | CategoryEntity $category = null |
||
51 | ): JsonResponse { |
||
52 | if (!in_array($action, ['edit', 'delete', 'deleteandmovechildren', 'copy', 'activate', 'deactivate'])) { |
||
53 | return $this->json($this->trans('Data provided was inappropriate.'), Response::HTTP_BAD_REQUEST); |
||
54 | } |
||
55 | $mode = $request->request->get('mode', 'edit'); |
||
56 | |||
57 | switch ($action) { |
||
58 | case 'copy': |
||
59 | if (!isset($category)) { |
||
60 | $category = new CategoryEntity($localeApi->getSupportedLocales()); |
||
61 | } |
||
62 | $newCategory = clone $category; |
||
63 | $newCategory->setName($category->getName() . 'copy'); |
||
64 | $displayNames = []; |
||
65 | foreach ($newCategory->getDisplayName() as $locale => $displayName) { |
||
66 | $displayNames[$locale] = $displayName . ' ' . $this->trans('copy'); |
||
67 | } |
||
68 | $newCategory->setDisplayName($displayNames); |
||
69 | $action = 'edit'; |
||
70 | $mode = 'new'; |
||
71 | $category = $newCategory; |
||
72 | // intentionally no break here |
||
73 | // no break |
||
74 | case 'edit': |
||
75 | if (!isset($category)) { |
||
76 | $category = new CategoryEntity($localeApi->getSupportedLocales()); |
||
77 | $parentId = $request->request->get('parent'); |
||
78 | $mode = 'new'; |
||
79 | if (!empty($parentId)) { |
||
80 | /** @var CategoryEntity $parent */ |
||
81 | $parent = $categoryRepository->find($parentId); |
||
82 | $category->setParent($parent); |
||
83 | $category->setRoot($parent->getRoot()); |
||
84 | } elseif (empty($parentId) && $request->request->has('after')) { // sibling of top-level child |
||
85 | /** @var CategoryEntity $sibling */ |
||
86 | $sibling = $categoryRepository->find($request->request->get('after')); |
||
87 | $category->setParent($sibling->getParent()); |
||
88 | $category->setRoot($sibling->getRoot()); |
||
89 | } |
||
90 | } |
||
91 | $form = $this->createForm(CategoryType::class, $category, [ |
||
92 | 'locales' => $localeApi->getSupportedLocales() |
||
93 | ]); |
||
94 | $form->get('after')->setData($request->request->get('after')); |
||
95 | $form->handleRequest($request); |
||
96 | if ($form->isSubmitted() && $form->isValid()) { |
||
97 | $category = $form->getData(); |
||
98 | $after = $form->get('after')->getData(); |
||
99 | if (!empty($after)) { |
||
100 | $sibling = $categoryRepository->find($after); |
||
101 | $categoryRepository->persistAsNextSiblingOf($category, $sibling); |
||
102 | } elseif ('new' === $mode) { |
||
103 | $categoryRepository->persistAsLastChild($category); |
||
104 | } // no need to persist edited entity |
||
105 | $this->getDoctrine()->getManager()->flush(); |
||
106 | |||
107 | return $this->json([ |
||
108 | 'node' => $category->toJson($this->domTreeNodePrefix, $request->getLocale()), |
||
109 | 'mode' => $mode |
||
110 | ]); |
||
111 | } |
||
112 | $response = [ |
||
113 | 'result' => $this->renderView('@ZikulaCategoriesModule/Category/edit.html.twig', [ |
||
114 | 'locales' => $localeApi->getSupportedLocaleNames(null, $request->getLocale()), |
||
115 | 'form' => $form->createView() |
||
116 | ]), |
||
117 | 'action' => $action, |
||
118 | 'id' => $category->getId(), |
||
119 | 'mode' => $mode |
||
120 | ]; |
||
121 | break; |
||
122 | case 'deleteandmovechildren': |
||
123 | /** @var CategoryEntity $newParent */ |
||
124 | $newParent = $categoryRepository->find($request->request->get('parent', 1)); |
||
125 | if (null === $newParent || $newParent === $category->getParent()) { |
||
|
|||
126 | $response = ['result' => true]; |
||
127 | break; |
||
128 | } |
||
129 | // move the children |
||
130 | foreach ($category->getChildren() as $child) { |
||
131 | if ($processingHelper->mayCategoryBeDeletedOrMoved($child)) { |
||
132 | $category->getChildren()->removeElement($child); |
||
133 | $newParent->getChildren()->add($child); |
||
134 | $child->setParent($newParent); |
||
135 | } |
||
136 | } |
||
137 | $this->getDoctrine()->getManager()->flush(); |
||
138 | // intentionally no break here |
||
139 | // no break |
||
140 | case 'delete': |
||
141 | $categoryId = $category->getId(); |
||
142 | $this->removeRecursive($category, $processingHelper); |
||
143 | $categoryRemoved = false; |
||
144 | if (0 === $category->getChildren()->count() |
||
145 | && $processingHelper->mayCategoryBeDeletedOrMoved($category)) { |
||
146 | $this->getDoctrine()->getManager()->remove($category); |
||
147 | $categoryRemoved = true; |
||
148 | } |
||
149 | $this->getDoctrine()->getManager()->flush(); |
||
150 | $response = [ |
||
151 | 'result' => $categoryRemoved, |
||
152 | 'id' => $categoryId, |
||
153 | 'action' => $action, |
||
154 | 'parent' => isset($newParent) ? $newParent->getId() : null |
||
155 | ]; |
||
156 | $categoryRepository->recover(); |
||
157 | $this->getDoctrine()->getManager()->flush(); |
||
158 | break; |
||
159 | case 'activate': |
||
160 | case 'deactivate': |
||
161 | $category->setStatus('A' === $category->getStatus() ? 'I' : 'A'); |
||
162 | $this->getDoctrine()->getManager()->flush(); |
||
163 | $response = [ |
||
164 | 'id' => $category->getId(), |
||
165 | 'parent' => null !== $category->getParent() ? $category->getParent()->getId() : null, |
||
166 | 'action' => $action, |
||
167 | 'result' => true |
||
168 | ]; |
||
169 | break; |
||
170 | default: |
||
171 | $response = ['result' => true]; |
||
172 | } |
||
173 | |||
174 | return $this->json($response); |
||
175 | } |
||
232 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.