Issues (1779)

src/fields/ShortLink.php (49 issues)

1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
6
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
@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
@license tag must contain a URL and a license name
Loading history...
8
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
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
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\PreviewableFieldInterface;
16
use craft\helpers\ElementHelper;
17
use craft\helpers\Json;
18
use craft\helpers\UrlHelper;
19
use nystudio107\retour\Retour as RetourPlugin;
20
use yii\helpers\StringHelper;
21
22
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
23
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
24
 * @package   Retour
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @since     4.1.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
26
 *
27
 * @property-read string $contentColumnType
28
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
29
class ShortLink extends Field implements PreviewableFieldInterface
30
{
31
    protected static bool $allowShortLinkUpdates = true;
32
    public string $redirectSrcMatch = 'pathonly';
33
    public int $redirectHttpCode = 301;
34
35
    // Static Methods
36
    // =========================================================================
37
38
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
39
     * @inheritdoc
40
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
41
    public static function displayName(): string
42
    {
43
        return Craft::t('retour', 'Short Link');
44
    }
45
46
    /**
47
     * Prevent element updates from updating the short link redirects.
48
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
49
    public static function preventShortLinkUpdates(): void
50
    {
51
        self::$allowShortLinkUpdates = false;
52
    }
53
54
    /**
55
     * Allow element updates to update the short link redirects.
56
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
57
    public static function allowShortLinkUpdates(): void
58
    {
59
        self::$allowShortLinkUpdates = true;
60
    }
61
62
    // Public Methods
63
    // =========================================================================
64
65
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Parameter $element should have a doc-comment as per coding-style.
Loading history...
66
     * @inheritdoc
67
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
68
    public function getInputHtml($value, ElementInterface $element = null): string
69
    {
70
        // Render the input template
71
        return Craft::$app->getView()->renderTemplate(
72
            'retour/_components/fields/ShortLink_input',
73
            [
74
                'name' => $this->handle,
75
                'value' => $value,
76
                'field' => $this,
77
            ]
78
        );
79
    }
80
81
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
82
     * @inheritdoc
83
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
84
    public function getSettingsHtml(): string
85
    {
86
        return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings',
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
87
            [
88
                'field' => $this,
89
            ]);
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
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...
90
    }
91
92
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $element should have a doc-comment as per coding-style.
Loading history...
Parameter $isNew should have a doc-comment as per coding-style.
Loading history...
93
     * @inheritdoc
94
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
95
    public function afterElementSave(ElementInterface $element, bool $isNew): void
96
    {
97
        if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) {
98
            return;
99
        }
100
101
        $value = $element->getFieldValue($this->handle);
102
        // Return for propagating elements
103
        if ($this->redirectSrcMatch === 'pathonly') {
104
            $parentElement = ElementHelper::rootElement($element);
0 ignored issues
show
Deprecated Code introduced by
The function craft\helpers\ElementHelper::rootElement() has been deprecated: in 4.12.0. Use [[ElementInterface::getRootOwner()]] instead. ( Ignorable by Annotation )

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

104
            $parentElement = /** @scrutinizer ignore-deprecated */ ElementHelper::rootElement($element);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
105
            if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) {
106
                return;
107
            }
108
        } elseif (!empty($value) && !StringHelper::startsWith($value, 'http')) {
109
            $value = UrlHelper::siteUrl($value, null, null, $element->siteId);
110
        }
111
112
        $parentElement = ElementHelper::rootElement($element);
0 ignored issues
show
Deprecated Code introduced by
The function craft\helpers\ElementHelper::rootElement() has been deprecated: in 4.12.0. Use [[ElementInterface::getRootOwner()]] instead. ( Ignorable by Annotation )

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

112
        $parentElement = /** @scrutinizer ignore-deprecated */ ElementHelper::rootElement($element);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
113
        RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false);
0 ignored issues
show
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

113
        RetourPlugin::$plugin->redirects->/** @scrutinizer ignore-call */ 
114
                                          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...
114
115
        if (!empty($value)) {
116
            $redirectSrcMatch = $this->redirectSrcMatch;
117
118
            RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode);
119
        }
120
121
        parent::afterElementSave($element, $isNew);
122
    }
123
124
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $element should have a doc-comment as per coding-style.
Loading history...
125
     * @inheritdoc
126
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
127
    public function afterElementDelete(ElementInterface $element): void
128
    {
129
        if (!$element->getIsCanonical()) {
130
            return;
131
        }
132
133
        RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true);
134
        parent::afterElementDelete($element);
135
    }
136
137
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Parameter $element should have a doc-comment as per coding-style.
Loading history...
138
     * @inheritdoc
139
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
140
    public function getTableAttributeHtml($value, ElementInterface $element): string
141
    {
142
        $decoded = Json::decodeIfJson($value);
143
        if (is_array($decoded)) {
144
            $value = $decoded['legacyUrl'] ?? '';
145
        }
146
        // Render the preview template
147
        return Craft::$app->getView()->renderTemplate(
148
            'retour/_components/fields/ShortLink_preview',
149
            [
150
                'name' => $this->handle,
151
                'value' => $value,
152
                'field' => $this,
153
            ]
154
        );
155
    }
156
157
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
158
     * @inheritdoc
159
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
160
    public function getElementValidationRules(): array
161
    {
162
        return [
163
            [
164
                function(ElementInterface $element) {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
165
                    $value = $element->getFieldValue($this->handle);
166
                    $redirect = RetourPlugin::$plugin->getRedirects()->getRedirectByRedirectSrcUrl($value);
0 ignored issues
show
The method getRedirects() 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

166
                    $redirect = RetourPlugin::$plugin->/** @scrutinizer ignore-call */ getRedirects()->getRedirectByRedirectSrcUrl($value);

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...
167
                    // Handle drafts
168
                    $element = $element->getCanonical();
169
                    if ($redirect && isset($redirect['associatedElementId'])) {
170
                        if ($redirect['associatedElementId'] == 0) {
171
                            $element->addError($this->handle, Craft::t('retour', 'A Retour redirect with this Legacy URL already exists.'));
172
                        } elseif ($redirect['associatedElementId'] !== $element->id) {
173
                            $element->addError($this->handle, Craft::t('retour', 'A Short Link with this URL already exists.'));
174
                        }
175
                    }
176
                },
177
            ],
178
        ];
179
    }
180
}
181