From 3a9ab946dba8ffedba49d2a1c9e3fdd7860a456b Mon Sep 17 00:00:00 2001 From: Chauncey McAskill Date: Wed, 13 Mar 2024 14:42:45 -0400 Subject: [PATCH] Fix issues reported by PHPCS, PHPStan, Psalm --- .editorconfig | 3 +++ composer.json | 1 + phpstan-baseline.neon | 6 +++++ phpstan.neon.dist | 3 +++ psalm.xml.dist | 5 ++++- .../CookieConsent/Config/PluginConfig.php | 12 +++++----- .../CookieConsent/CookieConsentManager.php | 18 ++++++++++----- .../CookieConsentServiceProvider.php | 6 ++--- .../Exception/ModelNotFoundException.php | 18 +++++++-------- src/Charcoal/CookieConsent/Model/Category.php | 2 +- .../Model/Repository/CategoryRepository.php | 18 +++------------ .../Model/Repository/DisclosureRepository.php | 16 ++------------ .../Repository/LinkRelationRepository.php | 22 +++++-------------- .../Model/Structure/ConsentRevision.php | 2 +- 14 files changed, 59 insertions(+), 73 deletions(-) create mode 100644 phpstan-baseline.neon diff --git a/.editorconfig b/.editorconfig index 1a2c8bd..6f2c193 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,6 +10,9 @@ indent_style = space insert_final_newline = true trim_trailing_whitespace = true +[*.{neon,neon.dist,yml}] +indent_size = 2 + [*.{md,markdown}] trim_trailing_whitespace = false diff --git a/composer.json b/composer.json index 4fe6599..495a2a2 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "pimple/pimple": "^3.5" }, "require-dev": { + "charcoal/app": "^5.0", "php-coveralls/php-coveralls": "^2.2", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^9.5", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..7948992 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Unsafe usage of new static\\(\\)\\.$#" + count: 1 + path: src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b04da94..df0a374 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,3 +1,6 @@ +includes: + - ./phpstan-baseline.neon + parameters: level: 1 paths: diff --git a/psalm.xml.dist b/psalm.xml.dist index ce9a2fe..6d3ee6d 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -2,8 +2,11 @@ + findUnusedBaselineEntry="true" + findUnusedCode="false" + resolveFromConfigFile="true"> diff --git a/src/Charcoal/CookieConsent/Config/PluginConfig.php b/src/Charcoal/CookieConsent/Config/PluginConfig.php index 0b4e8bc..587dc81 100644 --- a/src/Charcoal/CookieConsent/Config/PluginConfig.php +++ b/src/Charcoal/CookieConsent/Config/PluginConfig.php @@ -7,7 +7,7 @@ /** * Plugin Configuration Options. * - * {@link https://cookieconsent.orestbida.com/reference/configuration-reference.html Configuration options for vanilla-cookieconsent.} + * {@link https://cookieconsent.orestbida.com/reference/configuration-reference.html} * * @psalm-type CookieTableRow = array{ * name: string, @@ -22,15 +22,15 @@ * domain?: string, * } * - * @psalm-type AutoClear = array{ - * cookies?: list, + * @psalm-type CookieCategoryAutoClear = array{ + * cookies?: list, * reloadPage?: bool, * } * * @psalm-type CookieCategory = array{ - * enabled: bool, - * readOnly: bool, - * autoClear: AutoClear, + * enabled?: bool, + * readOnly?: bool, + * autoClear?: CookieCategoryAutoClear, * } * * @psalm-type Language = array{ diff --git a/src/Charcoal/CookieConsent/CookieConsentManager.php b/src/Charcoal/CookieConsent/CookieConsentManager.php index e98a396..328d03b 100644 --- a/src/Charcoal/CookieConsent/CookieConsentManager.php +++ b/src/Charcoal/CookieConsent/CookieConsentManager.php @@ -15,7 +15,7 @@ * * Generates the vanilla-cookieconsent configuration options. * - * @psalm-import-type AutoClear from PluginConfig + * @psalm-import-type CookieCategoryAutoClear from PluginConfig * @psalm-import-type CookieTableRow from PluginConfig * @psalm-import-type CookieAutoClear from PluginConfig * @psalm-import-type CookieCategory from PluginConfig @@ -218,7 +218,10 @@ public function createPluginOptions(): array $revisionMessage = \trim((string)$revisionInstance->getRevisionMessage()); if ($revisionMessage) { - if ($consentModal['description'] && \strpos($consentModal['description'], '{{revisionMessage}}') === false) { + if ( + $consentModal['description'] && + \strpos($consentModal['description'], '{{revisionMessage}}') === false + ) { $consentModal['description'] .= '{{revisionMessage}}'; } $consentModal['revisionMessage'] = '

' . $this->parsePlaceholders( @@ -253,7 +256,10 @@ public function createPluginOptions(): array return $pluginOptions->data(); } - protected function getStructureModel(ModelInterface $model, string $propertyIdent): ?ModelInterface + /** + * @return ModelInterface[]|ModelInterface|null + */ + protected function getStructureModel(ModelInterface $model, string $propertyIdent) { if (!$model->hasProperty($propertyIdent)) { throw new InvalidArgumentException(sprintf( @@ -322,7 +328,7 @@ protected function parseCookieCategory(Model\Category $category): array /** * @return array * - * @psalm-return AutoClear + * @psalm-return CookieCategoryAutoClear */ protected function parseAutoClear(Model\Category $category): ?array { @@ -379,8 +385,8 @@ protected function parseAutoClearCookie(array $cookie): ?array } /** - * @param array $placeholders The placeholders. - * @param array $counts The counts. + * @param array $placeholders The placeholders. + * @param array $counts The counts. */ protected function parsePlaceholders(string $text, array $placeholders = [], ?array &$counts = []): string { diff --git a/src/Charcoal/CookieConsent/CookieConsentServiceProvider.php b/src/Charcoal/CookieConsent/CookieConsentServiceProvider.php index da2152b..8404d7b 100644 --- a/src/Charcoal/CookieConsent/CookieConsentServiceProvider.php +++ b/src/Charcoal/CookieConsent/CookieConsentServiceProvider.php @@ -34,7 +34,7 @@ public function register(Container $container) $collectionLoader->setModel($container['cookie-consent/class-map']['model/disclosure']); $collectionLoader->setCollectionClass('array'); - return Repository\DisclosureRepository::create($collectionLoader); + return new Repository\DisclosureRepository($collectionLoader); }; /** @@ -45,7 +45,7 @@ public function register(Container $container) $collectionLoader->setModel($container['cookie-consent/class-map']['model/category']); $collectionLoader->setCollectionClass('array'); - return Repository\CategoryRepository::create($collectionLoader); + return new Repository\CategoryRepository($collectionLoader); }; /** @@ -56,7 +56,7 @@ public function register(Container $container) $collectionLoader->setModel($container['cookie-consent/config']->getPrivacyPolicyObjType()); $collectionLoader->setCollectionClass('array'); - return Repository\LinkRelationRepository::create($collectionLoader); + return new Repository\LinkRelationRepository($collectionLoader); }; $container['cookie-consent/config'] = function (Container $container) { diff --git a/src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php b/src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php index e4a37d6..bd7c55e 100644 --- a/src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php +++ b/src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php @@ -3,9 +3,10 @@ namespace Charcoal\CookieConsent\Exception; /** - * @psalm-template TModel of \Charcoal\Model\Modelinterface + * @psalm-template TModel of \Charcoal\Model\ModelInterface + * @psalm-consistent-constructor */ -final class ModelNotFoundException extends \RuntimeException implements ExceptionInterface +class ModelNotFoundException extends \RuntimeException implements ExceptionInterface { /** @vpsalm-var class-string */ protected ?string $model = null; @@ -17,8 +18,8 @@ final class ModelNotFoundException extends \RuntimeException implements Exceptio * @param (int|string)[] $ids * @return static * - * @psalm-param class-string $model - * @psalm-return list $ids + * @psalm-param class-string<\Charcoal\Model\ModelInterface> $model + * @psalm-param list $ids */ public static function create(string $model, array $ids = []) { @@ -29,13 +30,12 @@ public static function create(string $model, array $ids = []) } /** - * @param (int|string)[] $ids - * @return $this + * @param (int|string)[] $ids * - * @psalm-param class-string $model - * @psalm-return list $ids + * @psalm-param class-string $model + * @psalm-param list $ids */ - public function setModel(string $model, array $ids = []) + public function setModel(string $model, array $ids = []): self { $this->model = $model; $this->ids = $ids; diff --git a/src/Charcoal/CookieConsent/Model/Category.php b/src/Charcoal/CookieConsent/Model/Category.php index 206b205..6108cf2 100644 --- a/src/Charcoal/CookieConsent/Model/Category.php +++ b/src/Charcoal/CookieConsent/Model/Category.php @@ -18,7 +18,7 @@ class Category extends Content private bool $enabled = false; private bool $readOnly = false; private bool $reloadPage = false; - /** @var TranslatableValue<(array)[]> */ + /** @var TranslatableValue */ private ?TranslatableValue $cookiesTable = null; /** @var (array)[] */ private ?array $cookiesAutoClear = null; diff --git a/src/Charcoal/CookieConsent/Model/Repository/CategoryRepository.php b/src/Charcoal/CookieConsent/Model/Repository/CategoryRepository.php index f6d1b04..c0cc37e 100644 --- a/src/Charcoal/CookieConsent/Model/Repository/CategoryRepository.php +++ b/src/Charcoal/CookieConsent/Model/Repository/CategoryRepository.php @@ -11,22 +11,14 @@ * * Retrieves the active Category models. */ -final class CategoryRepository +class CategoryRepository { - /** @var CollectionLoader */ protected CollectionLoader $collectionLoader; - /** - * @param CollectionLoader $collectionLoader - * @return static - */ - public static function create( + public function __construct( CollectionLoader $collectionLoader ) { - $repository = new static(); - $repository->collectionLoader = $collectionLoader; - - return $repository; + $this->collectionLoader = $collectionLoader; } /** @@ -65,8 +57,4 @@ public function findCategories(array $ids): array return $categories; } - - private function __construct() - { - } } diff --git a/src/Charcoal/CookieConsent/Model/Repository/DisclosureRepository.php b/src/Charcoal/CookieConsent/Model/Repository/DisclosureRepository.php index 9a4615d..27a1dae 100644 --- a/src/Charcoal/CookieConsent/Model/Repository/DisclosureRepository.php +++ b/src/Charcoal/CookieConsent/Model/Repository/DisclosureRepository.php @@ -14,20 +14,12 @@ */ final class DisclosureRepository { - /** @var CollectionLoader */ protected CollectionLoader $collectionLoader; - /** - * @param CollectionLoader $collectionLoader - * @return static - */ - public static function create( + public function __construct( CollectionLoader $collectionLoader ) { - $repository = new static(); - $repository->collectionLoader = $collectionLoader; - - return $repository; + $this->collectionLoader = $collectionLoader; } /** @@ -65,8 +57,4 @@ public function getDisclosure($id = null): Disclosure $this->collectionLoader->modelClass() ); } - - private function __construct() - { - } } diff --git a/src/Charcoal/CookieConsent/Model/Repository/LinkRelationRepository.php b/src/Charcoal/CookieConsent/Model/Repository/LinkRelationRepository.php index 745113f..ea3275e 100644 --- a/src/Charcoal/CookieConsent/Model/Repository/LinkRelationRepository.php +++ b/src/Charcoal/CookieConsent/Model/Repository/LinkRelationRepository.php @@ -4,30 +4,22 @@ use Charcoal\CookieConsent\Exception\ModelNotFoundException; use Charcoal\Model\Collection; -use Charcoal\Loader\CollectionLoader; use Charcoal\Model\ModelInterface; +use Charcoal\Loader\CollectionLoader; /** * Link Model Repository Decorator * * Retrieves a model from a model identifier for a link. */ -final class LinkRelationRepository +class LinkRelationRepository { - /** @var CollectionLoader */ protected CollectionLoader $collectionLoader; - /** - * @param CollectionLoader $collectionLoader - * @return static - */ - public static function create( + public function __construct( CollectionLoader $collectionLoader ) { - $modelRepository = new static(); - $modelRepository->collectionLoader = $collectionLoader; - - return $modelRepository; + $this->collectionLoader = $collectionLoader; } /** @@ -42,7 +34,7 @@ public function getModelType(): string * @param int|string $id * @throws ModelNotFoundException */ - public function getModel($id): Modelinterface + public function getModel($id): ModelInterface { $this->collectionLoader ->reset() @@ -70,8 +62,4 @@ public function getModel($id): Modelinterface [ $id ] ); } - - private function __construct() - { - } } diff --git a/src/Charcoal/CookieConsent/Model/Structure/ConsentRevision.php b/src/Charcoal/CookieConsent/Model/Structure/ConsentRevision.php index c40128c..8c02b52 100644 --- a/src/Charcoal/CookieConsent/Model/Structure/ConsentRevision.php +++ b/src/Charcoal/CookieConsent/Model/Structure/ConsentRevision.php @@ -72,7 +72,7 @@ public function setRevisionNumber($number): self * @param mixed $number The revision number. * @return int * - * @psalm-return positive-int + * @psalm-return int<0, max> */ protected function sanitizeRevisionNumber($number): int {