yiisoft/yii2

View on GitHub
framework/helpers/BaseStringHelper.php

Summary

Maintainability
B
4 hrs
Test Coverage

Function truncateHtml has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring.
Open

    protected static function truncateHtml($string, $count, $suffix, $encoding = false)
    {
        $config = \HTMLPurifier_Config::create(null);
        if (Yii::$app !== null) {
            $config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());
Severity: Minor
Found in framework/helpers/BaseStringHelper.php - 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

The class BaseStringHelper has 17 public methods. Consider refactoring BaseStringHelper to keep number of public methods under 10.
Open

class BaseStringHelper
{
    /**
     * Returns the number of bytes in the given string.
     * This method ensures the string is treated as a byte array by using `mb_strlen()`.

TooManyPublicMethods

Since: 0.1

A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

By default it ignores methods starting with 'get' or 'set'.

Example

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

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

class BaseStringHelper
{
    /**
     * Returns the number of bytes in the given string.
     * This method ensures the string is treated as a byte array by using `mb_strlen()`.

Method truncate has 5 arguments (exceeds 4 allowed). Consider refactoring.
Open

    public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
Severity: Minor
Found in framework/helpers/BaseStringHelper.php - About 35 mins to fix

    Function explode has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

        public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
        {
            $result = explode($delimiter, $string);
            if ($trim !== false) {
                if ($trim === true) {
    Severity: Minor
    Found in framework/helpers/BaseStringHelper.php - 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

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

        protected static function truncateHtml($string, $count, $suffix, $encoding = false)
        {
            $config = \HTMLPurifier_Config::create(null);
            if (Yii::$app !== null) {
                $config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());

    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

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

        public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)

    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

    Missing class import via use statement (line '180', column '62').
    Open

            $tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());

    MissingImport

    Since: 2.7.0

    Importing all external classes in a file through use statements makes them clearly visible.

    Example

    function make() {
        return new \stdClass();
    }

    Source http://phpmd.org/rules/cleancode.html#MissingImport

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

        public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)

    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 endsWith has a boolean flag argument $caseSensitive, which is a certain sign of a Single Responsibility Principle violation.
    Open

        public static function endsWith($string, $with, $caseSensitive = true)

    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

    Missing class import via use statement (line '221', column '26').
    Open

            $generator = new \HTMLPurifier_Generator($config, $context);

    MissingImport

    Since: 2.7.0

    Importing all external classes in a file through use statements makes them clearly visible.

    Example

    function make() {
        return new \stdClass();
    }

    Source http://phpmd.org/rules/cleancode.html#MissingImport

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

        public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)

    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

    Missing class import via use statement (line '214', column '44').
    Open

                            $truncated[] = new \HTMLPurifier_Token_End($name);

    MissingImport

    Since: 2.7.0

    Importing all external classes in a file through use statements makes them clearly visible.

    Example

    function make() {
        return new \stdClass();
    }

    Source http://phpmd.org/rules/cleancode.html#MissingImport

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

        public static function startsWith($string, $with, $caseSensitive = true)

    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 explode has a boolean flag argument $skipEmpty, which is a certain sign of a Single Responsibility Principle violation.
    Open

        public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)

    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

    Missing class import via use statement (line '220', column '24').
    Open

            $context = new \HTMLPurifier_Context();

    MissingImport

    Since: 2.7.0

    Importing all external classes in a file through use statements makes them clearly visible.

    Example

    function make() {
        return new \stdClass();
    }

    Source http://phpmd.org/rules/cleancode.html#MissingImport

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

        protected static function truncateHtml($string, $count, $suffix, $encoding = false)

    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

    Avoid assigning values to variables in if clauses and the like (line '260', column '14').
    Open

        public static function endsWith($string, $with, $caseSensitive = true)
        {
            if (!$bytes = static::byteLength($with)) {
                return true;
            }

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

    Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

    Avoid assigning values to variables in if clauses and the like (line '236', column '14').
    Open

        public static function startsWith($string, $with, $caseSensitive = true)
        {
            if (!$bytes = static::byteLength($with)) {
                return true;
            }

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

    Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

    The method mb_ucfirst is not named in camelCase.
    Open

        public static function mb_ucfirst($string, $encoding = 'UTF-8')
        {
            $firstChar = mb_substr((string)$string, 0, 1, $encoding);
            $rest = mb_substr((string)$string, 1, null, $encoding);
    
    

    CamelCaseMethodName

    Since: 0.2

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

    Example

    class ClassName {
        public function get_name() {
        }
    }

    Source

    The method mb_ucwords is not named in camelCase.
    Open

        public static function mb_ucwords($string, $encoding = 'UTF-8')
        {
            $string = (string) $string;
            if (empty($string)) {
                return $string;

    CamelCaseMethodName

    Since: 0.2

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

    Example

    class ClassName {
        public function get_name() {
        }
    }

    Source

    There are no issues that match your filters.

    Category
    Status