Issues (1181)

src/base/Autocomplete.php (37 issues)

1
<?php
2
/**
3
 * Twigfield for Craft CMS
4
 *
5
 * Provides a twig editor field with Twig & Craft API autocomplete
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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...
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\twigfield\base;
12
13
use Craft;
14
use craft\base\Model;
15
use craft\helpers\ArrayHelper;
16
use nystudio107\twigfield\models\CompleteItem;
17
use nystudio107\twigfield\Twigfield;
18
use nystudio107\twigfield\types\AutocompleteTypes;
19
20
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @package   twigfield
0 ignored issues
show
Package name "twigfield" is not valid; consider "Twigfield" instead
Loading history...
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.0.12
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...
24
 */
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...
25
abstract class Autocomplete extends Model implements AutocompleteInterface
26
{
27
    // Constants
28
    // =========================================================================
29
30
    const COMPLETION_KEY = '__completions';
31
32
    // Public Properties
33
    // =========================================================================
34
35
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
36
     * @var string The name of the autocomplete
37
     */
38
    public $name = 'BaseAutocomplete';
39
40
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
41
     * @var string The type of the autocomplete
42
     */
43
    public $type = AutocompleteTypes::TwigExpressionAutocomplete;
44
45
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
46
     * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
47
     */
48
    public $hasSubProperties = false;
49
50
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
51
     * @var string The field type passed down from the template to the autocomplete
52
     */
53
    public $fieldType = TwigField::DEFAULT_FIELD_TYPE;
54
55
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
56
     * @var array The twigfield options object passed down from the template to the autocomplete
57
     */
58
    public $twigfieldOptions = [];
59
60
    // Protected Properties
61
    // =========================================================================
62
63
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
64
     * @var array The accumulated complete items
65
     */
66
    protected $completeItems = [];
67
68
    // Public Static Methods
69
    // =========================================================================
70
71
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
72
     * @inerhitDoc
73
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
74
    public function generateCompleteItems(): void
75
    {
76
    }
77
78
    /**
0 ignored issues
show
Parameter $item should have a doc-comment as per coding-style.
Loading history...
Parameter $path should have a doc-comment as per coding-style.
Loading history...
Missing short description in doc comment
Loading history...
79
     * @inerhitDoc
80
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
81
    public function addCompleteItem(CompleteItem $item, string $path = ''): void
82
    {
83
        if (!$item->validate()) {
84
            Craft::debug(print_r($item->getErrors(), true), __METHOD__);
0 ignored issues
show
It seems like print_r($item->getErrors(), true) can also be of type true; however, parameter $message of yii\BaseYii::debug() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

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

84
            Craft::debug(/** @scrutinizer ignore-type */ print_r($item->getErrors(), true), __METHOD__);
Loading history...
85
            return;
86
        }
87
        if (empty($path)) {
88
            $path = $item->label;
89
        }
90
        ArrayHelper::setValue($this->completeItems, $path, [
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
91
            self::COMPLETION_KEY => array_filter($item->toArray(), static function ($v) {
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
92
                return !is_null($v);
93
            })
0 ignored issues
show
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...
94
        ]);
0 ignored issues
show
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...
95
    }
96
97
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
98
     * @inerhitDoc
99
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
100
    public function getCompleteItems(): array
101
    {
102
        return $this->completeItems;
103
    }
104
}
105