Skip to content

Commit 45f0f12

Browse files
authored
Set Psalm level to 1 (#364)
1 parent 55aa313 commit 45f0f12

92 files changed

Lines changed: 1965 additions & 886 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="4"
3+
errorLevel="1"
44
resolveFromConfigFile="true"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xmlns="https://getpsalm.org/schema/config"

src/DataSet/ObjectDataSet.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ final class ObjectDataSet implements RulesProviderInterface, DataSetInterface
2727
private bool $dataSetProvided;
2828
private bool $rulesProvided;
2929

30+
/**
31+
* @var array<string, array<string, mixed>>
32+
*/
3033
#[ArrayShape([
3134
[
3235
'rules' => 'iterable',
@@ -54,7 +57,10 @@ public function __construct(
5457
public function getRules(): iterable
5558
{
5659
if ($this->rulesProvided) {
57-
return $this->object->getRules();
60+
/** @var RulesProviderInterface $object */
61+
$object = $this->object;
62+
63+
return $object->getRules();
5864
}
5965

6066
// Providing data set assumes object has its own attributes and rules getting logic. So further parsing of
@@ -64,6 +70,7 @@ public function getRules(): iterable
6470
}
6571

6672
if ($this->hasCacheItem('rules')) {
73+
/** @var array<string, RuleInterface> */
6774
return $this->getCacheItem('rules');
6875
}
6976

@@ -96,36 +103,52 @@ public function getObject(): object
96103
public function getAttributeValue(string $attribute): mixed
97104
{
98105
if ($this->dataSetProvided) {
99-
return $this->object->getAttributeValue($attribute);
106+
/** @var DataSetInterface $object */
107+
$object = $this->object;
108+
109+
return $object->getAttributeValue($attribute);
100110
}
101111

102112
return ($this->getReflectionProperties()[$attribute] ?? null)?->getValue($this->getObject());
103113
}
104114

105115
public function hasAttribute(string $attribute): bool
106116
{
107-
return $this->dataSetProvided
108-
? $this->object->hasAttribute($attribute)
109-
: array_key_exists($attribute, $this->getReflectionProperties());
117+
if (!$this->dataSetProvided) {
118+
return array_key_exists($attribute, $this->getReflectionProperties());
119+
}
120+
121+
/** @var DataSetInterface $object */
122+
$object = $this->object;
123+
124+
return $object->hasAttribute($attribute);
110125
}
111126

112-
public function getData(): array
127+
public function getData(): mixed
113128
{
114129
if ($this->dataSetProvided) {
115-
return $this->object->getData();
130+
/** @var DataSetInterface $object */
131+
$object = $this->object;
132+
133+
return $object->getData();
116134
}
117135

118136
$data = [];
119137
foreach ($this->getReflectionProperties() as $name => $property) {
138+
/** @psalm-suppress MixedAssignment */
120139
$data[$name] = $property->getValue($this->object);
121140
}
122141

123142
return $data;
124143
}
125144

145+
/**
146+
* @return array<string, ReflectionProperty>
147+
*/
126148
private function getReflectionProperties(): array
127149
{
128150
if ($this->hasCacheItem('reflectionProperties')) {
151+
/** @var array<string, ReflectionProperty> */
129152
return $this->getCacheItem('reflectionProperties');
130153
}
131154

@@ -154,20 +177,33 @@ private function canCache(): bool
154177

155178
private function hasCacheItem(#[ExpectedValues(['rules', 'reflectionProperties'])] string $name): bool
156179
{
180+
if ($this->cacheKey === null) {
181+
return false;
182+
}
183+
157184
if (!array_key_exists($this->cacheKey, self::$cache)) {
158185
return false;
159186
}
160187

161188
return array_key_exists($name, self::$cache[$this->cacheKey]);
162189
}
163190

191+
/**
192+
* @psalm-suppress MixedInferredReturnType
193+
* @psalm-suppress MixedReturnStatement
194+
*/
164195
private function getCacheItem(#[ExpectedValues(['rules', 'reflectionProperties'])] string $name): array
165196
{
197+
/** @psalm-suppress PossiblyNullArrayOffset */
166198
return self::$cache[$this->cacheKey][$name];
167199
}
168200

169-
private function setCacheItem(#[ExpectedValues(['rules', 'reflectionProperties'])] string $name, array $rules): void
201+
private function setCacheItem(#[ExpectedValues(['rules', 'reflectionProperties'])] string $name, array $value): void
170202
{
171-
self::$cache[$this->cacheKey][$name] = $rules;
203+
/**
204+
* @psalm-suppress PossiblyNullArrayOffset
205+
* @psalm-suppress MixedAssignment
206+
*/
207+
self::$cache[$this->cacheKey][$name] = $value;
172208
}
173209
}

src/DataSetInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ interface DataSetInterface
88
{
99
/**
1010
* Get specified attribute value.
11-
*
12-
* @return mixed
1311
*/
1412
public function getAttributeValue(string $attribute): mixed;
1513

src/Error.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
66

77
final class Error
88
{
9-
/**
10-
* @psalm-param list<int|string> $valuePath
11-
*/
129
public function __construct(
1310
private string $message,
14-
1511
/**
16-
* @psalm-var array<string,scalar|null>
12+
* @var array<string, scalar|null>
1713
*/
1814
private array $parameters = [],
19-
2015
/**
21-
* @psalm-var list<int|string>
16+
* @var list<int|string>
2217
*/
2318
private array $valuePath = [],
2419
) {
@@ -30,15 +25,15 @@ public function getMessage(): string
3025
}
3126

3227
/**
33-
* @psalm-return array<string,scalar|null>
28+
* @return array<string, scalar|null>
3429
*/
3530
public function getParameters(): array
3631
{
3732
return $this->parameters;
3833
}
3934

4035
/**
41-
* @psalm-return list<int|string>
36+
* @return list<int|string>
4237
*/
4338
public function getValuePath(bool $escape = false): array
4439
{

src/Exception/InvalidCallbackReturnTypeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
final class InvalidCallbackReturnTypeException extends \Exception implements FriendlyExceptionInterface
1212
{
13-
public function __construct($result, int $code = 0, ?Throwable $previous = null)
13+
public function __construct(mixed $result, int $code = 0, ?Throwable $previous = null)
1414
{
1515
$message = sprintf(
1616
'Return value of callback must be an instance of %s, %s returned.',

src/LimitInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator;
6+
7+
interface LimitInterface
8+
{
9+
public function getMin(): ?int;
10+
11+
public function getMax(): ?int;
12+
13+
public function getExactly(): ?int;
14+
15+
public function getLessThanMinMessage(): string;
16+
17+
public function getGreaterThanMaxMessage(): string;
18+
19+
public function getNotExactlyMessage(): string;
20+
}

src/Result.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Yiisoft\Validator;
66

7-
use Closure;
87
use InvalidArgumentException;
9-
use Yiisoft\Arrays\ArrayHelper;
108

119
use function array_slice;
1210
use function implode;
@@ -49,11 +47,11 @@ public function getErrors(): array
4947
*/
5048
public function getErrorMessages(): array
5149
{
52-
return ArrayHelper::getColumn($this->errors, static fn (Error $error) => $error->getMessage());
50+
return array_map(static fn (Error $error): string => $error->getMessage(), $this->errors);
5351
}
5452

5553
/**
56-
* @psalm-return array<string, non-empty-list<string>>
54+
* @return array<string, non-empty-list<string>>
5755
*/
5856
public function getErrorMessagesIndexedByPath(string $separator = '.'): array
5957
{
@@ -67,9 +65,9 @@ public function getErrorMessagesIndexedByPath(string $separator = '.'): array
6765
}
6866

6967
/**
70-
* @psalm-return array<string, non-empty-list<string>>
71-
*
7268
* @throws InvalidArgumentException
69+
*
70+
* @return array<string, non-empty-list<string>>
7371
*/
7472
public function getErrorMessagesIndexedByAttribute(): array
7573
{
@@ -91,32 +89,35 @@ public function getErrorMessagesIndexedByAttribute(): array
9189
*/
9290
public function getAttributeErrors(string $attribute): array
9391
{
94-
return $this->getAttributeErrorsMap($attribute, static fn (Error $error): Error => $error);
92+
$errors = [];
93+
foreach ($this->errors as $error) {
94+
$firstItem = $error->getValuePath()[0] ?? '';
95+
if ($firstItem === $attribute) {
96+
$errors[] = $error;
97+
}
98+
}
99+
100+
return $errors;
95101
}
96102

97103
/**
98104
* @return string[]
99105
*/
100106
public function getAttributeErrorMessages(string $attribute): array
101-
{
102-
return $this->getAttributeErrorsMap($attribute, static fn (Error $error): string => $error->getMessage());
103-
}
104-
105-
private function getAttributeErrorsMap(string $attribute, Closure $getErrorClosure): array
106107
{
107108
$errors = [];
108109
foreach ($this->errors as $error) {
109110
$firstItem = $error->getValuePath()[0] ?? '';
110111
if ($firstItem === $attribute) {
111-
$errors[] = $getErrorClosure($error);
112+
$errors[] = $error->getMessage();
112113
}
113114
}
114115

115116
return $errors;
116117
}
117118

118119
/**
119-
* @psalm-return array<string, non-empty-list<string>>
120+
* @return array<string, non-empty-list<string>>
120121
*/
121122
public function getAttributeErrorMessagesIndexedByPath(string $attribute, string $separator = '.'): array
122123
{
@@ -143,8 +144,8 @@ public function getCommonErrorMessages(): array
143144
}
144145

145146
/**
146-
* @psalm-param array<int|string> $valuePath
147-
* @psalm-param array<string,scalar|null> $parameters
147+
* @param array<string,scalar|null> $parameters
148+
* @param list<int|string> $valuePath
148149
*/
149150
public function addError(string $message, array $parameters = [], array $valuePath = []): self
150151
{

src/Rule/AtLeast.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,22 @@ final class AtLeast implements SerializableRuleInterface, SkipOnErrorInterface,
2727

2828
public function __construct(
2929
/**
30-
* The list of required attributes that will be checked.
30+
* @var string[] The list of required attributes that will be checked.
3131
*/
3232
private array $attributes,
3333
/**
34-
* The minimum required quantity of filled attributes to pass the validation.
35-
* Defaults to 1.
34+
* @var int The minimum required quantity of filled attributes to pass the validation. Defaults to 1.
3635
*/
3736
private int $min = 1,
37+
private string $incorrectInputMessage = 'Value must be an array or an object.',
3838
/**
39-
* Message to display in case of error.
39+
* @var string Message to display in case of error.
4040
*/
4141
private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.',
42-
4342
/**
4443
* @var bool|callable|null
4544
*/
46-
private $skipOnEmpty = null,
45+
private mixed $skipOnEmpty = null,
4746
private bool $skipOnError = false,
4847
/**
4948
* @var Closure(mixed, ValidationContext):bool|null
@@ -57,6 +56,9 @@ public function getName(): string
5756
return 'atLeast';
5857
}
5958

59+
/**
60+
* @return string[]
61+
*/
6062
public function getAttributes(): array
6163
{
6264
return $this->attributes;
@@ -67,6 +69,11 @@ public function getMin(): int
6769
return $this->min;
6870
}
6971

72+
public function getIncorrectInputMessage(): string
73+
{
74+
return $this->incorrectInputMessage;
75+
}
76+
7077
public function getMessage(): string
7178
{
7279
return $this->message;
@@ -77,6 +84,7 @@ public function getOptions(): array
7784
return [
7885
'attributes' => $this->attributes,
7986
'min' => $this->min,
87+
'incorrectInputMessage' => $this->incorrectInputMessage,
8088
'message' => [
8189
'message' => $this->message,
8290
'parameters' => ['min' => $this->min],

0 commit comments

Comments
 (0)