| Conditions | 28 |
| Paths | 17 |
| Total Lines | 207 |
| Code Lines | 133 |
| Lines | 24 |
| Ratio | 11.59 % |
| 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 |
||
| 45 | public function editAction($moduleName) |
||
| 46 | { |
||
| 47 | $templateParameters = []; |
||
| 48 | // get module's name and assign it to template |
||
| 49 | $templateParameters['currentmodule'] = $moduleName; |
||
| 50 | |||
| 51 | // check if user has admin permission on this module |
||
| 52 | if (!$this->get('zikula_permissions_module.api.permission')->hasPermission($moduleName . '::', '::', ACCESS_ADMIN)) { |
||
| 53 | throw new AccessDeniedException(); |
||
| 54 | } |
||
| 55 | |||
| 56 | $metaData = $this->get('kernel')->getModule($moduleName)->getMetaData(); |
||
| 57 | $moduleVersionObj = $this->get('zikula_hook_bundle.api.hook')->getHookContainerInstance($metaData); |
||
|
|
|||
| 58 | |||
| 59 | // find out the capabilities of the module |
||
| 60 | $isProvider = $this->isCapable($moduleName, HookCollectorInterface::HOOK_PROVIDER); |
||
| 61 | $templateParameters['isProvider'] = $isProvider; |
||
| 62 | |||
| 63 | $isSubscriber = $this->isCapable($moduleName, HookCollectorInterface::HOOK_SUBSCRIBER); |
||
| 64 | $templateParameters['isSubscriber'] = $isSubscriber; |
||
| 65 | |||
| 66 | $isSubscriberSelfCapable = $this->isCapable($moduleName, HookCollectorInterface::HOOK_SUBSCRIBE_OWN); |
||
| 67 | $templateParameters['isSubscriberSelfCapable'] = $isSubscriberSelfCapable; |
||
| 68 | $templateParameters['providerAreas'] = []; |
||
| 69 | |||
| 70 | $nonPersistedProviders = $this->get('zikula_hook_bundle.collector.hook_collector')->getProviders(); |
||
| 71 | $nonPersistedSubscribers = $this->get('zikula_hook_bundle.collector.hook_collector')->getSubscribers(); |
||
| 72 | |||
| 73 | // get areas of module and bundle titles also |
||
| 74 | if ($isProvider) { |
||
| 75 | $providerAreas = $this->get('hook_dispatcher')->getProviderAreasByOwner($moduleName); |
||
| 76 | $templateParameters['providerAreas'] = $providerAreas; |
||
| 77 | |||
| 78 | $providerAreasToTitles = []; |
||
| 79 | foreach ($providerAreas as $providerArea) { |
||
| 80 | if (isset($nonPersistedProviders[$providerArea])) { |
||
| 81 | $providerAreasToTitles[$providerArea] = $nonPersistedProviders[$providerArea]->getTitle(); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | $templateParameters['providerAreasToTitles'] = $providerAreasToTitles; |
||
| 85 | } |
||
| 86 | $templateParameters['subscriberAreas'] = []; |
||
| 87 | $templateParameters['hooksubscribers'] = []; |
||
| 88 | |||
| 89 | if ($isSubscriber) { |
||
| 90 | $subscriberAreas = $this->get('hook_dispatcher')->getSubscriberAreasByOwner($moduleName); |
||
| 91 | $templateParameters['subscriberAreas'] = $subscriberAreas; |
||
| 92 | |||
| 93 | $subscriberAreasToTitles = []; |
||
| 94 | $subscriberAreasToCategories = []; |
||
| 95 | $subscriberAreasAndCategories = []; |
||
| 96 | View Code Duplication | foreach ($subscriberAreas as $subscriberArea) { |
|
| 97 | if (isset($nonPersistedSubscribers[$subscriberArea])) { |
||
| 98 | $subscriberAreasToTitles[$subscriberArea] = $nonPersistedSubscribers[$subscriberArea]->getTitle(); |
||
| 99 | $category = $nonPersistedSubscribers[$subscriberArea]->getCategory(); |
||
| 100 | } |
||
| 101 | $subscriberAreasToCategories[$subscriberArea] = $category; |
||
| 102 | $subscriberAreasAndCategories[$category][] = $subscriberArea; |
||
| 103 | } |
||
| 104 | $templateParameters['subscriberAreasToTitles'] = $subscriberAreasToTitles; |
||
| 105 | $templateParameters['subscriberAreasToCategories'] = $subscriberAreasToCategories; |
||
| 106 | $templateParameters['subscriberAreasAndCategories'] = $subscriberAreasAndCategories; |
||
| 107 | } |
||
| 108 | |||
| 109 | // get available subscribers that can attach to provider |
||
| 110 | if ($isProvider && !empty($providerAreas)) { |
||
| 111 | /** @var ExtensionEntity[] $hooksubscribers */ |
||
| 112 | $hooksubscribers = $this->getExtensionsCapableOf(HookCollectorInterface::HOOK_SUBSCRIBER); |
||
| 113 | $amountOfHookSubscribers = count($hooksubscribers); |
||
| 114 | $amountOfAvailableSubscriberAreas = 0; |
||
| 115 | for ($i = 0; $i < $amountOfHookSubscribers; $i++) { |
||
| 116 | $hooksubscribers[$i] = $hooksubscribers[$i]->toArray(); |
||
| 117 | // don't allow subscriber and provider to be the same |
||
| 118 | // unless subscriber has the ability to connect to it's own providers |
||
| 119 | if ($hooksubscribers[$i]['name'] == $moduleName) { |
||
| 120 | unset($hooksubscribers[$i]); |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | // does the user have admin permissions on the subscriber module? |
||
| 124 | View Code Duplication | if (!$this->get('zikula_permissions_module.api.permission')->hasPermission($hooksubscribers[$i]['name'] . "::", '::', ACCESS_ADMIN)) { |
|
| 125 | unset($hooksubscribers[$i]); |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $metaData = $this->get('kernel')->getModule($hooksubscribers[$i]['name'])->getMetaData(); |
||
| 130 | $hooksubscriberVersionObj = $this->get('zikula_hook_bundle.api.hook')->getHookContainerInstance($metaData); |
||
| 131 | |||
| 132 | // get the areas of the subscriber |
||
| 133 | $hooksubscriberAreas = $this->get('hook_dispatcher')->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 hook provider from it's area |
||
| 181 | $sbaProviderModule = $this->get('hook_dispatcher')->getOwnerByArea($areaname); |
||
| 182 | |||
| 183 | $metaData = $this->get('kernel')->getModule($sbaProviderModule)->getMetaData(); |
||
| 184 | $sbaProviderModuleVersionObj = $this->get('zikula_hook_bundle.api.hook')->getHookContainerInstance($metaData); |
||
| 185 | |||
| 186 | // get the bundle title |
||
| 187 | if (isset($nonPersistedProviders[$areaname])) { |
||
| 188 | $currentSortingTitles[$areaname] = $nonPersistedProviders[$areaname]->getTitle(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | $templateParameters['areasSorting'] = $currentSorting; |
||
| 193 | $templateParameters['areasSortingTitles'] = $currentSortingTitles; |
||
| 194 | $templateParameters['total_attached_provider_areas'] = $amountOfAttachedProviderAreas; |
||
| 195 | |||
| 196 | // get available providers |
||
| 197 | /** @var ExtensionEntity[] $hookproviders */ |
||
| 198 | $hookproviders = $this->getExtensionsCapableOf(HookCollectorInterface::HOOK_PROVIDER); |
||
| 199 | $amountOfHookProviders = count($hookproviders); |
||
| 200 | $amountOfAvailableProviderAreas = 0; |
||
| 201 | for ($i = 0; $i < $amountOfHookProviders; $i++) { |
||
| 202 | $hookproviders[$i] = $hookproviders[$i]->toArray(); |
||
| 203 | // don't allow subscriber and provider to be the same |
||
| 204 | // unless subscriber has the ability to connect to it's own providers |
||
| 205 | if ($hookproviders[$i]['name'] == $moduleName && !$isSubscriberSelfCapable) { |
||
| 206 | unset($hookproviders[$i]); |
||
| 207 | continue; |
||
| 208 | } |
||
| 209 | |||
| 210 | // does the user have admin permissions on the provider module? |
||
| 211 | View Code Duplication | if (!$this->get('zikula_permissions_module.api.permission')->hasPermission($hookproviders[$i]['name']."::", '::', ACCESS_ADMIN)) { |
|
| 212 | unset($hookproviders[$i]); |
||
| 213 | continue; |
||
| 214 | } |
||
| 215 | |||
| 216 | $metaData = $this->get('kernel')->getModule($hookproviders[$i]['name'])->getMetaData(); |
||
| 217 | $hookproviderVersionObj = $this->get('zikula_hook_bundle.api.hook')->getHookContainerInstance($metaData); |
||
| 218 | |||
| 219 | // get the areas of the provider |
||
| 220 | $hookproviderAreas = $this->get('hook_dispatcher')->getProviderAreasByOwner($hookproviders[$i]['name']); |
||
| 221 | $hookproviders[$i]['areas'] = $hookproviderAreas; |
||
| 222 | $amountOfAvailableProviderAreas += count($hookproviderAreas); |
||
| 223 | |||
| 224 | $hookproviderAreasToTitles = []; // and get the titles |
||
| 225 | $hookproviderAreasToCategories = []; // and get the categories |
||
| 226 | $hookproviderAreasAndCategories = []; // and build array with category => areas |
||
| 227 | View Code Duplication | foreach ($hookproviderAreas as $hookproviderArea) { |
|
| 228 | if (isset($nonPersistedProviders[$hookproviderArea])) { |
||
| 229 | $hookproviderAreasToTitles[$hookproviderArea] = $nonPersistedProviders[$hookproviderArea]->getTitle(); |
||
| 230 | $category = $nonPersistedProviders[$hookproviderArea]->getCategory(); |
||
| 231 | } |
||
| 232 | $hookproviderAreasToCategories[$hookproviderArea] = $category; |
||
| 233 | $hookproviderAreasAndCategories[$category][] = $hookproviderArea; |
||
| 234 | } |
||
| 235 | $hookproviders[$i]['areasToTitles'] = $hookproviderAreasToTitles; |
||
| 236 | $hookproviders[$i]['areasToCategories'] = $hookproviderAreasToCategories; |
||
| 237 | $hookproviders[$i]['areasAndCategories'] = $hookproviderAreasAndCategories; |
||
| 238 | } |
||
| 239 | $templateParameters['hookproviders'] = $hookproviders; |
||
| 240 | $templateParameters['total_available_provider_areas'] = $amountOfAvailableProviderAreas; |
||
| 241 | } else { |
||
| 242 | $templateParameters['hookproviders'] = []; |
||
| 243 | } |
||
| 244 | $templateParameters['hookDispatcher'] = $this->get('hook_dispatcher'); |
||
| 245 | $request = $this->get('request_stack')->getCurrentRequest(); |
||
| 246 | $request->attributes->set('_zkModule', $moduleName); |
||
| 247 | $request->attributes->set('_zkType', 'admin'); |
||
| 248 | $request->attributes->set('_zkFunc', 'Hooks'); |
||
| 249 | |||
| 250 | return $templateParameters; |
||
| 251 | } |
||
| 252 | |||
| 430 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.