Conditions | 21 |
Paths | 21 |
Total Lines | 67 |
Code Lines | 48 |
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 |
||
52 | protected function voteOnUser(string $attribute, $subject, User $user): bool |
||
53 | { |
||
54 | //return $this->resolver->inherit($user, 'attachments', $attribute) ?? false; |
||
55 | |||
56 | if (!$subject instanceof AbstractParameter) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | //If the attachment has no element (which should not happen), we deny access, as we can not determine if the user is allowed to access the associated element |
||
61 | $target_element = $subject->getElement(); |
||
62 | if ($target_element !== null) { |
||
63 | //Depending on the operation delegate either to the attachments element or to the attachment permission |
||
64 | |||
65 | |||
66 | switch ($attribute) { |
||
67 | //We can view the attachment if we can view the element |
||
68 | case 'read': |
||
69 | case 'view': |
||
70 | $operation = 'read'; |
||
71 | break; |
||
72 | //We can edit/create/delete the attachment if we can edit the element |
||
73 | case 'edit': |
||
74 | case 'create': |
||
75 | case 'delete': |
||
76 | $operation = 'edit'; |
||
77 | break; |
||
78 | case 'show_history': |
||
79 | $operation = 'show_history'; |
||
80 | break; |
||
81 | case 'revert_element': |
||
82 | $operation = 'revert_element'; |
||
83 | break; |
||
84 | default: |
||
85 | throw new RuntimeException('Unknown operation: '.$attribute); |
||
86 | } |
||
87 | |||
88 | return $this->security->isGranted($operation, $target_element); |
||
89 | } |
||
90 | |||
91 | //If we do not have a concrete element, we delegate to the different categories |
||
92 | if ($subject instanceof AttachmentTypeParameter) { |
||
93 | $param = 'attachment_types'; |
||
94 | } elseif ($subject instanceof CategoryParameter) { |
||
95 | $param = 'categories'; |
||
96 | } elseif ($subject instanceof CurrencyParameter) { |
||
97 | $param = 'currencies'; |
||
98 | } elseif ($subject instanceof ProjectParameter) { |
||
99 | $param = 'projects'; |
||
100 | } elseif ($subject instanceof FootprintParameter) { |
||
101 | $param = 'footprints'; |
||
102 | } elseif ($subject instanceof GroupParameter) { |
||
103 | $param = 'groups'; |
||
104 | } elseif ($subject instanceof ManufacturerParameter) { |
||
105 | $param = 'manufacturers'; |
||
106 | } elseif ($subject instanceof MeasurementUnitParameter) { |
||
107 | $param = 'measurement_units'; |
||
108 | } elseif ($subject instanceof PartParameter) { |
||
109 | $param = 'parts'; |
||
110 | } elseif ($subject instanceof StorelocationParameter) { |
||
111 | $param = 'storelocations'; |
||
112 | } elseif ($subject instanceof SupplierParameter) { |
||
113 | $param = 'suppliers'; |
||
114 | } else { |
||
115 | throw new RuntimeException('Encountered unknown Parameter type: ' . get_class($subject)); |
||
116 | } |
||
117 | |||
118 | return $this->resolver->inherit($user, $param, $attribute) ?? false; |
||
119 | } |
||
131 | } |