NodeBB/NodeBB

View on GitHub
src/controllers/topics.js

Summary

Maintainability
C
1 day
Test Coverage

Function getTopic has a Cognitive Complexity of 32 (exceeds 10 allowed). Consider refactoring.
Open

topicsController.get = async function getTopic(req, res, next) {
    const tid = req.params.topic_id;
    if (
        (req.params.post_index && !utils.isNumber(req.params.post_index) && req.params.post_index !== 'unread') ||
        !utils.isNumber(tid)
Severity: Minor
Found in src/controllers/topics.js - About 3 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 getTopic has 99 lines of code (exceeds 75 allowed). Consider refactoring.
Open

topicsController.get = async function getTopic(req, res, next) {
    const tid = req.params.topic_id;
    if (
        (req.params.post_index && !utils.isNumber(req.params.post_index) && req.params.post_index !== 'unread') ||
        !utils.isNumber(tid)
Severity: Major
Found in src/controllers/topics.js - About 1 hr to fix

    Function addTags has 80 lines of code (exceeds 75 allowed). Consider refactoring.
    Open

    async function addTags(topicData, req, res, currentPage) {
        const postIndex = parseInt(req.params.post_index, 10) || 0;
        const postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10));
        let description = '';
        if (postAtIndex && postAtIndex.content) {
    Severity: Major
    Found in src/controllers/topics.js - About 1 hr to fix

      Avoid too many return statements within this function.
      Open

              return helpers.redirect(res, `/topic/${tid}/${req.params.slug}${postIndex > topicData.postcount ? `/${topicData.postcount}` : ''}${generateQueryString(req.query)}`);
      Severity: Major
      Found in src/controllers/topics.js - About 30 mins to fix

        Function addTags has a Cognitive Complexity of 11 (exceeds 10 allowed). Consider refactoring.
        Open

        async function addTags(topicData, req, res, currentPage) {
            const postIndex = parseInt(req.params.post_index, 10) || 0;
            const postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10));
            let description = '';
            if (postAtIndex && postAtIndex.content) {
        Severity: Minor
        Found in src/controllers/topics.js - About 25 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