Formula9/Framework

View on GitHub
Nine/Library/Strings.php

Summary

Maintainability
B
5 hrs
Test Coverage

Method encode_readable_json has 30 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public static function encode_readable_json($data, $indent = 0)
    {
        $_escape = function ($str) {
            return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
        };
Severity: Minor
Found in Nine/Library/Strings.php - About 1 hr to fix

    Function encode_readable_json has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        public static function encode_readable_json($data, $indent = 0)
        {
            $_escape = function ($str) {
                return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
            };
    Severity: Minor
    Found in Nine/Library/Strings.php - About 55 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

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

        public static function var_to_string($var)
        {
            if (is_object($var)) {
                return sprintf('Object(%s)', get_class($var));
            }
    Severity: Minor
    Found in Nine/Library/Strings.php - 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 too many return statements within this method.
    Open

            return (string) $var;
    Severity: Major
    Found in Nine/Library/Strings.php - About 30 mins to fix

      Avoid too many return statements within this method.
      Open

                  return 'true';
      Severity: Major
      Found in Nine/Library/Strings.php - About 30 mins to fix

        Avoid too many return statements within this method.
        Open

                    return 'false';
        Severity: Major
        Found in Nine/Library/Strings.php - About 30 mins to fix

          The method hsc has a boolean flag argument $double_encode, which is a certain sign of a Single Responsibility Principle violation.
          Open

              public static function hsc($string, $double_encode = TRUE)
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          BooleanArgumentFlag

          Since: 1.4.0

          A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

          Example

          class Foo {
              public function bar($flag = true) {
              }
          }

          Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

          The method snake_to_camel has a boolean flag argument $asIdentifier, which is a certain sign of a Single Responsibility Principle violation.
          Open

              public static function snake_to_camel($string, $asIdentifier = FALSE)
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          BooleanArgumentFlag

          Since: 1.4.0

          A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

          Example

          class Foo {
              public function bar($flag = true) {
              }
          }

          Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

          The method encode_readable_json uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
          Open

                      else {
                          $out .= $value;
                      }
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          ElseExpression

          Since: 1.4.0

          An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

          Example

          class Foo
          {
              public function bar($flag)
              {
                  if ($flag) {
                      // one branch
                  } else {
                      // another branch
                  }
              }
          }

          Source https://phpmd.org/rules/cleancode.html#elseexpression

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

              public static function str_matches($pattern, $value)
              {
                  if ($pattern === $value) {
                      return TRUE;
                  }
          Severity: Minor
          Found in Nine/Library/Strings.php and 1 other location - About 30 mins to fix
          Nine/Library/Strings.php on lines 168..182

          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 91.

          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

              public static function pattern_matches($value, $pattern)
              {
                  if ($pattern === $value) {
                      return TRUE;
                  }
          Severity: Minor
          Found in Nine/Library/Strings.php and 1 other location - About 30 mins to fix
          Nine/Library/Strings.php on lines 303..317

          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 91.

          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

          The parameter $camel_case_string is not named in camelCase.
          Open

              public static function camel_to_snake($camel_case_string, $delimiter = '_')
              {
                  preg_match_all(
                      '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!',
                      $camel_case_string,
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseParameterName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name parameters.

          Example

          class ClassName {
              public function doSomething($user_name) {
              }
          }

          Source

          Avoid variables with short names like $a. Configured minimum length is 3.
          Open

                      $a = [];
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          ShortVariable

          Since: 0.2

          Detects when a field, local, or parameter has a very short name.

          Example

          class Something {
              private $q = 15; // VIOLATION - Field
              public static function main( array $as ) { // VIOLATION - Formal
                  $r = 20 + $this->q; // VIOLATION - Local
                  for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                      $r += $this->q;
                  }
              }
          }

          Source https://phpmd.org/rules/naming.html#shortvariable

          The parameter $double_encode is not named in camelCase.
          Open

              public static function hsc($string, $double_encode = TRUE)
              {
                  return htmlspecialchars($string, ENT_COMPAT | ENT_HTML5, 'UTF-8', $double_encode);
              }
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseParameterName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name parameters.

          Example

          class ClassName {
              public function doSomething($user_name) {
              }
          }

          Source

          The variable $_escape is not named in camelCase.
          Open

              public static function encode_readable_json($data, $indent = 0)
              {
                  $_escape = function ($str) {
                      return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
                  };
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseVariableName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name variables.

          Example

          class ClassName {
              public function doSomething() {
                  $data_module = new DataModule();
              }
          }

          Source

          The variable $_escape is not named in camelCase.
          Open

              public static function encode_readable_json($data, $indent = 0)
              {
                  $_escape = function ($str) {
                      return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
                  };
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseVariableName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name variables.

          Example

          class ClassName {
              public function doSomething() {
                  $data_module = new DataModule();
              }
          }

          Source

          The variable $camel_case_string is not named in camelCase.
          Open

              public static function camel_to_snake($camel_case_string, $delimiter = '_')
              {
                  preg_match_all(
                      '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!',
                      $camel_case_string,
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseVariableName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name variables.

          Example

          class ClassName {
              public function doSomething() {
                  $data_module = new DataModule();
              }
          }

          Source

          The variable $double_encode is not named in camelCase.
          Open

              public static function hsc($string, $double_encode = TRUE)
              {
                  return htmlspecialchars($string, ENT_COMPAT | ENT_HTML5, 'UTF-8', $double_encode);
              }
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseVariableName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name variables.

          Example

          class ClassName {
              public function doSomething() {
                  $data_module = new DataModule();
              }
          }

          Source

          The variable $_escape is not named in camelCase.
          Open

              public static function encode_readable_json($data, $indent = 0)
              {
                  $_escape = function ($str) {
                      return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
                  };
          Severity: Minor
          Found in Nine/Library/Strings.php by phpmd

          CamelCaseVariableName

          Since: 0.2

          It is considered best practice to use the camelCase notation to name variables.

          Example

          class ClassName {
              public function doSomething() {
                  $data_module = new DataModule();
              }
          }

          Source

          There are no issues that match your filters.

          Category
          Status