studioespresso/craft-seo-fields

View on GitHub
src/services/DefaultsService.php

Summary

Maintainability
A
0 mins
Test Coverage

Avoid using static access to class '\craft\helpers\Json' in method 'getDataBySiteId'.
Open

                Json::decode($record->getAttribute("defaultMeta")) ?? [],
Severity: Minor
Found in src/services/DefaultsService.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 getDataBySiteId uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

        } else {
            return new SeoDefaultsModel();
        }
Severity: Minor
Found in src/services/DefaultsService.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 getRobotsForSite uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

        } else {
            $model = new SeoDefaultsModel();
            $fields = [
                'enableRobots' => $record->enableRobots,
                'robots' => $record->robots,
Severity: Minor
Found in src/services/DefaultsService.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\Json' in method 'getDataById'.
Open

                Json::decode($record->getAttribute("defaultMeta")) ?? [],
Severity: Minor
Found in src/services/DefaultsService.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\DefaultsRecord' in method 'saveDefaults'.
Open

        $record = DefaultsRecord::findOne(
            ['siteId' => $siteId]
        );
Severity: Minor
Found in src/services/DefaultsService.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\DefaultsRecord' in method 'getRecordForSiteId'.
Open

        $record = DefaultsRecord::findOne(
            ['siteId' => $siteId]
        );
Severity: Minor
Found in src/services/DefaultsService.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\DefaultsRecord' in method 'getDataById'.
Open

        $record = DefaultsRecord::findOne(
            ['id' => $id]
        );
Severity: Minor
Found in src/services/DefaultsService.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

Define a constant instead of duplicating this literal "defaultMeta" 3 times.
Open

        $record->setAttribute('defaultMeta', $model->toArray(['defaultSiteTitle', 'defaultMetaDescription', 'titleSeperator', 'defaultImage']));
Severity: Critical
Found in src/services/DefaultsService.php by sonar-php

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.

Define a constant instead of duplicating this literal "robots" 3 times.
Open

        $record->setAttribute('robots', $model->robots);
Severity: Critical
Found in src/services/DefaultsService.php by sonar-php

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.

Define a constant instead of duplicating this literal "enableRobots" 3 times.
Open

        $record->setAttribute('enableRobots', $model->enableRobots);
Severity: Critical
Found in src/services/DefaultsService.php by sonar-php

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.

Define a constant instead of duplicating this literal "siteId" 3 times.
Open

            ['siteId' => $siteId]
Severity: Critical
Found in src/services/DefaultsService.php by sonar-php

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.

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::setAttribute
Open

        $record->setAttribute('schema', $model->schema);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Class extends undeclared class \craft\base\Component
Open

class DefaultsService extends Component
Severity: Critical
Found in src/services/DefaultsService.php by phan

Parameter $site has undeclared type \craft\models\Site
Open

    public function getRobotsForSite(Site $site)
Severity: Minor
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::setAttribute
Open

        $record->setAttribute('enableRobots', $model->enableRobots);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Reference to instance property id from undeclared class \craft\models\Site
Open

        $defaults->siteId = $site->id;
Severity: Minor
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\models\SeoDefaultsModel::setAttributes
Open

            $model->setAttributes($fields);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\models\SeoDefaultsModel::toArray
Open

        $record->setAttribute('defaultMeta', $model->toArray(['defaultSiteTitle', 'defaultMetaDescription', 'titleSeperator', 'defaultImage']));
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::setAttribute
Open

        $record->setAttribute('defaultMeta', $model->toArray(['defaultSiteTitle', 'defaultMetaDescription', 'titleSeperator', 'defaultImage']));
Severity: Critical
Found in src/services/DefaultsService.php by phan

Reference to instance property id from undeclared class \craft\models\Site
Open

        $this->saveDefaults($defaults, $site->id);
Severity: Minor
Found in src/services/DefaultsService.php by phan

Static call to undeclared method \studioespresso\seofields\records\DefaultsRecord::findOne
Open

        $record = DefaultsRecord::findOne(
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::setAttribute
Open

        $record->setAttribute('sitemap', $model->sitemap);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::save
Open

            $record->save();
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to method decode from undeclared class \craft\helpers\Json
Open

                Json::decode($record->getAttribute("defaultMeta")) ?? [],
Severity: Critical
Found in src/services/DefaultsService.php by phan

Static call to undeclared method \studioespresso\seofields\records\DefaultsRecord::findOne
Open

        $record = DefaultsRecord::findOne(
Severity: Critical
Found in src/services/DefaultsService.php by phan

Reference to static property app from undeclared class \Craft
Open

        $site = Craft::$app->sites->getSiteByHandle($handle);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Parameter $site has undeclared type \craft\models\Site
Open

    public function copyDefaultsForSite(Site $site, $oldPrimarySiteId)
Severity: Minor
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::setAttribute
Open

        $record->setAttribute('siteId', $model->siteId ?? $siteId);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\models\SeoDefaultsModel::setAttributes
Open

            $model->setAttributes($fields);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Reference to instance property id from undeclared class \craft\models\Site
Open

        return $this->getDataBySiteId($site->id);
Severity: Minor
Found in src/services/DefaultsService.php by phan

Reference to static property app from undeclared class \Craft
Open

            $site = Craft::$app->getSites()->getPrimarySite();
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\models\SeoDefaultsModel::setAttributes
Open

            $model->setAttributes($fields);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::setAttribute
Open

        $record->setAttribute('robots', $model->robots);
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to undeclared method \studioespresso\seofields\records\DefaultsRecord::validate
Open

        if ($record->validate()) {
Severity: Critical
Found in src/services/DefaultsService.php by phan

Parameter $site has undeclared type \craft\models\Site
Open

    public function getDataBySite(Site $site)
Severity: Minor
Found in src/services/DefaultsService.php by phan

Reference to static property plugin from undeclared class \studioespresso\seofields\SeoFields
Open

        if (!SeoFields::$plugin->getSettings()->robotsPerSite) {
Severity: Critical
Found in src/services/DefaultsService.php by phan

Static call to undeclared method \studioespresso\seofields\records\DefaultsRecord::findOne
Open

        $record = DefaultsRecord::findOne(
Severity: Critical
Found in src/services/DefaultsService.php by phan

Call to method decode from undeclared class \craft\helpers\Json
Open

                Json::decode($record->getAttribute("defaultMeta")) ?? [],
Severity: Critical
Found in src/services/DefaultsService.php by phan

Reference to instance property id from undeclared class \craft\models\Site
Open

        $record = $this->getRecordForSiteId($site->id);
Severity: Minor
Found in src/services/DefaultsService.php by phan

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

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

        $record->setAttribute('defaultMeta', $model->toArray(['defaultSiteTitle', 'defaultMetaDescription', 'titleSeperator', 'defaultImage']));

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

                ]);

Closing parenthesis of a multi-line function call must be on a line by itself
Open

                ]);

There are no issues that match your filters.

Category
Status