Skip to content

Commit

Permalink
Fix psalm errors (#488)
Browse files Browse the repository at this point in the history
* fix

* fix

* fix

* Update src/Helper/ObjectParser.php

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
vjik and samdark committed Dec 23, 2022
1 parent 2a4f223 commit de58df9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/DataSet/ObjectDataSet.php
Expand Up @@ -163,6 +163,8 @@ final class ObjectDataSet implements RulesProviderInterface, DataSetInterface, A
* and protected only, this means that the rest (private ones) will be skipped. Defaults to all visibility levels
* (public, protected and private).
* @param bool $useCache Whether to use cache for data and attribute methods (partially) and rules (completely).
*
* @psalm-param int-mask-of<ReflectionProperty::IS_*> $propertyVisibility
*/
public function __construct(
/**
Expand Down
15 changes: 10 additions & 5 deletions src/Helper/ObjectParser.php
Expand Up @@ -112,7 +112,7 @@
*
* @link https://www.php.net/manual/en/language.attributes.overview.php
*
* @psalm-type RulesCache = array<int,array{0:RuleInterface,1:Attribute::TARGET_*}>|array<string,list<array{0:RuleInterface,1:Attribute::TARGET_*}>>
* @psalm-type RulesCacheItem = array{0:RuleInterface,1:Attribute::TARGET_*}
*/
final class ObjectParser
{
Expand Down Expand Up @@ -152,6 +152,7 @@ public function __construct(
* @var int Visibility levels the parsed properties must have. For example: public and protected only, this
* means that the rest (private ones) will be skipped. Defaults to all visibility levels (public, protected and
* private).
* @psalm-var int-mask-of<ReflectionProperty::IS_*>
*/
private int $propertyVisibility = ReflectionProperty::IS_PRIVATE |
ReflectionProperty::IS_PROTECTED |
Expand Down Expand Up @@ -197,7 +198,7 @@ public function __construct(
public function getRules(): array
{
if ($this->hasCacheItem('rules')) {
/** @psalm-var RulesCache */
/** @var array $rules */
$rules = $this->getCacheItem('rules');
return $this->prepareRules($rules);
}
Expand Down Expand Up @@ -317,6 +318,7 @@ public function getReflectionProperties(): array
}

if (PHP_VERSION_ID < 80100) {
/** @psalm-suppress UnusedMethodCall Need for pslam with PHP 8.1+ */
$property->setAccessible(true);
}

Expand Down Expand Up @@ -351,21 +353,24 @@ private function getReflectionSource(): ReflectionObject|ReflectionClass
}

/**
* @psalm-param RulesCache $source Raw rules containing additional metadata besides rule instances.
* @psalm-param array $source Raw rules containing additional metadata besides rule instances.
*
* @return array<int, RuleInterface>|array<string, list<RuleInterface>> An array of rules ready to use for the
* validation.
*/
private function prepareRules(array $source): array
{
$rules = [];
/**
* @var mixed $data
*/
foreach ($source as $key => $data) {
if (is_int($key)) {
/** @psalm-var array{0:RuleInterface,1:Attribute::TARGET_*} $data */
/** @psalm-var RulesCacheItem $data */
$rules[$key] = $this->prepareRule($data[0], $data[1]);
} else {
/**
* @psalm-var list<array{0:RuleInterface,1:Attribute::TARGET_*}> $data
* @psalm-var list<RulesCacheItem> $data
* @psalm-suppress UndefinedInterfaceMethod
*/
foreach ($data as $rule) {
Expand Down
5 changes: 5 additions & 0 deletions src/Rule/Nested.php
Expand Up @@ -77,13 +77,15 @@ public function __construct(

/**
* @var int What visibility levels to use when reading data and rules from validated object.
* @psalm-var int-mask-of<ReflectionProperty::IS_*>
*/
private int $propertyVisibility = ReflectionProperty::IS_PRIVATE
| ReflectionProperty::IS_PROTECTED
| ReflectionProperty::IS_PUBLIC,
/**
* @var int What visibility levels to use when reading rules from the class specified in {@see $rules}
* attribute.
* @psalm-var int-mask-of<ReflectionProperty::IS_*>
*/
private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
| ReflectionProperty::IS_PROTECTED
Expand Down Expand Up @@ -124,6 +126,9 @@ public function getRules(): iterable|null
return $this->rules;
}

/**
* @psalm-return int-mask-of<ReflectionProperty::IS_*>
*/
public function getPropertyVisibility(): int
{
return $this->propertyVisibility;
Expand Down
2 changes: 2 additions & 0 deletions src/RulesProvider/AttributesRulesProvider.php
Expand Up @@ -102,6 +102,8 @@ final class AttributesRulesProvider implements RulesProviderInterface
* protected only, this means that the rest (private ones) will be skipped. Defaults to all visibility levels
* (public, protected and private).
* @param bool $skipStaticProperties Whether the properties with "static" modifier must be skipped.
*
* @psalm-param int-mask-of<ReflectionProperty::IS_*> $propertyVisibility
*/
public function __construct(
string|object $source,
Expand Down

0 comments on commit de58df9

Please sign in to comment.