Conditions | 13 |
Paths | 10 |
Total Lines | 41 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
62 | public function handleAction(string $action, array $selected_parts, ?int $target_id): void |
||
63 | { |
||
64 | //Iterate over the parts and apply the action to it: |
||
65 | foreach ($selected_parts as $part) { |
||
66 | if (!$part instanceof Part) { |
||
67 | throw new \InvalidArgumentException('$selected_parts must be an array of Part elements!'); |
||
68 | } |
||
69 | |||
70 | //We modify parts, so you have to have the permission to modify it |
||
71 | $this->denyAccessUnlessGranted('edit', $part); |
||
72 | |||
73 | switch ($action) { |
||
74 | case 'favorite': |
||
75 | $part->setFavorite(true); |
||
76 | break; |
||
77 | case 'unfavorite': |
||
78 | $part->setFavorite(false); |
||
79 | break; |
||
80 | case 'delete': |
||
81 | $this->denyAccessUnlessGranted('delete', $part); |
||
82 | $this->entityManager->remove($part); |
||
83 | break; |
||
84 | case 'change_category': |
||
85 | $this->denyAccessUnlessGranted('category.edit', $part); |
||
86 | $part->setCategory($this->entityManager->find(Category::class, $target_id)); |
||
87 | break; |
||
88 | case 'change_footprint': |
||
89 | $this->denyAccessUnlessGranted('footprint.edit', $part); |
||
90 | $part->setFootprint($target_id === null ? null : $this->entityManager->find(Footprint::class, $target_id)); |
||
91 | break; |
||
92 | case 'change_manufacturer': |
||
93 | $this->denyAccessUnlessGranted('manufacturer.edit', $part); |
||
94 | $part->setManufacturer($target_id === null ? null : $this->entityManager->find(Manufacturer::class, $target_id)); |
||
95 | break; |
||
96 | case 'change_unit': |
||
97 | $this->denyAccessUnlessGranted('unit.edit', $part); |
||
98 | $part->setPartUnit($target_id === null ? null : $this->entityManager->find(MeasurementUnit::class, $target_id)); |
||
99 | break; |
||
100 | |||
101 | default: |
||
102 | throw new \InvalidArgumentException('The given action is unknown! (' . $action . ')'); |
||
103 | } |
||
123 | } |