t1gor/Robots.txt-Parser-Class

View on GitHub
source/Parser/Url.php

Summary

Maintainability
A
1 hr
Test Coverage

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

    protected function parse(string $url) {
        $parsed = parse_url($url);

        if ($parsed === false) {
            $this->log("Failed to parse URL from {$url}");
Severity: Minor
Found in source/Parser/Url.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 too many return statements within this method.
Open

        return $parsed;
Severity: Major
Found in source/Parser/Url.php - About 30 mins to fix

    Avoid using static access to class 't1gor\RobotsTxtParser\Parser\HostName' in method 'parse'.
    Open

            if (!isset($parsed['host']) || !HostName::isValid($parsed['host'])) {
    Severity: Minor
    Found in source/Parser/Url.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 variables with short names like $in. Configured minimum length is 3.
    Open

        protected string $in = '';
    Severity: Minor
    Found in source/Parser/Url.php by phpmd

    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

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

        public function __construct(string $in) {
    Severity: Minor
    Found in source/Parser/Url.php by phpmd

    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 Url implements LoggerAwareInterface {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                ',' => '!%2C!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * @param string $url
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        /**
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '+' => '!%2B!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        protected function parse(string $url) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            $parsed = parse_url($url);
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            return $this->in;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                ')' => '!%29!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '*' => '!%2A!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                $this->log("Failed to parse URL from {$url}");
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * @return string string
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        protected function encode(string $url): string {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '=' => '!%3D!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '%' => '!%25!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            return preg_replace(array_values($reserved), array_keys($reserved), rawurlencode($url));
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            $parsed = $this->parse($this->in);
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            if ($parsed !== false) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * @param string $url
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                ']' => '!%5D!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        /**
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            $this->in = $this->encode(trim($in));
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '(' => '!%28!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        public function getPath() {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            $reserved = [
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '#' => '!%23!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '[' => '!%5B!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        public static function isValidScheme(string $scheme): bool {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            if ($parsed === false) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                $this->log("URL scheme invalid or missing for {$url}");
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                return false;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            if (!isset($parsed['port'])) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        public function __construct(string $in) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '$' => '!%24!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                ';' => '!%3B!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                $parsed['port'] = getservbyname($parsed['scheme'], 'tcp');
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                if (!is_int($parsed['port'])) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            return $parsed;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '!' => '!%21!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                "'" => '!%27!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings.
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '/' => '!%2F!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         */
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        protected string $in = '';
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '&' => '!%26!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                    return false;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * URL encoder according to RFC 3986
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '?' => '!%3F!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            return in_array($scheme, ['http', 'https', 'ftp', 'sftp']);
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * Parse URL
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         */
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                ':' => '!%3A!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        }
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                $this->log("URL host invalid or missing for {$url}");
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * @link http://publicmind.in/blog/url-encoding/
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                '@' => '!%40!ui',
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            ];
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                return false;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            if (!isset($parsed['host']) || !HostName::isValid($parsed['host'])) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            $parsed['custom'] = ($parsed['path'] ?? '/') . (isset($parsed['query']) ? '?' . $parsed['query'] : '');
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

        use LogsIfAvailableTrait;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

         * @return array|false
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

            if (!isset($parsed['scheme']) || !static::isValidScheme($parsed['scheme'])) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                return false;
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                    $this->log("URL port should be a number, {$parsed['port']} found for {$url}");
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

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

                return $parsed['custom'];
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

    Opening brace should be on a new line
    Open

        protected function encode(string $url): string {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

    Opening brace should be on a new line
    Open

        protected function parse(string $url) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

    Opening brace should be on a new line
    Open

        public static function isValidScheme(string $scheme): bool {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

    Opening brace should be on a new line
    Open

        public function getPath() {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

    Opening brace should be on a new line
    Open

        public function __construct(string $in) {
    Severity: Minor
    Found in source/Parser/Url.php by phpcodesniffer

    There are no issues that match your filters.

    Category
    Status