Skip to content

Commit 174075e

Browse files
authored
Make RulesDumper::asArray() static (#502)
1 parent b14e96c commit 174075e

6 files changed

Lines changed: 16 additions & 87 deletions

File tree

src/Helper/RulesDumper.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ final class RulesDumper
5151
*
5252
* @return array Array of rule names and corresponding settings indexed by attributes.
5353
*/
54-
public function asArray(iterable $rules): array
54+
public static function asArray(iterable $rules): array
5555
{
56-
return $this->fetchOptions($rules);
56+
return self::fetchOptions($rules);
5757
}
5858

5959
/**
@@ -63,11 +63,13 @@ public function asArray(iterable $rules): array
6363
*
6464
* @return array Array of rule names and corresponding settings indexed by attributes.
6565
*/
66-
private function fetchOptions(iterable $rules): array
66+
private static function fetchOptions(iterable $rules): array
6767
{
6868
$result = [];
69-
/** @var mixed $attribute */
70-
/** @var mixed $rule */
69+
/**
70+
* @var mixed $attribute
71+
* @var mixed $rule
72+
*/
7173
foreach ($rules as $attribute => $rule) {
7274
if (!is_int($attribute) && !is_string($attribute)) {
7375
$message = sprintf(
@@ -79,7 +81,7 @@ private function fetchOptions(iterable $rules): array
7981
}
8082

8183
if (is_iterable($rule)) {
82-
$options = $this->fetchOptions($rule);
84+
$options = self::fetchOptions($rule);
8385
} elseif ($rule instanceof RuleWithOptionsInterface) {
8486
$options = array_merge([$rule->getName()], $rule->getOptions());
8587
} elseif ($rule instanceof RuleInterface) {

src/Rule/Composite.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,6 @@ class Composite implements
102102
* @psalm-var WhenType
103103
*/
104104
protected Closure|null $when = null;
105-
/**
106-
* @var RulesDumper|null A rules dumper instance used to dump grouped {@see $rules} as array. Lazily created by
107-
* {@see getRulesDumper()} only when it's needed.
108-
*/
109-
private ?RulesDumper $rulesDumper = null;
110105

111106
/**
112107
* @param iterable $rules A set of rules that needs to be grouped. They will be normalized using
@@ -185,23 +180,6 @@ public function afterInitAttribute(object $object, int $target): void
185180
*/
186181
final protected function dumpRulesAsArray(): array
187182
{
188-
return $this->getRulesDumper()->asArray($this->getRules());
189-
}
190-
191-
/**
192-
* Returns existing rules dumper instance for dumping grouped {@see $rules} as array if it's already set. If not set
193-
* yet, creates the new instance first.
194-
*
195-
* @return RulesDumper A rules dumper instance.
196-
*
197-
* @see $rulesDumper
198-
*/
199-
private function getRulesDumper(): RulesDumper
200-
{
201-
if ($this->rulesDumper === null) {
202-
$this->rulesDumper = new RulesDumper();
203-
}
204-
205-
return $this->rulesDumper;
183+
return RulesDumper::asArray($this->getRules());
206184
}
207185
}

src/Rule/Each.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ final class Each implements
7676
* @psalm-var iterable<RuleInterface>
7777
*/
7878
private iterable $rules;
79-
/**
80-
* @var RulesDumper|null A rules dumper instance used to dump {@see $rules} as array. Lazily created by
81-
* {@see getRulesDumper()} only when it's needed.
82-
*/
83-
private ?RulesDumper $rulesDumper = null;
8479

8580
/**
8681
* @param callable|iterable|RuleInterface $rules A set of rules that needs to be applied to each element of the
@@ -215,23 +210,6 @@ public function afterInitAttribute(object $object, int $target): void
215210
*/
216211
private function dumpRulesAsArray(): array
217212
{
218-
return $this->getRulesDumper()->asArray($this->getRules());
219-
}
220-
221-
/**
222-
* Returns existing rules dumper instance for dumping defined {@see $rules} as array if it's already set. If not set
223-
* yet, creates the new instance first.
224-
*
225-
* @return RulesDumper A rules dumper instance.
226-
*
227-
* @see $rulesDumper
228-
*/
229-
private function getRulesDumper(): RulesDumper
230-
{
231-
if ($this->rulesDumper === null) {
232-
$this->rulesDumper = new RulesDumper();
233-
}
234-
235-
return $this->rulesDumper;
213+
return RulesDumper::asArray($this->getRules());
236214
}
237215
}

src/Rule/Nested.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public function getOptions(): array
346346
'requirePropertyPath' => $this->getRequirePropertyPath(),
347347
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
348348
'skipOnError' => $this->skipOnError,
349-
'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
349+
'rules' => $this->rules === null ? null : RulesDumper::asArray($this->rules),
350350
];
351351
}
352352

src/Rule/StopOnError.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ final class StopOnError implements
5050
use SkipOnErrorTrait;
5151
use WhenTrait;
5252

53-
/**
54-
* @var RulesDumper|null A rules dumper instance used to dump {@see $rules} as array. Lazily created by
55-
* {@see getRulesDumper()} only when it's needed.
56-
*/
57-
private ?RulesDumper $rulesDumper = null;
58-
5953
/**
6054
* @param iterable $rules A set of rules for running the validation. Note that they are not normalized.
6155
* @psalm-param iterable<RuleInterface> $rules
@@ -128,23 +122,6 @@ public function afterInitAttribute(object $object, int $target): void
128122
*/
129123
private function dumpRulesAsArray(): array
130124
{
131-
return $this->getRulesDumper()->asArray($this->getRules());
132-
}
133-
134-
/**
135-
* Returns existing rules dumper instance for dumping defined {@see $rules} as array if it's already set. If not set
136-
* yet, creates the new instance first.
137-
*
138-
* @return RulesDumper A rules dumper instance.
139-
*
140-
* @see $rulesDumper
141-
*/
142-
private function getRulesDumper(): RulesDumper
143-
{
144-
if ($this->rulesDumper === null) {
145-
$this->rulesDumper = new RulesDumper();
146-
}
147-
148-
return $this->rulesDumper;
125+
return RulesDumper::asArray($this->getRules());
149126
}
150127
}

