Completed
Push — master ( 922ed9...2115c9 )
by
unknown
02:10
created

es   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
c 2
b 0
f 0
nc 13
dl 0
loc 48
rs 5.1266
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A abe-get-select-template-keys.js ➔ ... ➔ where.right.value.forEach 0 5 2

How to fix   Complexity   

Complexity

Complex classes like abe-get-select-template-keys.js ➔ recurseWhereVariables 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'
2
import fse from 'fs-extra'
3
import {execFile} from 'child_process'
4
import {
5
  fileUtils,
0 ignored issues
show
Unused Code introduced by
The variable fileUtils seems to be never used. Consider removing it.
Loading history...
6
  FileParser,
0 ignored issues
show
Unused Code introduced by
The variable FileParser seems to be never used. Consider removing it.
Loading history...
7
  Util,
8
  Sql,
9
  cleanSlug,
0 ignored issues
show
Unused Code introduced by
The variable cleanSlug seems to be never used. Consider removing it.
Loading history...
10
  getTemplate,
0 ignored issues
show
Unused Code introduced by
The variable getTemplate seems to be never used. Consider removing it.
Loading history...
11
  save,
0 ignored issues
show
Unused Code introduced by
The variable save seems to be never used. Consider removing it.
Loading history...
12
  config,
13
  Hooks,
0 ignored issues
show
Unused Code introduced by
The variable Hooks seems to be never used. Consider removing it.
Loading history...
14
  Manager
0 ignored issues
show
Unused Code introduced by
The variable Manager seems to be never used. Consider removing it.
Loading history...
15
} from '../../'
16
17
var traverseFileSystem = function (currentPath, arr) {
0 ignored issues
show
Unused Code introduced by
The parameter arr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
18
  var res = []
19
  var files = fse.readdirSync(currentPath)
20
  for (var i in files) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
21
    var currentFile = currentPath + '/' + files[i]
22
    var stats = fse.statSync(currentFile)
23
    if (stats.isFile()) {
24
      if (currentFile.indexOf(config.files.templates.extension) > -1) {
25
        res.push(currentFile)
26
      }
27
    }
28
    else if (stats.isDirectory()) {
29
      res = res.concat(traverseFileSystem(currentFile))
30
    }
31
  }
32
  return res
33
}
34
35
var findTemplates = function(templatesPath) {
36
  var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
    let templatesList = traverseFileSystem(templatesPath)
38
    resolve(templatesList)
39
  })
40
41
  return p
42
}
43
44
/**
45
 * Get columns and where.left ids of a select statement
46
 *
47
 * select title, image from ../ where template=""
48
 *
49
 * return [title, image, template]
50
 * 
51
 * @param  {Array} templatesList ["article.html", "other.html"]
52
 * @return {Promise}
53
 */
54
55
var recurseWhereVariables = function (where) {
56
  var ar = []
57
  switch(where.operator) {
58
  case 'AND':
59
    var arLeft = recurseWhereVariables(where.left)
60
    var arRight = recurseWhereVariables(where.right)
61
    return arLeft.concat(arRight)
62
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
63
  case 'OR':
64
    var arLeft = recurseWhereVariables(where.left)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable arLeft already seems to be declared on line 59. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
65
    var arRight = recurseWhereVariables(where.right)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable arRight already seems to be declared on line 60. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
66
    return arLeft.concat(arRight)
67
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
68
  case 'IN':
69
    break
70
  case 'NOT IN':
71
    if(where.left.column.indexOf('{{') > -1) {
72
      ar.push(where.left.column.replace(/\{\{(.*?)\}\}/, '$1'))
73
    }
74
    else{
75
      ar.push(where.left.column)
76
    }
77
78
    where.right.value.forEach(function (value) {
79
      if(value.column.indexOf('{{') > -1) {
80
        ar.push(value.column.replace(/\{\{(.*?)\}\}/, '$1'))
81
      }
82
    })
83
    
84
    break
85
  default:
86
    if(where.left.column.indexOf('{{') > -1) {
87
      ar.push(where.left.column.replace(/\{\{(.*?)\}\}/, '$1'))
88
    }
89
    else{
90
      ar.push(where.left.column)
91
    }
92
    if(where.right.value && where.right.value.indexOf('{{') > -1) {
93
      ar.push(where.right.value.replace(/\{\{(.*?)\}\}/, '$1'))
94
    }
95
    if(where.right.column && where.right.column.indexOf('{{') > -1) {
96
      ar.push(where.right.column.replace(/\{\{(.*?)\}\}/, '$1'))
97
    }
98
    break
99
  }
100
101
  return ar
102
}
103
104
var findRequestColumns = function(templatesList) {
105
  var whereKeysCheck = {}
0 ignored issues
show
Unused Code introduced by
The variable whereKeysCheck seems to be never used. Consider removing it.
Loading history...
106
  var whereKeys = []
107
  var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
108
    let util = new Util()
109
    Array.prototype.forEach.call(templatesList, (file) => {
110
      var template = fse.readFileSync(file, 'utf8')
111
      var matches = util.dataRequest(template)
112
113
      Array.prototype.forEach.call(matches, (match) => {
114
        var obj = Util.getAllAttributes(match[0], {})
115
        var type = Sql.getSourceType(obj.sourceString)
116
117
        switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
118
        case 'request':
119
          var request = Sql.handleSqlRequest(obj.sourceString, {})
120
          if(typeof request.columns !== 'undefined' && request.columns !== null) {
121
            Array.prototype.forEach.call(request.columns, (column) => {
122
              whereKeys.push(column)
123
            })
124
          }
125
          if(typeof request.where !== 'undefined' && request.where !== null) {
126
            whereKeys = whereKeys.concat(recurseWhereVariables(request.where))
127
          }
128
        }
129
      })
130
    })
131
    whereKeys = whereKeys.filter(function (item, pos) {return whereKeys.indexOf(item) == pos})
132
    resolve(whereKeys)
133
  })
134
135
  return p
136
}
137
138
var getSelectTemplateKeys = function(templatesPath) {
139
  var p = new Promise((resolve, reject) => {
140
    findTemplates(templatesPath)
141
      .then((templatesList) => {
142
143
        findRequestColumns(templatesList)
144
          .then((whereKeys) => {
145
            resolve(whereKeys)
146
          },
147
          () => {
148
            console.log('findRequestColumns reject')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
149
            reject()
150
          })
151
          .catch((e) => {
152
            console.error('getSelectTemplateKeys', e)
153
            reject()
154
          })
155
      },
156
      () => {
157
        console.log('findTemplates reject')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
158
        reject()
159
      })
160
      .catch((e) => {
161
        console.error('getSelectTemplateKeys', e)
162
        reject()
163
      })
164
165
  })
166
167
  return p
168
}
169
170
export default getSelectTemplateKeys