Test Failed
Push — v5 ( 76f887...23bb38 )
by Andrew
48:40 queued 29:42
created

ShortLink::getPreviewHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
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\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...
14
use craft\base\Field;
15
use craft\base\InlineEditableFieldInterface;
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, InlineEditableFieldInterface
31
{
32
    protected static bool $allowShortLinkUpdates = true;
33
    public string $redirectSrcMatch = 'pathonly';
34
    public int $redirectHttpCode = 301;
35
36
    // Static Methods
37
    // =========================================================================
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @inheritdoc
41
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
42
    public static function displayName(): string
43
    {
44
        return Craft::t('retour', 'Short Link');
45
    }
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @inheritdoc
49
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
50
    public static function icon(): string
51
    {
52
        return '@nystudio107/retour/icon-mask.svg';
53
    }
54
55
    /**
56
     * Prevent element updates from updating the short link redirects.
57
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
58
    public static function preventShortLinkUpdates(): void
59
    {
60
        self::$allowShortLinkUpdates = false;
61
    }
62
63
    /**
64
     * Allow element updates to update the short link redirects.
65
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
66
    public static function allowShortLinkUpdates(): void
67
    {
68
        self::$allowShortLinkUpdates = true;
69
    }
70
71
    // Public Methods
72
    // =========================================================================
73
74
    /**
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...
75
     * @inheritdoc
76
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
77
    public function getInputHtml($value, ElementInterface $element = null): string
78
    {
79
        // Render the input template
80
        return Craft::$app->getView()->renderTemplate(
81
            'retour/_components/fields/ShortLink_input',
82
            [
83
                'name' => $this->handle,
84
                'value' => $value,
85
                'field' => $this,
86
            ]
87
        );
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @inheritdoc
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function getSettingsHtml(): string
94
    {
95
        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...
96
            [
97
                'field' => $this,
98
            ]);
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...
99
    }
100
101
    /**
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...
102
     * @inheritdoc
103
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
104
    public function getPreviewHtml($value, ElementInterface $element): string
105
    {
106
        $decoded = Json::decodeIfJson($value);
107
        if (is_array($decoded)) {
108
            $value = $decoded['legacyUrl'] ?? '';
109
        }
110
        // Render the preview template
111
        return Craft::$app->getView()->renderTemplate(
112
            'retour/_components/fields/ShortLink_preview',
113
            [
114
                'name' => $this->handle,
115
                'value' => $value,
116
                'field' => $this,
117
            ]
118
        );
119
    }
120
121
    /**
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...
122
     * @inheritdoc
123
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
124
    public function afterElementSave(ElementInterface $element, bool $isNew): void
125
    {
126
        if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) {
127
            return;
128
        }
129
130
        $value = $element->getFieldValue($this->handle);
131
        // Return for propagating elements
132
        if ($this->redirectSrcMatch === 'pathonly') {
133
            $parentElement = ElementHelper::rootElement($element);
134
            if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) {
135
                return;
136
            }
137
        } elseif (!empty($value) && !StringHelper::startsWith($value, 'http')) {
138
            $value = UrlHelper::siteUrl($value, null, null, $element->siteId);
139
        }
140
141
        $parentElement = ElementHelper::rootElement($element);
142
        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

142
        RetourPlugin::$plugin->redirects->/** @scrutinizer ignore-call */ 
143
                                          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...
143
144
        if (!empty($value)) {
145
            $redirectSrcMatch = $this->redirectSrcMatch;
146
147
            if ($this->translationMethod !== Field::TRANSLATION_METHOD_NONE) {
148
                if (!UrlHelper::isAbsoluteUrl($value)) {
149
                    $value = UrlHelper::siteUrl($value, null, null, $parentElement->siteId);
150
                    $redirectSrcMatch = 'fullurl';
151
                }
152
            }
153
154
            RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode);
155
        }
156
157
        parent::afterElementSave($element, $isNew);
158
    }
159
160
    /**
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...
161
     * @inheritdoc
162
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
163
    public function afterElementDelete(ElementInterface $element): void
164
    {
165
        if (!$element->getIsCanonical()) {
166
            return;
167
        }
168
169
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true);
170
        parent::afterElementDelete($element);
171
    }
172
}
173