Skip to content

Commit

Permalink
Fix issues reported by PHPCS, PHPStan, Psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
mcaskill committed Mar 13, 2024
1 parent e8a9ea2 commit 3a9ab94
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- ./phpstan-baseline.neon

parameters:
level: 1
paths:
Expand Down
5 changes: 4 additions & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<psalm xmlns="https://getpsalm.org/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
errorLevel="6"
resolveFromConfigFile="true" errorBaseline="psalm-baseline.xml">
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true">
<projectFiles>
<directory name="src" />
<directory name="tests" />
Expand Down
12 changes: 6 additions & 6 deletions src/Charcoal/CookieConsent/Config/PluginConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,15 +22,15 @@
* domain?: string,
* }
*
* @psalm-type AutoClear = array{
* cookies?: list<CookieAutoclear>,
* @psalm-type CookieCategoryAutoClear = array{
* cookies?: list<CookieAutoClear>,
* reloadPage?: bool,
* }
*
* @psalm-type CookieCategory = array{
* enabled: bool,
* readOnly: bool,
* autoClear: AutoClear,
* enabled?: bool,
* readOnly?: bool,
* autoClear?: CookieCategoryAutoClear,
* }
*
* @psalm-type Language = array{
Expand Down
18 changes: 12 additions & 6 deletions src/Charcoal/CookieConsent/CookieConsentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'] = '<br><br>' . $this->parsePlaceholders(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -322,7 +328,7 @@ protected function parseCookieCategory(Model\Category $category): array
/**
* @return array<string, mixed>
*
* @psalm-return AutoClear
* @psalm-return CookieCategoryAutoClear
*/
protected function parseAutoClear(Model\Category $category): ?array
{
Expand Down Expand Up @@ -379,8 +385,8 @@ protected function parseAutoClearCookie(array $cookie): ?array
}

/**
* @param array<string, string> $placeholders The placeholders.
* @param array<string, int> $counts The counts.
* @param array<string, mixed> $placeholders The placeholders.
* @param array<string, int> $counts The counts.
*/
protected function parsePlaceholders(string $text, array $placeholders = [], ?array &$counts = []): string
{
Expand Down
6 changes: 3 additions & 3 deletions src/Charcoal/CookieConsent/CookieConsentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand All @@ -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);
};

/**
Expand All @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions src/Charcoal/CookieConsent/Exception/ModelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TModel> */
protected ?string $model = null;
Expand All @@ -17,8 +18,8 @@ final class ModelNotFoundException extends \RuntimeException implements Exceptio
* @param (int|string)[] $ids
* @return static
*
* @psalm-param class-string<TModel> $model
* @psalm-return list<int|string> $ids
* @psalm-param class-string<\Charcoal\Model\ModelInterface> $model
* @psalm-param list<int|string> $ids
*/
public static function create(string $model, array $ids = [])
{
Expand All @@ -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<TModel> $model
* @psalm-return list<int|string> $ids
* @psalm-param class-string<TModel> $model
* @psalm-param list<int|string> $ids
*/
public function setModel(string $model, array $ids = [])
public function setModel(string $model, array $ids = []): self
{
$this->model = $model;
$this->ids = $ids;
Expand Down
2 changes: 1 addition & 1 deletion src/Charcoal/CookieConsent/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Category extends Content
private bool $enabled = false;
private bool $readOnly = false;
private bool $reloadPage = false;
/** @var TranslatableValue<(array<string, mixed>)[]> */
/** @var TranslatableValue */
private ?TranslatableValue $cookiesTable = null;
/** @var (array<string, mixed>)[] */
private ?array $cookiesAutoClear = null;
Expand Down
18 changes: 3 additions & 15 deletions src/Charcoal/CookieConsent/Model/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,14 @@
*
* Retrieves the active Category models.
*/
final class CategoryRepository
class CategoryRepository
{
/** @var CollectionLoader<Category> */
protected CollectionLoader $collectionLoader;

/**
* @param CollectionLoader<Category> $collectionLoader
* @return static
*/
public static function create(
public function __construct(
CollectionLoader $collectionLoader
) {
$repository = new static();
$repository->collectionLoader = $collectionLoader;

return $repository;
$this->collectionLoader = $collectionLoader;
}

/**
Expand Down Expand Up @@ -65,8 +57,4 @@ public function findCategories(array $ids): array

return $categories;
}

private function __construct()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@
*/
final class DisclosureRepository
{
/** @var CollectionLoader<Disclosure> */
protected CollectionLoader $collectionLoader;

/**
* @param CollectionLoader<Disclosure> $collectionLoader
* @return static
*/
public static function create(
public function __construct(
CollectionLoader $collectionLoader
) {
$repository = new static();
$repository->collectionLoader = $collectionLoader;

return $repository;
$this->collectionLoader = $collectionLoader;
}

/**
Expand Down Expand Up @@ -65,8 +57,4 @@ public function getDisclosure($id = null): Disclosure
$this->collectionLoader->modelClass()
);
}

private function __construct()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModelInterface> */
protected CollectionLoader $collectionLoader;

/**
* @param CollectionLoader<Modelinterface> $collectionLoader
* @return static
*/
public static function create(
public function __construct(
CollectionLoader $collectionLoader
) {
$modelRepository = new static();
$modelRepository->collectionLoader = $collectionLoader;

return $modelRepository;
$this->collectionLoader = $collectionLoader;
}

/**
Expand All @@ -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()
Expand Down Expand Up @@ -70,8 +62,4 @@ public function getModel($id): Modelinterface
[ $id ]
);
}

private function __construct()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 3a9ab94

Please sign in to comment.