studioespresso/craft-seo-fields

View on GitHub
src/services/RedirectService.php

Summary

Maintainability
A
3 hrs
Test Coverage

Method import has 34 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function import($data, $settings)
    {
        App::maxPowerCaptain();
        $patternCol = $settings['patternCol'];
        $redirectCol = $settings['redirectCol'];
Severity: Minor
Found in src/services/RedirectService.php - About 1 hr to fix

    Function import has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        public function import($data, $settings)
        {
            App::maxPowerCaptain();
            $patternCol = $settings['patternCol'];
            $redirectCol = $settings['redirectCol'];
    Severity: Minor
    Found in src/services/RedirectService.php - About 55 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

    Function getElementUrls has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
    Open

        private function getElementUrls(Element $element)
        {
            $uris = [];
            if (!ElementHelper::isDraftOrRevision($element) && $element->id) {
                foreach (Craft::$app->getSites()->getAllSites(true) as $site) {
    Severity: Minor
    Found in src/services/RedirectService.php - About 35 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

    Function redirect has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
    Open

        private function redirect(RedirectModel|RedirectRecord|array $redirect)
        {
            try {
                if (is_array($redirect)) {
                    $url = $redirect['url'];
    Severity: Minor
    Found in src/services/RedirectService.php - About 35 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

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

        public function handleUriChange(Element $element)
        {
            if (empty($this->oldUris[$element->id])) {
                return;
            }
    Severity: Minor
    Found in src/services/RedirectService.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

    Avoid using static access to class '\Craft' in method 'import'.
    Open

                    Craft::error(Json::encode($model->getErrors()));
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\UrlHelper' in method 'handleUriChange'.
    Open

                    $newUrl = UrlHelper::siteUrl($newUri, null, null, $siteId);
    Severity: Minor
    Found in src/services/RedirectService.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 '\studioespresso\seofields\records\RedirectRecord' in method 'deleteRedirectById'.
    Open

            $record = RedirectRecord::findOne(['id' => $id]);
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\App' in method 'import'.
    Open

            App::maxPowerCaptain();
    Severity: Minor
    Found in src/services/RedirectService.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 handleRedirect uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

            } else {
                $model = new RedirectModel($redirect->getAttributes());
            }
    Severity: Minor
    Found in src/services/RedirectService.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

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

            } else {
                $record->setAttribute('pattern', "/$model->pattern");
            }
    Severity: Minor
    Found in src/services/RedirectService.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 '\Craft' in method 'handleRedirect'.
    Open

            Craft::debug("Found a redirect for this 404, redirecting", SeoFields::class);
    Severity: Minor
    Found in src/services/RedirectService.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 '\studioespresso\seofields\records\RedirectRecord' in method 'getAllRedirects'.
    Open

            $query = RedirectRecord::find();
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\DateTimeHelper' in method 'updateOnRedirect'.
    Open

            $model->dateLastHit = DateTimeHelper::toIso8601(time());
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\UrlHelper' in method 'handleUriChange'.
    Open

                    $oldUrl = UrlHelper::siteUrl($oldUri, null, null, $siteId);
    Severity: Minor
    Found in src/services/RedirectService.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 '\Craft' in method 'handleRedirect'.
    Open

                Craft::error($e->getMessage(), __METHOD__);
    Severity: Minor
    Found in src/services/RedirectService.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 '\studioespresso\seofields\records\RedirectRecord' in method 'getRedirectById'.
    Open

            $record = RedirectRecord::findOne(['id' => $id]);
    Severity: Minor
    Found in src/services/RedirectService.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 saveRedirect uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

            } else {
                $record = new RedirectRecord();
            }
    Severity: Minor
    Found in src/services/RedirectService.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 '\studioespresso\seofields\records\RedirectRecord' in method 'saveRedirect'.
    Open

                $record = RedirectRecord::findOne(['id' => $model->id]);
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\Json' in method 'import'.
    Open

                    Craft::error(Json::encode($model->getErrors()));
    Severity: Minor
    Found in src/services/RedirectService.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 redirect uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                } else {
                    $method = $redirect->method;
                    if ($redirect->siteId) {
                        $url = UrlHelper::siteUrl($redirect->redirect, null, null, $redirect->siteId);
                    } else {
    Severity: Minor
    Found in src/services/RedirectService.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

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

                    } else {
                        $url = $redirect->redirect;
                    }
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\UrlHelper' in method 'redirect'.
    Open

                        $url = UrlHelper::siteUrl($redirect->redirect, null, null, $redirect->siteId);
    Severity: Minor
    Found in src/services/RedirectService.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 '\craft\helpers\ElementHelper' in method 'getElementUrls'.
    Open

            if (!ElementHelper::isDraftOrRevision($element) && $element->id) {
    Severity: Minor
    Found in src/services/RedirectService.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 unused local variables such as '$oldUrl'.
    Open

                    $oldUrl = UrlHelper::siteUrl($oldUri, null, null, $siteId);
    Severity: Minor
    Found in src/services/RedirectService.php by phpmd

    UnusedLocalVariable

    Since: 0.2

    Detects when a local variable is declared and/or assigned, but not used.

    Example

    class Foo {
        public function doSomething()
        {
            $i = 5; // Unused
        }
    }

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

    Avoid using empty try-catch blocks in redirect.
    Open

            } catch (\Exception $e) {
            }
    Severity: Minor
    Found in src/services/RedirectService.php by phpmd

    EmptyCatchBlock

    Since: 2.7.0

    Usually empty try-catch is a bad idea because you are silently swallowing an error condition and then continuing execution. Occasionally this may be the right thing to do, but often it's a sign that a developer saw an exception, didn't know what to do about it, and so used an empty catch to silence the problem.

    Example

    class Foo {
    
      public function bar()
      {
          try {
              // ...
          } catch (Exception $e) {} // empty catch block
      }
    }

    Source https://phpmd.org/rules/design.html#emptycatchblock

    syntax error, unexpected '|', expecting variable (T_VARIABLE)
    Open

        public function handleRedirect(RedirectRecord|array $redirect)
    Severity: Critical
    Found in src/services/RedirectService.php by phan

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

        public function getRedirectById($id)
    Severity: Minor
    Found in src/services/RedirectService.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 $id. Configured minimum length is 3.
    Open

        public function deleteRedirectById($id)
    Severity: Minor
    Found in src/services/RedirectService.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

    Line exceeds 120 characters; contains 122 characters
    Open

                    $response->redirect($url . "?" . Craft::$app->getRequest()->getQueryStringWithoutPath(), $method)->send();

    There are no issues that match your filters.

    Category
    Status