| Conditions | 14 |
| Total Lines | 61 |
| Code Lines | 47 |
| 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:
Complex classes like tabs.init.ts ➔ createRouteForTabItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /** |
||
| 56 | |||
| 57 | /* istanbul ignore next */ |
||
| 58 | function createRouteForTabItem(to: Route, router: Router, next: () => void): boolean { |
||
| 59 | /** |
||
| 60 | * Create new route for the url if it matches a tab extension |
||
| 61 | */ |
||
| 62 | let matchingTabItemConfig: undefined | TabItemEntry; |
||
| 63 | |||
| 64 | Object.values(Shopware.State.get('tabs').tabItems).find((tabItemConfigs) => { |
||
| 65 | const _matchingTabItemConfig = tabItemConfigs.find((tabItemConfig) => { |
||
| 66 | return to.fullPath.endsWith(tabItemConfig.componentSectionId); |
||
| 67 | }); |
||
| 68 | |||
| 69 | if (_matchingTabItemConfig) { |
||
| 70 | matchingTabItemConfig = _matchingTabItemConfig; |
||
| 71 | return true; |
||
| 72 | } |
||
| 73 | |||
| 74 | return false; |
||
| 75 | }); |
||
| 76 | |||
| 77 | if (!matchingTabItemConfig) { |
||
| 78 | next(); |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | const dynamicPath = getDynamicPath(to.fullPath, router); |
||
| 83 | const parentRoute = getParentRoute(dynamicPath, router as Router & { options?: { routes: RouteConfig[] } }); |
||
| 84 | |||
| 85 | if (parentRoute && parentRoute?.children === undefined) { |
||
| 86 | parentRoute.children = []; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (parentRoute && parentRoute.children) { |
||
| 90 | const firstChild = parentRoute.children[0]; |
||
| 91 | const newRouteName = `${parentRoute.name ?? ''}.${matchingTabItemConfig.componentSectionId}`; |
||
| 92 | |||
| 93 | const routeAlreadyExists = router.match({ |
||
| 94 | name: newRouteName, |
||
| 95 | }).matched.some((route) => route.name === newRouteName); |
||
| 96 | |||
| 97 | if (!routeAlreadyExists) { |
||
| 98 | router.addRoute(parentRoute.name, { |
||
| 99 | path: dynamicPath, |
||
| 100 | // @ts-expect-error |
||
| 101 | component: Shopware.Application.view.getComponent('sw-extension-component-section'), |
||
| 102 | name: newRouteName, |
||
| 103 | meta: { |
||
| 104 | // eslint-disable-next-line max-len |
||
| 105 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment |
||
| 106 | parentPath: firstChild?.meta?.parentPath ?? '', |
||
| 107 | }, |
||
| 108 | isChildren: true, |
||
| 109 | props: { |
||
| 110 | 'position-identifier': matchingTabItemConfig.componentSectionId, |
||
| 111 | }, |
||
| 112 | }); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return !!parentRoute?.children; |
||
| 117 | } |
||
| 175 |