t1gor/Robots.txt-Parser-Class

View on GitHub
source/Stream/GeneratorBasedReader.php

Summary

Maintainability
A
25 mins
Test Coverage

Function __destruct has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    public function __destruct() {
        foreach ($this->filters as $class => $instance) {
            try {
                if (is_resource($instance)) {
                    stream_filter_remove($instance);
Severity: Minor
Found in source/Stream/GeneratorBasedReader.php - About 25 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

Missing class import via use statement (line '81', column '14').
Open

            throw new \InvalidArgumentException($error);

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

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

class GeneratorBasedReader implements ReaderInterface {

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

    use LogsIfAvailableTrait;

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

     */

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

     * @var string[]

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

            SkipEndOfCommentedLineFilter::class          => false,

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

        $this->filters = [

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 (!is_resource($stream)) {

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

                $this->stream,

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

    public function __destruct() {

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

     */

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

    protected function __construct() {

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

    /**

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

                    stream_filter_remove($instance);

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

            TrimSpacesLeftFilter::class                  => false,

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

            SkipUnsupportedDirectivesFilter::class       => false,

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

                $this->log('Failed to remove filter "{class}": {message}', [

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

    private array $filters;

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

        /** @note order matters */

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

            SkipEmptyLinesFilter::class                  => false,

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

    }

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

     */

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

    private $stream;

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

     * @param string $input

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

        ];

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

     * @return static

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

            SkipCommentedLinesFilter::class              => false,

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

            SkipDirectivesWithInvalidValuesFilter::class => false,

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

                    'message' => $throwable->getMessage(),

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

     * @link https://www.php.net/manual/en/function.stream-filter-append.php#84637

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

                }

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

            try {

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

            }

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

    /**

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

        $stream = tmpfile();

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

    }

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

            $error = sprintf('Argument must be a valid resource type. %s given.', gettype($stream));

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

    protected function setStream($stream): GeneratorBasedReader {

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

            stream_filter_register($filterClass::NAME, $filterClass);

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

                STREAM_FILTER_READ,

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

        }

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

     * @param string $encoding

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

    public function setEncoding(string $encoding) {

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

        while (!feof($this->stream)) {

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

                if (is_resource($instance)) {

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

        $reader = new GeneratorBasedReader();

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

            );

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 function getContentRaw(): string {

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

    public function getContentIterated(): \Generator {

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

        rewind($stream);

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

    public static function fromStream($stream): self {

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

    /**

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

     *

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

        foreach ($this->filters as $filterClass => & $filter) {

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 (strtoupper($encoding) === RobotsTxtParser::DEFAULT_ENCODING) {

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

            return;

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

            $filter = stream_filter_append(

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

        }

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

            $line = fgets($this->stream);

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

                $filterClass::NAME,

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

        return $this;

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

        rewind($this->stream);

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

            }

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

    }

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

        }

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

        rewind($this->stream);

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

                    'class'   => $class,

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

        $this->log(WarmingMessages::ENCODING_NOT_UTF8, [], LogLevel::WARNING);

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

    }

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

            } catch (\Throwable $throwable) {

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

            fclose($this->stream);

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

        }

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

        $reader = new GeneratorBasedReader();

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

        fwrite($stream, $input);

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

        fseek($stream, 0);

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

        $reader->log(WarmingMessages::STRING_INIT_DEPRECATE);

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

        $this->stream = $stream;

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

        $this->filters['iconv'] = stream_filter_prepend($this->stream, $filterName, STREAM_FILTER_READ);

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

     * @TODO check on composer install if we have filters available

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

        $filterName = 'convert.iconv.' . $encoding . '/utf-8';

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

    }

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

        return stream_get_contents($this->stream);

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

        }

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

        return $reader->setStream($stream);

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

        // convert encoding

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

                yield $line;

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 (is_resource($this->stream)) {

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 static function fromString(string $input = ''): self {

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

            throw new \InvalidArgumentException($error);

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

                ['logger' => $this->logger] // pass logger to filters

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

        $this->log('Adding encoding filter ' . $filterName);

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

            if (false !== $line) {

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

        foreach ($this->filters as $class => $instance) {

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

        }

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

     *

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

        return $reader->setStream($stream);

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

    }

Opening brace should be on a new line
Open

    protected function __construct() {

Opening brace should be on a new line
Open

    public function __destruct() {

Opening brace should be on a new line
Open

    public function getContentIterated(): \Generator {

Opening brace should be on a new line
Open

    public static function fromStream($stream): self {

Opening brace should be on a new line
Open

    protected function setStream($stream): GeneratorBasedReader {

Opening brace should be on a new line
Open

    public static function fromString(string $input = ''): self {

Opening brace should be on a new line
Open

    public function getContentRaw(): string {

Opening brace should be on a new line
Open

    public function setEncoding(string $encoding) {

There are no issues that match your filters.

Category
Status