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
Coding Style
introduced
by
Loading history...
|
|||||
8 | * @copyright Copyright (c) 2022 nystudio107 |
||||
0 ignored issues
–
show
|
|||||
9 | */ |
||||
0 ignored issues
–
show
|
|||||
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
|
|||||
21 | * @author nystudio107 |
||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||
22 | * @package twigfield |
||||
0 ignored issues
–
show
|
|||||
23 | * @since 1.0.12 |
||||
0 ignored issues
–
show
|
|||||
24 | */ |
||||
0 ignored issues
–
show
|
|||||
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
|
|||||
36 | * @var string The name of the autocomplete |
||||
37 | */ |
||||
38 | public $name = 'BaseAutocomplete'; |
||||
39 | |||||
40 | /** |
||||
0 ignored issues
–
show
|
|||||
41 | * @var string The type of the autocomplete |
||||
42 | */ |
||||
43 | public $type = AutocompleteTypes::TwigExpressionAutocomplete; |
||||
44 | |||||
45 | /** |
||||
0 ignored issues
–
show
|
|||||
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
|
|||||
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
|
|||||
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
|
|||||
64 | * @var array The accumulated complete items |
||||
65 | */ |
||||
66 | protected $completeItems = []; |
||||
67 | |||||
68 | // Public Static Methods |
||||
69 | // ========================================================================= |
||||
70 | |||||
71 | /** |
||||
0 ignored issues
–
show
|
|||||
72 | * @inerhitDoc |
||||
73 | */ |
||||
0 ignored issues
–
show
|
|||||
74 | public function generateCompleteItems(): void |
||||
75 | { |
||||
76 | } |
||||
77 | |||||
78 | /** |
||||
0 ignored issues
–
show
|
|||||
79 | * @inerhitDoc |
||||
80 | */ |
||||
0 ignored issues
–
show
|
|||||
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
Loading history...
|
|||||
85 | return; |
||||
86 | } |
||||
87 | if (empty($path)) { |
||||
88 | $path = $item->label; |
||||
89 | } |
||||
90 | ArrayHelper::setValue($this->completeItems, $path, [ |
||||
0 ignored issues
–
show
|
|||||
91 | self::COMPLETION_KEY => array_filter($item->toArray(), static function ($v) { |
||||
0 ignored issues
–
show
|
|||||
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
|
|||||
98 | * @inerhitDoc |
||||
99 | */ |
||||
0 ignored issues
–
show
|
|||||
100 | public function getCompleteItems(): array |
||||
101 | { |
||||
102 | return $this->completeItems; |
||||
103 | } |
||||
104 | } |
||||
105 |