studioespresso/craft-seo-fields

View on GitHub
src/services/NotFoundService.php

Summary

Maintainability
A
3 hrs
Test Coverage

Method handleNotFound has 39 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function handleNotFound(Request $request, Site $site)
    {
        try {
            $notFoundRecord = NotFoundRecord::findOne(['fullUrl' => $request->getAbsoluteUrl(), 'urlPath' => $request->getUrl(), 'siteId' => $site->id]);
            if ($notFoundRecord) {
Severity: Minor
Found in src/services/NotFoundService.php - About 1 hr to fix

    Method getMatchingRedirect has 30 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        private function getMatchingRedirect(NotFoundModel $model): RedirectRecord|array|bool
        {
            Craft::debug("Check if our 404 is matched to a redirect", SeoFields::class);
            $parsedUrl = parse_url($model->urlPath);
    
    
    Severity: Minor
    Found in src/services/NotFoundService.php - About 1 hr to fix

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

          public function handleNotFound(Request $request, Site $site)
          {
              try {
                  $notFoundRecord = NotFoundRecord::findOne(['fullUrl' => $request->getAbsoluteUrl(), 'urlPath' => $request->getUrl(), 'siteId' => $site->id]);
                  if ($notFoundRecord) {
      Severity: Minor
      Found in src/services/NotFoundService.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 using static access to class '\craft\helpers\Db' in method 'getMatchingRedirect'.
      Open

                  Db::parseParam('sourceMatch', 'path', '='),
      Severity: Minor
      Found in src/services/NotFoundService.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\NotFoundRecord' in method 'markAsHandled'.
      Open

                  $query = NotFoundRecord::find();
      Severity: Minor
      Found in src/services/NotFoundService.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\Db' in method 'getMatchingRedirect'.
      Open

                  Db::parseParam('pattern', $parsedUrl['path'], '='),
      Severity: Minor
      Found in src/services/NotFoundService.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\NotFoundRecord' in method 'shouldWeCleanupRedirects'.
      Open

              $toDelete = NotFoundRecord::find();
      Severity: Minor
      Found in src/services/NotFoundService.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\StringHelper' in method 'saveNotFound'.
      Open

                  $record->uid = StringHelper::UUID();
      Severity: Minor
      Found in src/services/NotFoundService.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 'handleNotFound'.
      Open

                      Craft::debug("Updating excisting 404", SeoFields::class);
      Severity: Minor
      Found in src/services/NotFoundService.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 handleNotFound uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
      Open

                  } else {
                      Craft::debug("First time we see this 404, saving new record", SeoFields::class);
                      $notFoundModel = new NotFoundModel();
                      $notFoundModel->setAttributes([
                          'fullUrl' => urldecode($request->getAbsoluteUrl()),
      Severity: Minor
      Found in src/services/NotFoundService.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\NotFoundRecord' in method 'deletetById'.
      Open

              $record = NotFoundRecord::findOne(['id' => $id]);
      Severity: Minor
      Found in src/services/NotFoundService.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\Db' in method 'getAllRegexRedirects'.
      Open

              $query->where(Db::parseParam('matchType', 'regexMatch'));
      Severity: Minor
      Found in src/services/NotFoundService.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 'handleNotFound'.
      Open

                      $notFoundModel->dateLastHit = DateTimeHelper::toIso8601(time());
      Severity: Minor
      Found in src/services/NotFoundService.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 'getMatchingRedirect'.
      Open

              $redirect = RedirectRecord::find();
      Severity: Minor
      Found in src/services/NotFoundService.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 'getAllRegexRedirects'.
      Open

              $query = RedirectRecord::find();
      Severity: Minor
      Found in src/services/NotFoundService.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\NotFoundRecord' in method 'handleNotFound'.
      Open

                  $notFoundRecord = NotFoundRecord::findOne(['fullUrl' => $request->getAbsoluteUrl(), 'urlPath' => $request->getUrl(), 'siteId' => $site->id]);
      Severity: Minor
      Found in src/services/NotFoundService.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 'handleNotFound'.
      Open

                          'dateLastHit' => DateTimeHelper::toIso8601(time()),
      Severity: Minor
      Found in src/services/NotFoundService.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 'getMatchingRedirect'.
      Open

              Craft::debug("Check if our 404 is matched to a redirect", SeoFields::class);
      Severity: Minor
      Found in src/services/NotFoundService.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\Db' in method 'getMatchingRedirect'.
      Open

                  Db::parseParam('sourceMatch', 'pathWithoutParams', '='),
      Severity: Minor
      Found in src/services/NotFoundService.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 'handleNotFound'.
      Open

                      Craft::debug("First time we see this 404, saving new record", SeoFields::class);
      Severity: Minor
      Found in src/services/NotFoundService.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 'handleNotFound'.
      Open

                  Craft::error($e->getMessage(), 'seo-fields');
      Severity: Minor
      Found in src/services/NotFoundService.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\Db' in method 'getMatchingRedirect'.
      Open

              $redirect->andWhere(Db::parseParam('siteId', [null, $model->siteId], 'in'));
      Severity: Minor
      Found in src/services/NotFoundService.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\NotFoundRecord' in method 'saveNotFound'.
      Open

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

                      } else {
                          $notFoundModel->redirect = $redirect->id;
                      }
      Severity: Minor
      Found in src/services/NotFoundService.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\Db' in method 'getMatchingRedirect'.
      Open

                  Db::parseParam('pattern', $model->urlPath, '='),
      Severity: Minor
      Found in src/services/NotFoundService.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\NotFoundRecord' in method 'deleteAll'.
      Open

              $records = NotFoundRecord::find();
      Severity: Minor
      Found in src/services/NotFoundService.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\Db' in method 'getAllRegexRedirects'.
      Open

              $query->andWhere(Db::parseParam('siteId', [$model->siteId, null], 'IN'));
      Severity: Minor
      Found in src/services/NotFoundService.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

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

          public function markAsHandled(NotFoundRecord|int $record): void
      Severity: Critical
      Found in src/services/NotFoundService.php by phan

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

          public function deletetById($id)
      Severity: Minor
      Found in src/services/NotFoundService.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 153 characters
      Open

                  $notFoundRecord = NotFoundRecord::findOne(['fullUrl' => $request->getAbsoluteUrl(), 'urlPath' => $request->getUrl(), 'siteId' => $site->id]);

      Line exceeds 120 characters; contains 153 characters
      Open

              if ($request->isLivePreview || $request->isCpRequest || $request->isConsoleRequest || substr($request->getFullPath(), 0, 11) === 'cpresources') {

      Multi-line function call not indented correctly; expected 16 spaces but found 20
      Open

                          $model->urlPath

      Multi-line function call not indented correctly; expected 16 spaces but found 20
      Open

                          $pattern,

      Multi-line function call not indented correctly; expected 12 spaces but found 16
      Open

                      ) === 1) {

      There are no issues that match your filters.

      Category
      Status