| Conditions | 15 |
| Paths | > 20000 |
| Total Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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:
Complex classes like plugins.js ➔ ... ➔ ??? 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 | import path from 'path' |
||
| 24 | Array.prototype.forEach.call(this._plugins, (plugin) => { |
||
| 25 | // has hooks |
||
| 26 | var plugHooks = path.join(plugin.path, config.hooks.url) |
||
| 27 | try { |
||
| 28 | var directoryHook = fse.lstatSync(plugHooks) |
||
| 29 | if (directoryHook.isDirectory()) { |
||
| 30 | var plugHooksFile = path.join(plugHooks, 'hooks.js') |
||
| 31 | var h = require(plugHooksFile) |
||
| 32 | plugin.hooks = h.default |
||
| 33 | }else { |
||
| 34 | plugin.hooks = null |
||
| 35 | } |
||
| 36 | }catch(e) { |
||
| 37 | plugin.hooks = null |
||
| 38 | } |
||
| 39 | |||
| 40 | // has partials |
||
| 41 | var plugPartials = path.join(plugin.path, config.pluginsPartials) |
||
| 42 | try { |
||
| 43 | var directoryPartials = fse.lstatSync(plugPartials) |
||
| 44 | if (directoryPartials.isDirectory()) { |
||
| 45 | plugin.partials = plugPartials |
||
| 46 | }else { |
||
| 47 | plugin.partials = null |
||
| 48 | } |
||
| 49 | }catch(e) { |
||
| 50 | plugin.partials = null |
||
| 51 | } |
||
| 52 | |||
| 53 | // has templates |
||
| 54 | var plugTemplates = path.join(plugin.path, config.templates.url) |
||
| 55 | try { |
||
| 56 | var directoryTemplates = fse.lstatSync(plugTemplates) |
||
| 57 | if (directoryTemplates.isDirectory()) { |
||
| 58 | plugin.templates = plugTemplates |
||
| 59 | }else { |
||
| 60 | plugin.templates = null |
||
| 61 | } |
||
| 62 | }catch(e) { |
||
| 63 | plugin.templates = null |
||
| 64 | } |
||
| 65 | |||
| 66 | // has process |
||
| 67 | var plugProcess = path.join(plugin.path, 'process') |
||
| 68 | try { |
||
| 69 | var directoryProcess = fse.lstatSync(plugProcess) |
||
| 70 | if (directoryProcess.isDirectory()) { |
||
| 71 | plugin.process = {} |
||
| 72 | var processFiles = cmsData.file.getFiles(plugProcess, true, 0) |
||
| 73 | Array.prototype.forEach.call(processFiles, (processFile) => { |
||
| 74 | plugin.process[processFile.cleanNameNoExt] = processFile.path |
||
| 75 | }) |
||
| 76 | }else { |
||
| 77 | plugin.process = null |
||
| 78 | } |
||
| 79 | }catch(e) { |
||
| 80 | plugin.process = null |
||
| 81 | } |
||
| 82 | |||
| 83 | // has routes |
||
| 84 | var plugRoutes = path.join(plugin.path, 'routes') |
||
| 85 | try { |
||
| 86 | var directoryRoute = fse.lstatSync(plugRoutes) |
||
| 87 | if (directoryRoute.isDirectory()) { |
||
| 88 | plugin.routes = {} |
||
| 89 | |||
| 90 | var gets = path.join(plugRoutes, 'get') |
||
| 91 | try { |
||
| 92 | var directoryGets = fse.lstatSync(gets) |
||
| 93 | if (directoryGets.isDirectory()) { |
||
| 94 | var routesGet = cmsData.file.getFiles(gets, true, 0) |
||
| 95 | Array.prototype.forEach.call(routesGet, (route) => { |
||
| 96 | route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*` |
||
| 97 | }) |
||
| 98 | plugin.routes.get = routesGet |
||
| 99 | } |
||
| 100 | }catch(e) { |
||
| 101 | |||
| 102 | } |
||
| 103 | try { |
||
| 104 | var posts = path.join(plugRoutes, 'post') |
||
| 105 | var directoryPosts = fse.lstatSync(gets) |
||
| 106 | if (directoryPosts.isDirectory()) { |
||
| 107 | var routesPost = cmsData.file.getFiles(posts, true, 0) |
||
| 108 | Array.prototype.forEach.call(routesPost, (route) => { |
||
| 109 | route.routePath = `/abe/plugin/${plugin.name}/${route.name.replace('.js', '')}*` |
||
| 110 | }) |
||
| 111 | plugin.routes.post = routesPost |
||
| 112 | } |
||
| 113 | }catch(e) { |
||
| 114 | |||
| 115 | } |
||
| 116 | }else { |
||
| 117 | plugin.routes = null |
||
| 118 | } |
||
| 119 | }catch(e) { |
||
| 120 | plugin.routes = null |
||
| 121 | } |
||
| 122 | }) |
||
| 123 | |||
| 197 | export default Plugins |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.