attogram/justrefs

View on GitHub
src/Topic.php

Summary

Maintainability
C
1 day
Test Coverage

The class Topic has an overall complexity of 69 which is very high. The configured complexity threshold is 50.
Open

class Topic extends Base
{
    const ERROR_NOT_FOUND  = 'Topic Not Found';

    /** @var array $data - topic data */
Severity: Minor
Found in src/Topic.php by phpmd

Function setNamespaces has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Open

    private function setNamespaces()
    {
        foreach ($this->data[self::TOPICS] as $topic) {
            if (!isset($topic[self::EXISTS])) {
                $this->vars[self::MISSING][] = $topic[self::ASTERISK]; // page does not exist
Severity: Minor
Found in src/Topic.php - About 2 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

Method setNamespaces has 57 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    private function setNamespaces()
    {
        foreach ($this->data[self::TOPICS] as $topic) {
            if (!isset($topic[self::EXISTS])) {
                $this->vars[self::MISSING][] = $topic[self::ASTERISK]; // page does not exist
Severity: Major
Found in src/Topic.php - About 2 hrs to fix

    File Topic.php has 259 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    <?php
    /**
     * Just Refs - https://github.com/attogram/justrefs
     * Topic Class
     */
    Severity: Minor
    Found in src/Topic.php - About 2 hrs to fix

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

          private function listify($index)
          {
              if (in_array($index, [self::EXISTS, self::MISSING]) // skip internal-usage vars
                  || empty($this->vars[$index]) // Error - index not found, or index empty
              ) {
      Severity: Minor
      Found in src/Topic.php - 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 removeTemplateTopics has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
      Open

          private function removeTemplateTopics()
          {
              if (empty($this->vars[self::MAIN])
                  || (empty($this->vars[self::TEMPLATE]) && $this->vars[self::TEMPLATE_SECONDARY])
              ) {
      Severity: Minor
      Found in src/Topic.php - 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 setTemplates has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
      Open

          private function setTemplates()
          {
              foreach ($this->data[self::TEMPLATES] as $item) {
                  switch ($item[self::NS]) {
                      case '0': // Main
      Severity: Minor
      Found in src/Topic.php - 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 get has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

          public function get()
          {
              if (!$this->setTopicFromUrl()) { // build topic from URL
                  $this->error404(self::ERROR_NOT_FOUND);
              }
      Severity: Minor
      Found in src/Topic.php - 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

      The method setNamespaces() has a Cyclomatic Complexity of 19. The configured cyclomatic complexity threshold is 10.
      Open

          private function setNamespaces()
          {
              foreach ($this->data[self::TOPICS] as $topic) {
                  if (!isset($topic[self::EXISTS])) {
                      $this->vars[self::MISSING][] = $topic[self::ASTERISK]; // page does not exist
      Severity: Minor
      Found in src/Topic.php by phpmd

      CyclomaticComplexity

      Since: 0.1

      Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

      Example

      // Cyclomatic Complexity = 11
      class Foo {
      1   public function example() {
      2       if ($a == $b) {
      3           if ($a1 == $b1) {
                      fiddle();
      4           } elseif ($a2 == $b2) {
                      fiddle();
                  } else {
                      fiddle();
                  }
      5       } elseif ($c == $d) {
      6           while ($c == $d) {
                      fiddle();
                  }
      7        } elseif ($e == $f) {
      8           for ($n = 0; $n < $h; $n++) {
                      fiddle();
                  }
              } else {
                  switch ($z) {
      9               case 1:
                          fiddle();
                          break;
      10              case 2:
                          fiddle();
                          break;
      11              case 3:
                          fiddle();
                          break;
                      default:
                          fiddle();
                          break;
                  }
              }
          }
      }

      Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

      Reduce the number of returns of this function 4, down to the maximum allowed 3.
      Open

          public function get()
      Severity: Major
      Found in src/Topic.php by sonar-php

      Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.

      Noncompliant Code Example

      With the default threshold of 3:

      function myFunction(){ // Noncompliant as there are 4 return statements
        if (condition1) {
          return true;
        } else {
          if (condition2) {
            return false;
          } else {
            return true;
          }
        }
        return false;
      }
      

      TODO found
      Open

              $this->data = json_decode($this->data, true); // @TODO catch errors from decode
      Severity: Minor
      Found in src/Topic.php by fixme

      Define a constant instead of duplicating this literal "cached" 4 times.
      Open

              if (!isset($this->data['cached'])) {  // if no cache time is set...
      Severity: Critical
      Found in src/Topic.php by sonar-php

      Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

      On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

      Noncompliant Code Example

      With the default threshold of 3:

      function run() {
        prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
        execute('action1');
        release('action1');
      }
      

      Compliant Solution

      ACTION_1 = 'action1';
      
      function run() {
        prepare(ACTION_1);
        execute(ACTION_1);
        release(ACTION_1);
      }
      

      Exceptions

      To prevent generating some false-positives, literals having less than 5 characters are excluded.

      There are no issues that match your filters.

      Category
      Status