wol-soft/php-performance-timer

View on GitHub

Showing 8 of 8 total issues

Avoid using static access to class 'PerformanceTimer\Timer' in method '__destruct'.
Open

        Timer::handleResults();
Severity: Minor
Found in src/TimerResultHandler.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

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

        } else {
            error_log("PerformanceTimer error: " . $exception->getMessage());
        }
Severity: Minor
Found in src/Timer.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

Line indented incorrectly; expected 12 spaces, found 16
Open

                static function (string $carry, array $result) use ($timerKey) {

Line indented incorrectly; expected 12 spaces, found 16
Open

                },

Assigning array<string></string> to property but \PerformanceTimer\Timer::$settings is array<string>|array<string>|array<string>|array{throwExceptions:true,outputHandler:null,profileNamespace:''}</string></string></string>
Open

            self::$settings['outputHandler'] = new ToLogFileHandler();
Severity: Minor
Found in src/Timer.php by phan

Call to method handle on non-class type null
Open

        $result = self::$settings['outputHandler']->handle(self::$results);
Severity: Critical
Found in src/Timer.php by phan

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

        'profileNamespace' => '',
Severity: Critical
Found in src/Timer.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 "outputHandler" 4 times.
Open

        'outputHandler' => null,
Severity: Critical
Found in src/Timer.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.

Severity
Category
Status
Source
Language