t1gor/Robots.txt-Parser-Class

View on GitHub
source/Stream/Filters/SkipDirectivesWithInvalidValuesFilter.php

Summary

Maintainability
A
1 hr
Test Coverage

Function filter has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
Open

    public function filter($in, $out, &$consumed, $closing) {
        while ($bucket = stream_bucket_make_writeable($in)) {
            $skippedRequestRateValues = 0;
            $skippedCrawlDelayValues = 0;
            $skippedAllowanceValues = 0;
Severity: Minor
Found in source/Stream/Filters/SkipDirectivesWithInvalidValuesFilter.php - About 1 hr 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 using static access to class '\t1gor\RobotsTxtParser\Directive' in method 'filter'.
Open

            $bucket->data = preg_replace(Directive::getCrawlDelayRegex(), '', $bucket->data, -1, $skippedCrawlDelayValues);

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 '\t1gor\RobotsTxtParser\Directive' in method 'filter'.
Open

            $bucket->data = preg_replace(Directive::getRequestRateRegex(), '', $bucket->data, -1, $skippedRequestRateValues);

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

Remove this commented out code.
Open

//            $bucket->data = preg_replace(Directive::getAllowDisallowRegex(), '', $bucket->data, -1, $skippedAllowanceValues);

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"

Define a constant instead of duplicating this literal "logger" 5 times.
Open

            if (isset($this->params['logger']) && $this->params['logger'] instanceof LoggerInterface) {

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.

Avoid unused parameters such as '$closing'.
Open

    public function filter($in, $out, &$consumed, $closing) {

UnusedFormalParameter

Since: 0.2

Avoid passing parameters to methods or constructors and then not using those parameters.

Example

class Foo
{
    private function bar($howdy)
    {
        // $howdy is not used
    }
}

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

Avoid excessively long variable names like $skippedCrawlDelayValues. Keep variable name length under 20.
Open

            $skippedCrawlDelayValues = 0;

LongVariable

Since: 0.2

Detects when a field, formal or local variable is declared with a long name.

Example

class Something {
    protected $reallyLongIntName = -3; // VIOLATION - Field
    public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
        $otherReallyLongName = -5; // VIOLATION - Local
        for ($interestingIntIndex = 0; // VIOLATION - For
             $interestingIntIndex < 10;
             $interestingIntIndex++ ) {
        }
    }
}

Source https://phpmd.org/rules/naming.html#longvariable

Avoid excessively long variable names like $skippedRequestRateValues. Keep variable name length under 20.
Open

            $skippedRequestRateValues = 0;

LongVariable

Since: 0.2

Detects when a field, formal or local variable is declared with a long name.

Example

class Something {
    protected $reallyLongIntName = -3; // VIOLATION - Field
    public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
        $otherReallyLongName = -5; // VIOLATION - Local
        for ($interestingIntIndex = 0; // VIOLATION - For
             $interestingIntIndex < 10;
             $interestingIntIndex++ ) {
        }
    }
}

Source https://phpmd.org/rules/naming.html#longvariable

Avoid excessively long variable names like $skippedAllowanceValues. Keep variable name length under 20.
Open

            $skippedAllowanceValues = 0;

LongVariable

Since: 0.2

Detects when a field, formal or local variable is declared with a long name.

Example

class Something {
    protected $reallyLongIntName = -3; // VIOLATION - Field
    public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
        $otherReallyLongName = -5; // VIOLATION - Local
        for ($interestingIntIndex = 0; // VIOLATION - For
             $interestingIntIndex < 10;
             $interestingIntIndex++ ) {
        }
    }
}

Source https://phpmd.org/rules/naming.html#longvariable

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

    public function filter($in, $out, &$consumed, $closing) {

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

Opening brace of a class must be on the line after the definition
Open

class SkipDirectivesWithInvalidValuesFilter extends \php_user_filter implements CustomFilterInterface {

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

            $bucket->data = preg_replace(Directive::getRequestRateRegex(), '', $bucket->data, -1, $skippedRequestRateValues);

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

            $bucket->data = preg_replace(Directive::getCrawlDelayRegex(), '', $bucket->data, -1, $skippedCrawlDelayValues);

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

                if ($skippedAllowanceValues > 0) {

Line exceeds 120 characters; contains 130 characters
Open

                    $this->params['logger']->debug($skippedRequestRateValues . ' char(s) dropped as invalid Request-rate value.');

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

    public function filter($in, $out, &$consumed, $closing) {

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

            if (isset($this->params['logger']) && $this->params['logger'] instanceof LoggerInterface) {

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

                    $this->params['logger']->debug($skippedCrawlDelayValues . ' char(s) dropped as invalid Crawl-delay value.');

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

        return PSFS_PASS_ON;

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

            $consumed += $bucket->datalen;

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

                if ($skippedRequestRateValues > 0) {

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

                }

Line exceeds 120 characters; contains 128 characters
Open

                    $this->params['logger']->debug($skippedCrawlDelayValues . ' char(s) dropped as invalid Crawl-delay value.');

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

            }

Line exceeds 120 characters; contains 125 characters
Open

//            $bucket->data = preg_replace(Directive::getAllowDisallowRegex(), '', $bucket->data, -1, $skippedAllowanceValues);

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

            stream_bucket_append($out, $bucket);

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

        }

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

    public const NAME = 'RTP_skip_directives_invalid_value';

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

            $skippedRequestRateValues = 0;

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

            $skippedCrawlDelayValues = 0;

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

            $skippedAllowanceValues = 0;

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

                    $this->params['logger']->debug($skippedAllowanceValues . ' char(s) dropped as invalid allow/disallow value.');

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

    public $filtername = self::NAME;

Line exceeds 120 characters; contains 125 characters
Open

            $bucket->data = preg_replace(Directive::getRequestRateRegex(), '', $bucket->data, -1, $skippedRequestRateValues);

Line exceeds 120 characters; contains 130 characters
Open

                    $this->params['logger']->debug($skippedAllowanceValues . ' char(s) dropped as invalid allow/disallow value.');

Line exceeds 120 characters; contains 123 characters
Open

            $bucket->data = preg_replace(Directive::getCrawlDelayRegex(), '', $bucket->data, -1, $skippedCrawlDelayValues);

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

    }

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

                    $this->params['logger']->debug($skippedRequestRateValues . ' char(s) dropped as invalid Request-rate value.');

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

        while ($bucket = stream_bucket_make_writeable($in)) {

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

                }

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

                if ($skippedCrawlDelayValues > 0) {

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

                }

Opening brace should be on a new line
Open

    public function filter($in, $out, &$consumed, $closing) {

There are no issues that match your filters.

Category
Status