mysociety/popit-api

View on GitHub

Showing 97 of 97 total issues

Similar blocks of code found in 2 locations. Consider refactoring.
Open

    if (options.apiBaseUrl) {
      doc.set('url', [
        options.apiBaseUrl,
        doc.constructor.collection.name.toLowerCase(),
        encodeURIComponent(doc._id || doc.id),
Severity: Major
Found in src/transform.js and 1 other location - About 1 hr to fix
src/transform.js on lines 14..20

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 67.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Similar blocks of code found in 2 locations. Consider refactoring.
Open

    if (options.baseUrl && (doc._id || doc.id)) {
      doc.set('html_url', [
        options.baseUrl,
        doc.constructor.collection.name.toLowerCase(),
        encodeURIComponent(doc._id || doc.id),
Severity: Major
Found in src/transform.js and 1 other location - About 1 hr to fix
src/transform.js on lines 7..13

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 67.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Function merge has 40 lines of code (exceeds 25 allowed). Consider refactoring.
Open

  schema.methods.merge = function merge(other, callback) {
    var self = this;
    var conflicts = [];
    var pathsToMerge = Object.keys(other.toObject());
    _.each(pathsToMerge, function(path) {
Severity: Minor
Found in src/mongoose/merge.js - About 1 hr to fix

    Function paginate has 39 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    function paginate(options) {
      options = options || {};
      var page = parseInt(options.page, 10) || 1;
      var perPage = parseInt(options.per_page, 10) || 30;
    
    
    Severity: Minor
    Found in src/paginate.js - About 1 hr to fix

      Function exports has 39 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      module.exports = function(app) {
      
        app.get('/:collection/:id(*)/full', function (req, res, next) {
          var id = req.params.id;
      
      
      Severity: Minor
      Found in src/routes/1.0/document.get.js - About 1 hr to fix

        Function importer has 37 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function importer(connection, popoloObject, callback) {
          var collections = [
            ['persons', 'Person'],
            ['people', 'Person'],
            ['organizations', 'Organization'],
        Severity: Minor
        Found in src/importer.js - About 1 hr to fix

          Function checkMembership has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
          Open

          function checkMembership(req, membership, doc, done) {
            if ( req.model.modelName == 'Person' ) {
              if ( !membership.person_id ) {
                membership.person_id = doc.id;
              } else if ( membership.person_id != doc.id ) {
          Severity: Minor
          Found in src/inline-memberships.js - About 1 hr to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function paginate has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
          Open

          function paginate(options) {
            options = options || {};
            var page = parseInt(options.page, 10) || 1;
            var perPage = parseInt(options.per_page, 10) || 30;
          
          
          Severity: Minor
          Found in src/paginate.js - About 1 hr to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function exports has 34 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          module.exports = function(app) {
          
            app.get('/:collection/:id/full', function (req, res, next) {
              var id = req.params.id;
          
          
          Severity: Minor
          Found in src/routes/document.get.js - About 1 hr to fix

            Function reIndex has 34 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              schema.statics.reIndex = function(done) {
                var self = this;
                self.find(function(err, docs) {
                  if (err) {
                    return done(err);
            Severity: Minor
            Found in src/mongoose/elasticsearch.js - About 1 hr to fix

              Function hiddenFields has 33 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

              function hiddenFields(req, res, next) {
                var fields = {
                  all: {}
                };
              
              
              Severity: Minor
              Found in src/middleware/hidden-fields.js - About 1 hr to fix

                Function afterCreateMemberships has 33 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                          }, function afterCreateMemberships(err) {
                          if (err) {
                            tidyUpInlineMembershipError(req, null, req.created_memberships, req.updated_memberships, function(innerErr) {
                              if ( innerErr ) {
                                return res.send(400, {errors: [innerErr]});
                Severity: Minor
                Found in src/routes/1.0/document.put.js - About 1 hr to fix

                  Function callCreateMembership has 31 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      function callCreateMembership(membership, cb) {
                        if ( ! membership.id ) {
                          createMembership(req, membership, function(err, membership) {
                            if ( err ) {
                              return cb(err);
                  Severity: Minor
                  Found in src/inline-memberships.js - About 1 hr to fix

                    Function esFilterDatesOnly has 30 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                      function esFilterDatesOnly(doc, ret) {
                        var missing = '';
                        for (var field in ret) {
                          if ( ['start_date', 'birth_date', 'founding_date', 'created_at', 'updated_at', 'valid_from'].indexOf(field) != -1 ) {
                            if (!ret[field]) {
                    Severity: Minor
                    Found in src/esfilter.js - About 1 hr to fix

                      Function exports has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                      module.exports = function(app) {
                      
                        app.get('/search/:collection', function(req, res, next) {
                          var pagination = paginate(req.query);
                          req.collection.search(req.query, function(err, result) {
                      Severity: Minor
                      Found in src/routes/search.js - About 1 hr to fix

                        Function exports has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                        module.exports = function(app) {
                        
                          app.get('/search/:collection', function(req, res, next) {
                            var pagination = paginate(req.query);
                            req.collection.search(req.query, function(err, result) {
                        Severity: Minor
                        Found in src/routes/1.0/search.js - About 1 hr to fix

                          Function bulkReIndex has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                          Open

                            schema.statics.bulkReIndex = function(docs, done) {
                              var self = this;
                              var indexed = 0;
                              var body;
                              async.concatSeries(docs, function(doc, callback) {
                          Severity: Minor
                          Found in src/mongoose/elasticsearch.js - About 1 hr to fix

                            Function exports has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                            Open

                            module.exports = function(app) {
                            
                              app.get('/:collection', function (req, res, next) {
                                var pagination = paginate(req.query);
                                req.collection.find({}, null, pagination, function (err, docs) {
                            Severity: Minor
                            Found in src/routes/collection.get.js - About 1 hr to fix

                              Function popitApiApp has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                              Open

                              function popitApiApp(options) {
                                var app = express();
                              
                                require('../app-setup')(app, options);
                              
                              
                              Severity: Minor
                              Found in src/versions/0.1.js - About 1 hr to fix

                                Function removeOldMemberships has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                                Open

                                function removeOldMemberships(req, memberships, key, id, done) {
                                  var membership_ids =
                                    _.chain(memberships)
                                     .map( function(membership) { return membership.id; })
                                     .compact()
                                Severity: Minor
                                Found in src/inline-memberships.js - About 1 hr to fix
                                  Severity
                                  Category
                                  Status
                                  Source
                                  Language