From 8a31e7f5127cfead7e5ac412861fa7a44b0102b5 Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Wed, 20 Apr 2022 12:32:34 +0200 Subject: [PATCH] refactor: code cleanup / PHPdoc / types / PHPStan --- composer.json | 1 - src/ColourSwatches.php | 31 ++++++++++++++++---- src/elements/ColourSwatches.php | 36 ----------------------- src/fields/ColourSwatches.php | 51 +++++++++++++++++---------------- src/models/ColourSwatches.php | 30 +++++++++---------- src/models/Settings.php | 26 +++++++++++++++-- 6 files changed, 88 insertions(+), 87 deletions(-) delete mode 100644 src/elements/ColourSwatches.php diff --git a/composer.json b/composer.json index 0646e53..c45b11c 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,6 @@ "name": "Colour Swatches", "handle": "colour-swatches", "description": "Adding custom selectable colour palettes, gradients, classes and more to your field types.", - "schemaVersion": "1.4.2", "developer": "percipiolondon", "developerUrl": "https://percipio.london", "documentationUrl": "https://github.com/percipioglobal/craft-colour-swatches/blob/v4/README.md", diff --git a/src/ColourSwatches.php b/src/ColourSwatches.php index 061d5f1..682c623 100644 --- a/src/ColourSwatches.php +++ b/src/ColourSwatches.php @@ -11,6 +11,7 @@ namespace percipiolondon\colourswatches; +use Craft; use craft\base\Plugin; use craft\events\RegisterComponentTypesEvent; use craft\services\Fields; @@ -19,11 +20,14 @@ use yii\base\Event; /** - * Class Colorswatches. + * Class ColourSwatches. * * @author Percipio Global Ltd. * * @since 1.0.0 + * @property Settings $settings + * + * @method Settings getSettings() */ class ColourSwatches extends Plugin { @@ -33,15 +37,23 @@ class ColourSwatches extends Plugin /** * @var ColourSwatches */ - public static $plugin; + public static ColourSwatches $plugin; + + // Public Properties + // ========================================================================= + + /** + * @var string + */ + public string $schemaVersion = '1.4.2'; // Public Methods // ========================================================================= /** - * {@inheritdoc} + * @inheritdoc */ - public function init() + public function init(): void { parent::init(); self::$plugin = $this; @@ -53,9 +65,18 @@ function(RegisterComponentTypesEvent $event) { $event->types[] = ColourSwatchesField::class; } ); + + Craft::info( + Craft::t( + 'colour-swatches', + '{name} plugin loaded', + ['name' => $this->name] + ), + __METHOD__ + ); } - protected function createSettingsModel(): ?\craft\base\Model + protected function createSettingsModel(): Settings { return new Settings(); } diff --git a/src/elements/ColourSwatches.php b/src/elements/ColourSwatches.php deleted file mode 100644 index 6bcd638..0000000 --- a/src/elements/ColourSwatches.php +++ /dev/null @@ -1,36 +0,0 @@ - 0) { - return new ColourSwatchesModel($value); - } + $valueData = (array)Json::decode($value); + + return new ColourSwatchesModel($value); } /** - * {@inheritdoc} + * @inheritdoc */ - public function serializeValue($value, ?\craft\base\ElementInterface $element = null) + public function serializeValue($value, ?ElementInterface $element = null): mixed { $settingsPalette = $this->options; $saveValue = null; @@ -145,7 +147,7 @@ public function serializeValue($value, ?\craft\base\ElementInterface $element = } /** - * {@inheritdoc} + * @inheritdoc */ public function getSettingsHtml(): ?string { @@ -185,9 +187,9 @@ public function getSettingsHtml(): ?string } /** - * {@inheritdoc} + * @inheritdoc */ - public function getInputHtml($value, ?\craft\base\ElementInterface $element = null): string + public function getInputHtml($value, ?ElementInterface $element = null): string { // Register our asset bundle Craft::$app->getView() @@ -218,7 +220,7 @@ public function getInputHtml($value, ?\craft\base\ElementInterface $element = nu } /** - * {@inheritdoc} + * @inheritdoc */ public function getTableAttributeHtml($value, ElementInterface $element): string { @@ -251,6 +253,5 @@ public function getTableAttributeHtml($value, ElementInterface $element): string } } return '
'; - // return print_r($color); } } diff --git a/src/models/ColourSwatches.php b/src/models/ColourSwatches.php index 103b3d2..826faf5 100644 --- a/src/models/ColourSwatches.php +++ b/src/models/ColourSwatches.php @@ -3,47 +3,43 @@ namespace percipiolondon\colourswatches\models; use craft\base\Model; +use craft\helpers\Json; class ColourSwatches extends Model { /** * @var string */ + public string $label = ''; - public $label = ''; - public $color = ''; + /** + * @var string + */ + public string $color = ''; + /** + * @inheritdoc + */ public function __construct($value) { if ($this->validateJson($value)) { - // typecast our object to an array - $colorData = (array)json_decode($value); - $value = null; - $color = array_filter($colorData); - // if our array has usable data + $colorData = Json::decode($value); if (!empty($colorData['label'])) { $this->label = $colorData['label']; $this->color = $colorData['color']; } - // else ensure we return a null value (only really needed for old data stores) - else { - $value = null; - } - // else ensure we return a null value - } else { - $value = null; } } // making sure we have json data, returns boolean(true) if this is the case - public function validateJson($value) + public function validateJson($value): bool { - $json = json_decode($value); + $json = Json::decode($value); return $json && $value != $json; } - public function __toString() + public function __toString(): string { return $this->label; } diff --git a/src/models/Settings.php b/src/models/Settings.php index 776f84f..544e26b 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -6,7 +6,27 @@ class Settings extends Model { - public $colors = []; - public $palettes = []; - public $default = null; + /** + * @var array + */ + public array $colors = []; + /** + * @var array + */ + public array $palettes = []; + /** + * @var array|null + */ + public ?array $default = null; + + /** + * @inheritdoc + */ + protected function defineRules(): array + { + return [ + [['colors', 'palettes'], 'required'], + [['colors', 'palettes'], 'array'], + ]; + } }