sam002/yii2-otp

View on GitHub
src/Otp.php

Summary

Maintainability
A
45 mins
Test Coverage

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

    public function init()
    {
        parent::init();
        if ($this->algorithm === self::ALGORITHM_TOTP) {
            $this->otp = OtpHelper::getTotp($this->label, $this->digits, $this->digest, $this->interval, $this->issuer);
Severity: Minor
Found in src/Otp.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

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

        } else {
            throw new InvalidConfigException('otp::$algorithm = \"' . $this->algorithm . '\" not allowed, only Otp::ALGORITHM_TOTP or Otp::ALGORITHM_HOTP');
        }
Severity: Minor
Found in src/Otp.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 '\sam002\otp\helpers\OtpHelper' in method 'init'.
Open

            $this->otp = OtpHelper::getTotp($this->label, $this->digits, $this->digest, $this->interval, $this->issuer);
Severity: Minor
Found in src/Otp.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 '\sam002\otp\helpers\OtpHelper' in method 'init'.
Open

            $this->otp = OtpHelper::getHotp($this->label, $this->digits, $this->digest, $this->counter, $this->issuer);
Severity: Minor
Found in src/Otp.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 '\sam002\otp\helpers\OtpHelper' in method 'getSecret'.
Open

            $this->secret = OtpHelper::generateSecret($this->secretLength);
Severity: Minor
Found in src/Otp.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 init uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

            } else {
                throw new InvalidConfigException($validator->message);
            }
Severity: Minor
Found in src/Otp.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 '\ParagonIE\ConstantTime\Base32' in method 'setSecret'.
Open

        } elseif ( strlen(Base32::decodeUpper(strtoupper($value))) < 1 ) {
Severity: Minor
Found in src/Otp.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

Expected 0 spaces before closing bracket; 1 found
Open

        } elseif ( strlen(Base32::decodeUpper(strtoupper($value))) < 1 ) {
Severity: Minor
Found in src/Otp.php by phpcodesniffer

Line exceeds 120 characters; contains 150 characters
Open

            throw new InvalidConfigException('Otp::setSecret length is not equal to ' . $this->secretLength . ' ([\'length\'] component settenings)');
Severity: Minor
Found in src/Otp.php by phpcodesniffer

Line exceeds 120 characters; contains 143 characters
Open

            throw new InvalidConfigException('otp::$length only integer, min='. self::SECRET_LENGTH_MIN .'and max=' . self::SECRET_LENGTH_MAX);
Severity: Minor
Found in src/Otp.php by phpcodesniffer

Line exceeds 120 characters; contains 156 characters
Open

            throw new InvalidConfigException('otp::$algorithm = \"' . $this->algorithm . '\" not allowed, only Otp::ALGORITHM_TOTP or Otp::ALGORITHM_HOTP');
Severity: Minor
Found in src/Otp.php by phpcodesniffer

Line exceeds 120 characters; contains 145 characters
Open

        if (!is_numeric($this->secretLength) || $this->secretLength < self::SECRET_LENGTH_MIN || $this->secretLength > self::SECRET_LENGTH_MAX) {
Severity: Minor
Found in src/Otp.php by phpcodesniffer

Expected 0 spaces after opening bracket; 1 found
Open

        } elseif ( strlen(Base32::decodeUpper(strtoupper($value))) < 1 ) {
Severity: Minor
Found in src/Otp.php by phpcodesniffer

No space found after comma in function call
Open

                $this->otp->setParameter('image',$this->imgLabelUrl);
Severity: Minor
Found in src/Otp.php by phpcodesniffer

Expected 1 space after IF keyword; 0 found
Open

        if(strlen($value) !== $this->secretLength) {
Severity: Minor
Found in src/Otp.php by phpcodesniffer

There are no issues that match your filters.

Category
Status