Passed
Push — develop-v4 ( deab4e...1c6bc9 )
by Andrew
17:52
created

ShortLink::afterElementSave()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 19
c 5
b 1
f 0
dl 0
loc 34
ccs 0
cts 20
cp 0
rs 6.6166
cc 13
nc 14
nop 2
crap 182

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
6
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @license   https://nystudio107.com/license
0 ignored issues
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
8
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
9
10
namespace nystudio107\retour\fields;
11
12
use Craft;
13
use craft\base\Element;
14
use craft\base\ElementInterface;
0 ignored issues
show
Bug introduced by
The type craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use craft\base\Field;
16
use craft\base\PreviewableFieldInterface;
17
use craft\helpers\ElementHelper;
18
use craft\helpers\Json;
19
use craft\helpers\UrlHelper;
20
use nystudio107\retour\Retour as RetourPlugin;
21
use yii\helpers\StringHelper;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
25
 * @package   Retour
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @since     4.1.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
27
 *
28
 * @property-read string $contentColumnType
29
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
30
class ShortLink extends Field implements PreviewableFieldInterface
31
{
32
33
    public string $redirectSrcMatch = 'pathonly';
34
    public int $redirectHttpCode = 301;
35
36
    static protected bool $allowShortLinkUpdates = true;
37
38
    // Static Methods
39
    // =========================================================================
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @inheritdoc
43
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
44
    public static function displayName(): string
45
    {
46
        return Craft::t('retour', 'Short Link');
47
    }
48
49
    /**
50
     * Prevent element updates from updating the short link redirects.
51
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
52
    public static function preventShortLinkUpdates(): void
53
    {
54
        self::$allowShortLinkUpdates = false;
55
    }
56
57
    /**
58
     * Allow element updates to update the short link redirects.
59
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
60
    public static function allowShortLinkUpdates(): void
61
    {
62
        self::$allowShortLinkUpdates = true;
63
    }
64
65
    // Public Methods
66
    // =========================================================================
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
69
     * @inheritdoc
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71
    public function getInputHtml($value, ElementInterface $element = null): string
72
    {
73
        // Render the input template
74
        return Craft::$app->getView()->renderTemplate(
75
            'retour/_components/fields/ShortLink_input',
76
            [
77
                'name' => $this->handle,
78
                'value' => $value,
79
                'field' => $this,
80
            ]
81
        );
82
    }
83
84
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
85
     * @inheritdoc
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    public function getSettingsHtml(): string
88
    {
89
        return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings',
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
90
            [
91
                'field' => $this,
92
            ]);
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
93
    }
94
95
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $isNew should have a doc-comment as per coding-style.
Loading history...
96
     * @inheritdoc
97
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
98
    public function afterElementSave(ElementInterface $element, bool $isNew): void
99
    {
100
        if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) {
101
            return;
102
        }
103
104
        $value = $element->getFieldValue($this->handle);
105
        // Return for propagating elements
106
        if ($this->redirectSrcMatch === 'pathonly') {
107
            $parentElement = ElementHelper::rootElement($element);
108
            if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) {
109
                return;
110
            }
111
        } else if (!empty($value) && !StringHelper::startsWith($value, 'http')) {
112
            $value = UrlHelper::siteUrl($value, null, null, $element->siteId);
113
        }
114
115
        $parentElement = ElementHelper::rootElement($element);
116
        RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false);
0 ignored issues
show
Bug introduced by
The method removeElementRedirect() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        RetourPlugin::$plugin->redirects->/** @scrutinizer ignore-call */ 
117
                                          removeElementRedirect($parentElement, false);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
        if (!empty($value)) {
119
            $redirectSrcMatch = $this->redirectSrcMatch;
120
121
            if ($this->translationMethod !== Field::TRANSLATION_METHOD_NONE) {
122
                if (!UrlHelper::isAbsoluteUrl($value)) {
123
                    $value = UrlHelper::siteUrl($value, null, null, $parentElement->siteId);
124
                    $redirectSrcMatch = 'fullurl';
125
                }
126
            }
127
128
            RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode);
129
        }
130
131
        parent::afterElementSave($element, $isNew);
132
    }
133
134
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
135
     * @inheritdoc
136
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
137
    public function afterElementDelete(ElementInterface $element): void
138
    {
139
        if (!$element->getIsCanonical()) {
140
            return;
141
        }
142
143
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true);
144
        parent::afterElementDelete($element);
145
    }
146
147
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
148
     * @inheritdoc
149
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
150
    public function getTableAttributeHtml($value, ElementInterface $element): string
151
    {
152
        $decoded = Json::decodeIfJson($value);
153
        if ($decoded) {
154
            return $decoded['legacyUrl'] ?? '';
155
        }
156
157
        // Render the input template
158
        return $value;
159
    }
160
}
161