YetiForceCompany/YetiForceCRM

View on GitHub
app/Log/Profiling.php

Summary

Maintainability
A
3 hrs
Test Coverage
F
0%

Function export has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

    public function export()
    {
        $timings = [];
        $stack = [];
        foreach ($this->messages as $i => $log) {
Severity: Minor
Found in app/Log/Profiling.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 export has 41 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function export()
    {
        $timings = [];
        $stack = [];
        foreach ($this->messages as $i => $log) {
Severity: Minor
Found in app/Log/Profiling.php - About 1 hr to fix

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

        public function export()
        {
            $timings = [];
            $stack = [];
            foreach ($this->messages as $i => $log) {
    Severity: Minor
    Found in app/Log/Profiling.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

    Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.
    Open

        public function export()
    Severity: Critical
    Found in app/Log/Profiling.php by sonar-php

    Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

    See

    Missing class import via use statement (line '68', column '17').
    Open

            $logID = (new \App\Db\Query())->from($this->db->quoteSql($this->logTable))->max('id', $this->db);
    Severity: Minor
    Found in app/Log/Profiling.php by phpmd

    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 export uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                    } else {
                        $text = VarDumper::export($text);
                    }
    Severity: Minor
    Found in app/Log/Profiling.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

    Avoid using static access to class '\App\Db' in method 'init'.
    Open

            $this->db = DB::getInstance('log');
    Severity: Minor
    Found in app/Log/Profiling.php by phpmd

    StaticAccess

    Since: 1.4.0

    Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

    Example

    class Foo
    {
        public function bar()
        {
            Bar::baz();
        }
    }

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

    Avoid using static access to class '\yii\helpers\VarDumper' in method 'export'.
    Open

                        $text = VarDumper::export($text);
    Severity: Minor
    Found in app/Log/Profiling.php by phpmd

    StaticAccess

    Since: 1.4.0

    Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

    Example

    class Foo
    {
        public function bar()
        {
            Bar::baz();
        }
    }

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

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

        public function export()
        {
            $timings = [];
            $stack = [];
            foreach ($this->messages as $i => $log) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpmd

    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

    Define a constant instead of duplicating this literal "level" 3 times.
    Open

                            'level' => \count($stack),
    Severity: Critical
    Found in app/Log/Profiling.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.

    Define a constant instead of duplicating this literal "trace" 3 times.
    Open

                            'trace' => $last[4],
    Severity: Critical
    Found in app/Log/Profiling.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.

    Define a constant instead of duplicating this literal "category" 3 times.
    Open

                            'category' => $last[2],
    Severity: Critical
    Found in app/Log/Profiling.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.

    Define a constant instead of duplicating this literal "duration" 3 times.
    Open

                            'duration' => $timestamp - $last[3],
    Severity: Critical
    Found in app/Log/Profiling.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.

    Call to undeclared method \App\Db\Query::from
    Open

            $logID = (new \App\Db\Query())->from($this->db->quoteSql($this->logTable))->max('id', $this->db);
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    Call to method export from undeclared class \yii\helpers\VarDumper
    Open

                        $text = VarDumper::export($text);
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    @throws type of init has undeclared type \App\Log\InvalidConfigException
    Open

        public function init()
    Severity: Minor
    Found in app/Log/Profiling.php by phan

    Reference to constant LEVEL_PROFILE_END from undeclared class \yii\log\Logger
    Open

                } elseif (Logger::LEVEL_PROFILE_END == $level) {
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    Reference to constant LEVEL_PROFILE_BEGIN from undeclared class \yii\log\Logger
    Open

                if (Logger::LEVEL_PROFILE_BEGIN == $level) {
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    Reference to undeclared class \yii\log\Target
    Open

            parent::init();
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    Class extends undeclared class \yii\log\Target
    Open

    class Profiling extends Target
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    Call to undeclared method \App\Db::createCommand
    Open

                $this->db->createCommand()->insert($this->db->quoteSql($this->logTable), [
    Severity: Critical
    Found in app/Log/Profiling.php by phan

    Reference to undeclared property \App\Log\Profiling->messages
    Open

            foreach ($this->messages as $i => $log) {
    Severity: Minor
    Found in app/Log/Profiling.php by phan

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

        public $db;
    Severity: Minor
    Found in app/Log/Profiling.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

    Spaces must be used to indent lines; tabs are not allowed
    Open

            parent::init();
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $logID = (new \App\Db\Query())->from($this->db->quoteSql($this->logTable))->max('id', $this->db);
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'log_time' => $message['timestamp'],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         * Initializes the DbTarget component.
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        public function init()
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                [$token, $level, , $timestamp] = $log;
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                if (Logger::LEVEL_PROFILE_BEGIN == $level) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    $stack[] = $log;
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $timings[$last[5]] = [
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        /**
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $timings = [];
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         */
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            foreach ($this->messages as $i => $log) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    } else {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'info' => $text,
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                $log[5] = $i;
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                } elseif (Logger::LEVEL_PROFILE_END == $level) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        public $db;
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                $text = $message['info'];
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $stack = [];
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                            'level' => \count($stack),
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        ];
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        public $logTable = 'l_#__profile';
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        }
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'id' => $logID,
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         * @throws InvalidConfigException if [[db]] is invalid
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         * Stores log messages to DB.
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        /**
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                            'category' => $last[2],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                            'timestamp' => $last[3],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                            'duration' => $timestamp - $last[3],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            ++$logID;
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        /**
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

        public function export()
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                ])->execute();
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         */
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $this->db = DB::getInstance('log');
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         */
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    if (null !== ($last = array_pop($stack)) && $last[0] === $token) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $text = (string) $text;
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'trace' => $message['trace'],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    if ($text instanceof \Throwable || $text instanceof \Exception) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'category' => $message['category'],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'level' => $message['level'],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         *
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                            'info' => $last[0],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

            foreach ($timings as &$message) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                $this->db->createCommand()->insert($this->db->quoteSql($this->logTable), [
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    'duration' => $message['duration'],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    // exceptions may not be serializable if in the call stack somewhere is a Closure
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         * @var string name of the DB table to store cache content. Defaults to "log"
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

         * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                            'trace' => $last[4],
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                if (!\is_string($text)) {
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $text = VarDumper::export($text);
    Severity: Minor
    Found in app/Log/Profiling.php by phpcodesniffer

    There are no issues that match your filters.

    Category
    Status