| Conditions | 28 |
| Paths | 17 |
| Total Lines | 192 |
| Code Lines | 124 |
| Lines | 24 |
| Ratio | 12.5 % |
| 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 |
||
| 51 | public function editAction($moduleName) |
||
| 52 | { |
||
| 53 | $templateParameters = []; |
||
| 54 | // get module's name and assign it to template |
||
| 55 | $templateParameters['currentmodule'] = $moduleName; |
||
| 56 | |||
| 57 | // check if user has admin permission on this module |
||
| 58 | if (!$this->get('zikula_permissions_module.api.permission')->hasPermission($moduleName . '::', '::', ACCESS_ADMIN)) { |
||
| 59 | throw new AccessDeniedException(); |
||
| 60 | } |
||
| 61 | |||
| 62 | // find out the capabilities of the module |
||
| 63 | $isProvider = $this->get('zikula_hook_bundle.collector.hook_collector')->isCapable($moduleName, HookCollectorInterface::HOOK_PROVIDER); |
||
| 64 | $templateParameters['isProvider'] = $isProvider; |
||
| 65 | |||
| 66 | $isSubscriber = $this->get('zikula_hook_bundle.collector.hook_collector')->isCapable($moduleName, HookCollectorInterface::HOOK_SUBSCRIBER); |
||
| 67 | $templateParameters['isSubscriber'] = $isSubscriber; |
||
| 68 | |||
| 69 | $isSubscriberSelfCapable = $this->get('zikula_hook_bundle.collector.hook_collector')->isCapable($moduleName, HookCollectorInterface::HOOK_SUBSCRIBE_OWN); |
||
| 70 | $templateParameters['isSubscriberSelfCapable'] = $isSubscriberSelfCapable; |
||
| 71 | $templateParameters['providerAreas'] = []; |
||
| 72 | |||
| 73 | $nonPersistedProviders = $this->get('zikula_hook_bundle.collector.hook_collector')->getProviders(); |
||
| 74 | $nonPersistedSubscribers = $this->get('zikula_hook_bundle.collector.hook_collector')->getSubscribers(); |
||
| 75 | |||
| 76 | // get areas of module and bundle titles also |
||
| 77 | if ($isProvider) { |
||
| 78 | $providerAreas = $this->get('zikula_hook_bundle.collector.hook_collector')->getProviderAreasByOwner($moduleName); |
||
| 79 | $templateParameters['providerAreas'] = $providerAreas; |
||
| 80 | |||
| 81 | $providerAreasToTitles = []; |
||
| 82 | foreach ($providerAreas as $providerArea) { |
||
| 83 | if (isset($nonPersistedProviders[$providerArea])) { |
||
| 84 | $providerAreasToTitles[$providerArea] = $nonPersistedProviders[$providerArea]->getTitle(); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | $templateParameters['providerAreasToTitles'] = $providerAreasToTitles; |
||
| 88 | } |
||
| 89 | $templateParameters['subscriberAreas'] = []; |
||
| 90 | $templateParameters['hooksubscribers'] = []; |
||
| 91 | |||
| 92 | if ($isSubscriber) { |
||
| 93 | $subscriberAreas = $this->get('zikula_hook_bundle.collector.hook_collector')->getSubscriberAreasByOwner($moduleName); |
||
| 94 | $templateParameters['subscriberAreas'] = $subscriberAreas; |
||
| 95 | |||
| 96 | $subscriberAreasToTitles = []; |
||
| 97 | $subscriberAreasToCategories = []; |
||
| 98 | $subscriberAreasAndCategories = []; |
||
| 99 | View Code Duplication | foreach ($subscriberAreas as $subscriberArea) { |
|
|
|
|||
| 100 | if (isset($nonPersistedSubscribers[$subscriberArea])) { |
||
| 101 | $subscriberAreasToTitles[$subscriberArea] = $nonPersistedSubscribers[$subscriberArea]->getTitle(); |
||
| 102 | $category = $nonPersistedSubscribers[$subscriberArea]->getCategory(); |
||
| 103 | } |
||
| 104 | $subscriberAreasToCategories[$subscriberArea] = $category; |
||
| 105 | $subscriberAreasAndCategories[$category][] = $subscriberArea; |
||
| 106 | } |
||
| 107 | $templateParameters['subscriberAreasToTitles'] = $subscriberAreasToTitles; |
||
| 108 | $templateParameters['subscriberAreasToCategories'] = $subscriberAreasToCategories; |
||
| 109 | $templateParameters['subscriberAreasAndCategories'] = $subscriberAreasAndCategories; |
||
| 110 | } |
||
| 111 | |||
| 112 | // get available subscribers that can attach to provider |
||
| 113 | if ($isProvider && !empty($providerAreas)) { |
||
| 114 | /** @var ExtensionEntity[] $hooksubscribers */ |
||
| 115 | $hooksubscribers = $this->getExtensionsCapableOf(HookCollectorInterface::HOOK_SUBSCRIBER); |
||
| 116 | $amountOfHookSubscribers = count($hooksubscribers); |
||
| 117 | $amountOfAvailableSubscriberAreas = 0; |
||
| 118 | for ($i = 0; $i < $amountOfHookSubscribers; $i++) { |
||
| 119 | $hooksubscribers[$i] = $hooksubscribers[$i]->toArray(); |
||
| 120 | // don't allow subscriber and provider to be the same |
||
| 121 | // unless subscriber has the ability to connect to it's own providers |
||
| 122 | if ($hooksubscribers[$i]['name'] == $moduleName) { |
||
| 123 | unset($hooksubscribers[$i]); |
||
| 124 | continue; |
||
| 125 | } |
||
| 126 | // does the user have admin permissions on the subscriber module? |
||
| 127 | View Code Duplication | if (!$this->get('zikula_permissions_module.api.permission')->hasPermission($hooksubscribers[$i]['name'] . "::", '::', ACCESS_ADMIN)) { |
|
| 128 | unset($hooksubscribers[$i]); |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | // get the areas of the subscriber |
||
| 133 | $hooksubscriberAreas = $this->get('zikula_hook_bundle.collector.hook_collector')->getSubscriberAreasByOwner($hooksubscribers[$i]['name']); |
||
| 134 | $hooksubscribers[$i]['areas'] = $hooksubscriberAreas; |
||
| 135 | $amountOfAvailableSubscriberAreas += count($hooksubscriberAreas); |
||
| 136 | |||
| 137 | $hooksubscriberAreasToTitles = []; // and get the titles |
||
| 138 | $hooksubscriberAreasToCategories = []; // and get the categories |
||
| 139 | foreach ($hooksubscriberAreas as $hooksubscriberArea) { |
||
| 140 | if (isset($nonPersistedSubscribers[$hooksubscriberArea])) { |
||
| 141 | $hooksubscriberAreasToTitles[$hooksubscriberArea] = $nonPersistedSubscribers[$hooksubscriberArea]->getTitle(); |
||
| 142 | $category = $nonPersistedSubscribers[$hooksubscriberArea]->getCategory(); |
||
| 143 | } |
||
| 144 | $hooksubscriberAreasToCategories[$hooksubscriberArea] = $category; |
||
| 145 | } |
||
| 146 | $hooksubscribers[$i]['areasToTitles'] = $hooksubscriberAreasToTitles; |
||
| 147 | $hooksubscribers[$i]['areasToCategories'] = $hooksubscriberAreasToCategories; |
||
| 148 | } |
||
| 149 | $templateParameters['hooksubscribers'] = $hooksubscribers; |
||
| 150 | $templateParameters['total_available_subscriber_areas'] = $amountOfAvailableSubscriberAreas; |
||
| 151 | } else { |
||
| 152 | $templateParameters['total_available_subscriber_areas'] = 0; |
||
| 153 | } |
||
| 154 | |||
| 155 | // get providers that are already attached to the subscriber |
||
| 156 | // and providers that can attach to the subscriber |
||
| 157 | if ($isSubscriber && !empty($subscriberAreas)) { |
||
| 158 | // get current sorting |
||
| 159 | $currentSortingTitles = []; |
||
| 160 | $currentSorting = []; |
||
| 161 | $amountOfAttachedProviderAreas = 0; |
||
| 162 | $amountOfSubscriberAreas = count($subscriberAreas); |
||
| 163 | for ($i = 0; $i < $amountOfSubscriberAreas; $i++) { |
||
| 164 | $sortsByArea = $this->get('hook_dispatcher')->getBindingsFor($subscriberAreas[$i]); |
||
| 165 | foreach ($sortsByArea as $sba) { |
||
| 166 | $areaname = $sba['areaname']; |
||
| 167 | $category = $sba['category']; |
||
| 168 | |||
| 169 | if (!isset($currentSorting[$category])) { |
||
| 170 | $currentSorting[$category] = []; |
||
| 171 | } |
||
| 172 | |||
| 173 | if (!isset($currentSorting[$category][$subscriberAreas[$i]])) { |
||
| 174 | $currentSorting[$category][$subscriberAreas[$i]] = []; |
||
| 175 | } |
||
| 176 | |||
| 177 | array_push($currentSorting[$category][$subscriberAreas[$i]], $areaname); |
||
| 178 | $amountOfAttachedProviderAreas++; |
||
| 179 | |||
| 180 | // get the bundle title |
||
| 181 | if (isset($nonPersistedProviders[$areaname])) { |
||
| 182 | $currentSortingTitles[$areaname] = $nonPersistedProviders[$areaname]->getTitle(); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | $templateParameters['areasSorting'] = $currentSorting; |
||
| 187 | $templateParameters['areasSortingTitles'] = $currentSortingTitles; |
||
| 188 | $templateParameters['total_attached_provider_areas'] = $amountOfAttachedProviderAreas; |
||
| 189 | |||
| 190 | // get available providers |
||
| 191 | /** @var ExtensionEntity[] $hookproviders */ |
||
| 192 | $hookproviders = $this->getExtensionsCapableOf(HookCollectorInterface::HOOK_PROVIDER); |
||
| 193 | $amountOfHookProviders = count($hookproviders); |
||
| 194 | $amountOfAvailableProviderAreas = 0; |
||
| 195 | for ($i = 0; $i < $amountOfHookProviders; $i++) { |
||
| 196 | $hookproviders[$i] = $hookproviders[$i]->toArray(); |
||
| 197 | // don't allow subscriber and provider to be the same |
||
| 198 | // unless subscriber has the ability to connect to it's own providers |
||
| 199 | if ($hookproviders[$i]['name'] == $moduleName && !$isSubscriberSelfCapable) { |
||
| 200 | unset($hookproviders[$i]); |
||
| 201 | continue; |
||
| 202 | } |
||
| 203 | |||
| 204 | // does the user have admin permissions on the provider module? |
||
| 205 | View Code Duplication | if (!$this->get('zikula_permissions_module.api.permission')->hasPermission($hookproviders[$i]['name']."::", '::', ACCESS_ADMIN)) { |
|
| 206 | unset($hookproviders[$i]); |
||
| 207 | continue; |
||
| 208 | } |
||
| 209 | |||
| 210 | // get the areas of the provider |
||
| 211 | $hookproviderAreas = $this->get('zikula_hook_bundle.collector.hook_collector')->getProviderAreasByOwner($hookproviders[$i]['name']); |
||
| 212 | $hookproviders[$i]['areas'] = $hookproviderAreas; |
||
| 213 | $amountOfAvailableProviderAreas += count($hookproviderAreas); |
||
| 214 | |||
| 215 | $hookproviderAreasToTitles = []; // and get the titles |
||
| 216 | $hookproviderAreasToCategories = []; // and get the categories |
||
| 217 | $hookproviderAreasAndCategories = []; // and build array with category => areas |
||
| 218 | View Code Duplication | foreach ($hookproviderAreas as $hookproviderArea) { |
|
| 219 | if (isset($nonPersistedProviders[$hookproviderArea])) { |
||
| 220 | $hookproviderAreasToTitles[$hookproviderArea] = $nonPersistedProviders[$hookproviderArea]->getTitle(); |
||
| 221 | $category = $nonPersistedProviders[$hookproviderArea]->getCategory(); |
||
| 222 | } |
||
| 223 | $hookproviderAreasToCategories[$hookproviderArea] = $category; |
||
| 224 | $hookproviderAreasAndCategories[$category][] = $hookproviderArea; |
||
| 225 | } |
||
| 226 | $hookproviders[$i]['areasToTitles'] = $hookproviderAreasToTitles; |
||
| 227 | $hookproviders[$i]['areasToCategories'] = $hookproviderAreasToCategories; |
||
| 228 | $hookproviders[$i]['areasAndCategories'] = $hookproviderAreasAndCategories; |
||
| 229 | } |
||
| 230 | $templateParameters['hookproviders'] = $hookproviders; |
||
| 231 | $templateParameters['total_available_provider_areas'] = $amountOfAvailableProviderAreas; |
||
| 232 | } else { |
||
| 233 | $templateParameters['hookproviders'] = []; |
||
| 234 | } |
||
| 235 | $templateParameters['hookDispatcher'] = $this->get('hook_dispatcher'); |
||
| 236 | $request = $this->get('request_stack')->getCurrentRequest(); |
||
| 237 | $request->attributes->set('_zkModule', $moduleName); |
||
| 238 | $request->attributes->set('_zkType', 'admin'); |
||
| 239 | $request->attributes->set('_zkFunc', 'Hooks'); |
||
| 240 | |||
| 241 | return $templateParameters; |
||
| 242 | } |
||
| 243 | |||
| 413 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.