Skip to content

Commit b74a429

Browse files
committed
fix: apply linter suggestions
1 parent aa27ea7 commit b74a429

6 files changed

Lines changed: 77 additions & 8 deletions

File tree

src/Support/Metadata/Schema.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public function get(string $name): Field
4646
*/
4747
public function rules(): array
4848
{
49-
return array_map(fn (Field $field) => $field->rules(), $this->available());
49+
$callback = fn (Field $field) => $field->rules();
50+
$rules = $this->available();
51+
return array_map($callback, $rules);
5052
}
5153

5254
/**
@@ -65,6 +67,9 @@ public function mappings(): array
6567
return $mappings;
6668
}
6769

70+
/**
71+
* @return array<string, Field>
72+
*/
6873
private function available(): array
6974
{
7075
return $this->fieldset->filter(fn (Field $field) => $field->isAvailable());

src/Support/Metadata/Schema/Field.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ final class Field
112112
private bool $available = true;
113113

114114
private Closure|string|null $map = null;
115+
116+
/**
117+
* @var class-string<object>|null
118+
*/
115119
private ?string $source = null;
116120

117121
public function __construct(
@@ -121,6 +125,9 @@ public function __construct(
121125
) {
122126
}
123127

128+
/**
129+
* @return array<string>
130+
*/
124131
public function rules(): array
125132
{
126133
return $this->rules->all();
@@ -136,11 +143,17 @@ public function isAvailable(): bool
136143
return $this->available;
137144
}
138145

146+
/**
147+
* @param class-string<object> $source
148+
*/
139149
public function setSource(string $source): void
140150
{
141151
$this->source = $source;
142152
}
143153

154+
/**
155+
* @return class-string<object>|null
156+
*/
144157
public function getSource(): ?string
145158
{
146159
return $this->source;
@@ -174,9 +187,11 @@ private function is(array $haystack, string $needle): bool
174187
return in_array($needle, $haystack, true);
175188
}
176189

177-
private function handleMapping(Closure|string $map): void
190+
private function handleMapping(mixed $mapping): void
178191
{
179-
$this->map = $map;
192+
if ($mapping instanceof Closure || is_string($mapping)) {
193+
$this->map = $mapping;
194+
}
180195
}
181196

182197
private function handleVisibility(string $name): void

src/Support/Metadata/Schema/Field/Fieldset.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ public function has(string $name): bool
2929
return array_key_exists($name, $this->fields);
3030
}
3131

32+
/**
33+
* @return array<string, Field>
34+
*/
3235
public function filter(Closure $criteria): array
3336
{
3437
return array_filter($this->fields, $criteria);
3538
}
39+
40+
/**
41+
* @return array<string, Field>
42+
*/
43+
public function all(): array
44+
{
45+
return $this->fields;
46+
}
3647
}

src/Support/Metadata/Schema/Field/Formatter/PatternFormatter.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use function Constructo\Cast\stringify;
1313
use function count;
1414
use function is_array;
15-
use function sprintf;
15+
use function vsprintf;
1616

1717
class PatternFormatter implements Formatter
1818
{
@@ -39,9 +39,29 @@ public function formatWithParameters(array $value): array
3939
if ($parameters === null) {
4040
$parameters = [];
4141
}
42+
$parameters = $this->normalizeParameters($parameters);
43+
return [vsprintf(stringify($pattern), $parameters)];
44+
}
45+
46+
/**
47+
* @return array<bool|float|int|string|null>
48+
*/
49+
public function normalizeParameters(mixed $parameters): array
50+
{
4251
if ($parameters instanceof Closure) {
4352
$parameters = arrayify($parameters());
4453
}
45-
return [sprintf(stringify($pattern), ...$parameters)];
54+
if (! is_array($parameters)) {
55+
$parameters = [$parameters];
56+
}
57+
58+
$normalizer = fn (mixed $value) => match (true) {
59+
is_bool($value) => $value,
60+
is_int($value) => $value,
61+
is_float($value) => $value,
62+
is_string($value) => $value,
63+
default => null
64+
};
65+
return array_map($normalizer, $parameters);
4666
}
4767
}

src/Support/Metadata/Schema/Field/Rule.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use function Constructo\Cast\arrayify;
1212
use function Constructo\Cast\stringify;
13+
use function is_string;
1314
use function sprintf;
1415

1516
readonly class Rule implements Stringable, JsonSerializable
@@ -20,7 +21,11 @@ public function __construct(
2021
public Spec $spec,
2122
public array $arguments = [],
2223
) {
23-
$this->key = $spec->properties->get('kind') ?? $spec->name;
24+
$kind = $spec->properties->get('kind');
25+
if (! is_string($kind)) {
26+
$kind = null;
27+
}
28+
$this->key = $kind ?? $spec->name;
2429
}
2530

2631
public function __toString(): string
@@ -48,7 +53,12 @@ private function enforce(mixed $item): string
4853
is_string($item) => $item,
4954
is_iterable($item) => implode(
5055
',',
51-
array_map(fn (mixed $element): string => $this->enforce($element), $item)
56+
array_map(
57+
fn ($element): string => $this->enforce($element),
58+
is_array($item)
59+
? $item
60+
: iterator_to_array($item)
61+
)
5262
),
5363
default => stringify($item),
5464
};

src/Support/Metadata/Schema/Field/Rules.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414

1515
class Rules
1616
{
17+
/**
18+
* @var array<string, Rule>
19+
*/
1720
private array $rules = [];
1821

22+
/**
23+
* @return array<string>
24+
*/
1925
public function all(): array
2026
{
21-
return array_map(fn (Rule $rule): string => stringify($rule), array_values($this->rules));
27+
$callback = fn (mixed $rule): string => stringify($rule);
28+
$rules = array_values($this->rules);
29+
return array_map($callback, $rules);
2230
}
2331

2432
public function register(Spec $spec, array $arguments): void

0 commit comments

Comments
 (0)