gregor-j/correct-horse

View on GitHub

Showing 15 of 15 total issues

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

    public function remove(): void
    {
        if (!$this->upperCase->has() && $this->lowerCase->has()) {
            $this->lowerCase->remove();
        } elseif (!$this->lowerCase->has() && $this->upperCase->has()) {
Severity: Minor
Found in src/Generators/RandomWords.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 unused private methods such as 'getRandomWord'.
Open

    private function getRandomWord(): string
    {
        return $this->dictionary->getRandomWord();
    }
Severity: Minor
Found in src/Generators/RandomWord.php by phpmd

UnusedPrivateMethod

Since: 0.2

Unused Private Method detects when a private method is declared but is unused.

Example

class Something
{
    private function foo() {} // unused
}

Source https://phpmd.org/rules/unusedcode.html#unusedprivatemethod

Avoid unused private methods such as 'getRandomWord'.
Open

    private function getRandomWord(): string
    {
        return strtolower($this->dictionary->getRandomWord());
    }

UnusedPrivateMethod

Since: 0.2

Unused Private Method detects when a private method is declared but is unused.

Example

class Something
{
    private function foo() {} // unused
}

Source https://phpmd.org/rules/unusedcode.html#unusedprivatemethod

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

            } else {
                $this->upperCase->remove();
            }
Severity: Minor
Found in src/Generators/RandomWords.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 unused private methods such as 'getRandomWord'.
Open

    private function getRandomWord(): string
    {
        return strtoupper($this->dictionary->getRandomWord());
    }

UnusedPrivateMethod

Since: 0.2

Unused Private Method detects when a private method is declared but is unused.

Example

class Something
{
    private function foo() {} // unused
}

Source https://phpmd.org/rules/unusedcode.html#unusedprivatemethod

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

        } else {
            $this->upperCase->add();
        }
Severity: Minor
Found in src/Generators/RandomWords.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

Remove this commented out code.
Open

    //abstract private function getRandomWord(): string;

Programmers should not comment out code as it bloats programs and reduces readability.

Unused code should be deleted and can be retrieved from source control history if required.

See

  • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
  • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
  • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
  • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

Reference to undeclared property \GregorJ\CorrectHorse\Generators\DictionaryWordTrait->items
Open

        return in_array($word, $this->items, true);
Severity: Minor
Found in src/Generators/DictionaryWordTrait.php by phan

Reference to undeclared property \GregorJ\CorrectHorse\Generators\DictionaryWordTrait->items
Open

        $this->items[] = $word;
Severity: Minor
Found in src/Generators/DictionaryWordTrait.php by phan

Rename "$items" which has the same name as the field declared at line 21.
Open

        $items = $this->items;

Shadowing fields with a local variable is a bad practice that reduces code readability: it makes it confusing to know whether the field or the variable is being used.

Noncompliant Code Example

class Foo {
  public $myField;

  public function doSomething() {
    $myField = 0;
    ...
  }
}

See

Call to undeclared method \GregorJ\CorrectHorse\Generators\DictionaryWordTrait::getRandomWord
Open

            $word = $this->getRandomWord();
Severity: Critical
Found in src/Generators/DictionaryWordTrait.php by phan

Rename "$chars" which has the same name as the field declared at line 30.
Open

        $chars = array_values(array_diff($this->chars, $this->items));

Shadowing fields with a local variable is a bad practice that reduces code readability: it makes it confusing to know whether the field or the variable is being used.

Noncompliant Code Example

class Foo {
  public $myField;

  public function doSomething() {
    $myField = 0;
    ...
  }
}

See

Call to undeclared method \GregorJ\CorrectHorse\Generators\DictionaryWordTrait::reset
Open

        $this->reset();
Severity: Critical
Found in src/Generators/DictionaryWordTrait.php by phan

Define and throw a dedicated exception instead of using a generic one.
Open

            throw new RuntimeException('File not found: ' . $this->filename);

If you throw a general exception type, such as ErrorException, RuntimeException, or Exception in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle.

Instead, either throw a subtype that already exists in the Standard PHP Library, or create your own type that derives from Exception.

Noncompliant Code Example

throw new Exception();  // Noncompliant

Compliant Solution

throw new InvalidArgumentException();
// or
throw new UnexpectedValueException();

See

Rename "$separator" which has the same name as the field declared at line 29.
Open

        $separator = $this->separator->get();
Severity: Major
Found in src/PassphraseGenerator.php by sonar-php

Shadowing fields with a local variable is a bad practice that reduces code readability: it makes it confusing to know whether the field or the variable is being used.

Noncompliant Code Example

class Foo {
  public $myField;

  public function doSomething() {
    $myField = 0;
    ...
  }
}

See

Severity
Category
Status
Source
Language