tests/Helper/RulesDumperTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,30 @@ public function asArrayDataProvider(): array
7575
*/
7676
public function testAsArray($rules, array $expected): void
7777
{
78-
$dumper = new RulesDumper();
79-
$result = $dumper->asArray($rules);
78+
$result = RulesDumper::asArray($rules);
8079

8180
$this->assertEquals($expected, $result);
8281
}
8382

8483
public function testWrongRuleException(): void
8584
{
86-
$dumper = new RulesDumper();
87-
8885
$this->expectException(InvalidArgumentException::class);
8986

9087
$message = 'Every rule must implement "Yiisoft\Validator\RuleInterface". Type "string" given.';
9188
$this->expectExceptionMessage($message);
9289

93-
$dumper->asArray(['not a rule']);
90+
RulesDumper::asArray(['not a rule']);
9491
}
9592

9693
public function testWrongKeyException(): void
9794
{
98-
$dumper = new RulesDumper();
99-
10095
$this->expectException(InvalidArgumentException::class);
10196
$this->expectExceptionMessage('An attribute can only have an integer or a string type. bool given.');
102-
$dumper->asArray(new IteratorWithBooleanKey());
97+
RulesDumper::asArray(new IteratorWithBooleanKey());
10398
}
10499

105100
public function testRuleWithoutOptions(): void
106101
{
107-
$dumper = new RulesDumper();
108102
$rules = [
109103
new BooleanValue(),
110104
new RuleWithoutOptions(),
@@ -137,6 +131,6 @@ public function testRuleWithoutOptions(): void
137131
],
138132
];
139133

140-
$this->assertSame($expectedRules, $dumper->asArray($rules));
134+
$this->assertSame($expectedRules, RulesDumper::asArray($rules));
141135
}
142136
}

0 commit comments

Comments
 (0)