BigstickCarpet/swagger-express-middleware

View on GitHub
lib/mock/index.js

Summary

Maintainability
D
2 days
Test Coverage

Function mock has a Cognitive Complexity of 45 (exceeds 5 allowed). Consider refactoring.
Open

function mock (context, router, dataStore) {
  router = router || context.router;
  dataStore = (dataStore instanceof DataStore) ? dataStore : new MemoryDataStore();
  let isDisabled = _.noop;

Severity: Minor
Found in lib/mock/index.js - About 6 hrs 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 mock has 72 lines of code (exceeds 25 allowed). Consider refactoring.
Open

function mock (context, router, dataStore) {
  router = router || context.router;
  dataStore = (dataStore instanceof DataStore) ? dataStore : new MemoryDataStore();
  let isDisabled = _.noop;

Severity: Major
Found in lib/mock/index.js - About 2 hrs to fix

    Function mockResponseHeaders has 45 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    function mockResponseHeaders (req, res, next) {
      if (res.swagger) {
        util.debug("Setting %d response headers...", _.keys(res.swagger.headers).length);
    
        if (res.swagger.headers) {
    Severity: Minor
    Found in lib/mock/index.js - About 1 hr to fix

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

        function mockResponse (req, res, next) {
          if (util.isSwaggerRequest(req) && !isDisabled()) {
            let response;
      
            // Is there already a statusCode? (perhaps set by third-party middleware)
      Severity: Minor
      Found in lib/mock/index.js - About 1 hr to fix

        Function sendFile has 32 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function sendFile (req, res, next, file) {
          if (file instanceof Buffer) {
            setContentType(req, res, ["application/octet-stream", "*/*"]);
        
            // `file` is the file's contents
        Severity: Minor
        Found in lib/mock/index.js - About 1 hr to fix

          Avoid deeply nested control flow statements.
          Open

                      if (req.method === "POST" || req.method === "PUT") {
                        res.status(201);
                      }
                      else if (req.method === "DELETE" && !responses[0].api.schema) {
                        res.status(204);
          Severity: Major
          Found in lib/mock/index.js - About 45 mins to fix

            Avoid deeply nested control flow statements.
            Open

                          if (req.cookies.swagger === undefined) {
                            res.cookie("swagger", _.uniqueId("random") + _.random(99999999999.9999));
                          }
                          else {
                            res.cookie("swagger", req.cookies.swagger);
            Severity: Major
            Found in lib/mock/index.js - About 45 mins to fix

              Function mockResponseBody has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

              function mockResponseBody (req, res, next) {
                if (res.swagger) {
                  if (res.swagger.isEmpty) {
                    // There is no response schema, so send an empty response
                    util.debug("%s %s does not have a response schema. Sending an empty response", req.method, req.path);
              Severity: Minor
              Found in lib/mock/index.js - About 45 mins 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

              Avoid deeply nested control flow statements.
              Open

                            if (_.isDate(sample)) {
                              res.set(name, util.rfc1123(sample));
                            }
                            else {
                              res.set(name, _(sample).toString());
              Severity: Major
              Found in lib/mock/index.js - About 45 mins to fix

                Function setContentType has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                function setContentType (req, res, supported, excluded) {
                  // Get the MIME types that this operation produces
                  let produces = req.swagger.operation.produces || req.swagger.api.produces || [];
                
                  if (produces.length === 0) {
                Severity: Minor
                Found in lib/mock/index.js - About 35 mins 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

                There are no issues that match your filters.

                Category
                Status