Skip to content

Commit 45fac0a

Browse files
committed
improved PHPDoc descriptions
1 parent bd12731 commit 45fac0a

11 files changed

Lines changed: 136 additions & 17 deletions

File tree

src/Schema/Context.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use function count;
1111

1212

13+
/**
14+
* Accumulates errors and warnings during schema validation and tracks the current path.
15+
*/
1316
final class Context
1417
{
1518
public bool $skipDefaults = false;
@@ -44,7 +47,10 @@ public function addWarning(string $message, string $code, array $variables = [])
4447
}
4548

4649

47-
/** @return \Closure(): bool */
50+
/**
51+
* Returns a closure that returns true as long as no new errors have been added since the call.
52+
* @return \Closure(): bool
53+
*/
4854
public function createChecker(): \Closure
4955
{
5056
$count = count($this->errors);

src/Schema/Elements/AnyOf.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use function array_merge, array_unique, implode, is_array;
1515

1616

17+
/**
18+
* Schema that accepts any of a fixed set of values or sub-schemas (union type / enumeration).
19+
*/
1720
final class AnyOf implements Schema
1821
{
1922
use Base;
@@ -32,20 +35,29 @@ public function __construct(mixed ...$set)
3235
}
3336

3437

38+
/**
39+
* Sets the first variant as the default value (instead of null).
40+
*/
3541
public function firstIsDefault(): self
3642
{
3743
$this->default = $this->set[0];
3844
return $this;
3945
}
4046

4147

48+
/**
49+
* Allows null as an accepted value in addition to the defined variants.
50+
*/
4251
public function nullable(): self
4352
{
4453
$this->set[] = null;
4554
return $this;
4655
}
4756

4857

58+
/**
59+
* Allows the value to be a DynamicParameter as an accepted variant.
60+
*/
4961
public function dynamic(): self
5062
{
5163
$this->set[] = new Type(Nette\Schema\DynamicParameter::class);

src/Schema/Elements/Base.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,41 @@ public function required(bool $state = true): self
4343
}
4444

4545

46-
/** @param callable(mixed): mixed $handler */
46+
/**
47+
* Sets a pre-normalization callback applied to the raw input value before any validation.
48+
* @param callable(mixed): mixed $handler
49+
*/
4750
public function before(callable $handler): self
4851
{
4952
$this->before = $handler(...);
5053
return $this;
5154
}
5255

5356

57+
/**
58+
* Casts the validated value to a built-in type or instantiates the given class.
59+
*/
5460
public function castTo(string $type): self
5561
{
5662
return $this->transform(Helpers::getCastStrategy($type));
5763
}
5864

5965

60-
/** @param callable(mixed, Context): mixed $handler */
66+
/**
67+
* Adds a post-validation transformation callback. The handler may also report errors via Context.
68+
* @param callable(mixed, Context): mixed $handler
69+
*/
6170
public function transform(callable $handler): self
6271
{
6372
$this->transforms[] = $handler(...);
6473
return $this;
6574
}
6675

6776

68-
/** @param callable(mixed): bool $handler */
77+
/**
78+
* Adds a custom validation assertion; optionally describe it for error messages.
79+
* @param callable(mixed): bool $handler
80+
*/
6981
public function assert(callable $handler, ?string $description = null): self
7082
{
7183
$expected = $description ?? (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
@@ -82,7 +94,9 @@ public function assert(callable $handler, ?string $description = null): self
8294
}
8395

8496

85-
/** Marks as deprecated */
97+
/**
98+
* Marks the item as deprecated; emits a warning with the given message when the item is used.
99+
*/
86100
public function deprecated(string $message = 'The item %path% is deprecated.'): self
87101
{
88102
$this->deprecated = $message;

src/Schema/Elements/Structure.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function __construct(array $shape)
3939
}
4040

4141

42+
/**
43+
* Not supported for structures; always throws.
44+
*/
4245
public function default(mixed $value): self
4346
{
4447
throw new Nette\InvalidStateException('Structure cannot have default value.');
@@ -59,21 +62,30 @@ public function max(?int $max): self
5962
}
6063

6164

65+
/**
66+
* Allows extra keys not defined in the shape, validating their values against the given type.
67+
*/
6268
public function otherItems(string|Schema $type = 'mixed'): self
6369
{
6470
$this->otherItems = $type instanceof Schema ? $type : new Type($type);
6571
return $this;
6672
}
6773

6874

75+
/**
76+
* When enabled, properties whose value equals the default are omitted from the output.
77+
*/
6978
public function skipDefaults(bool $state = true): self
7079
{
7180
$this->skipDefaults = $state;
7281
return $this;
7382
}
7483

7584

76-
/** @param Schema[]|self $shape */
85+
/**
86+
* Creates a new structure by merging this shape with additional properties.
87+
* @param Schema[]|self $shape
88+
*/
7789
public function extend(array|self $shape): self
7890
{
7991
$shape = $shape instanceof self ? $shape->items : $shape;

src/Schema/Elements/Type.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,29 @@ public function __construct(string $type)
3636
}
3737

3838

39+
/**
40+
* Allows the value to be null in addition to the declared type.
41+
*/
3942
public function nullable(): self
4043
{
4144
$this->type = 'null|' . $this->type;
4245
return $this;
4346
}
4447

4548

49+
/**
50+
* Controls whether the default value is merged with the input array (enabled by default).
51+
*/
4652
public function mergeDefaults(bool $state = true): self
4753
{
4854
$this->merge = $state;
4955
return $this;
5056
}
5157

5258

59+
/**
60+
* Allows the value to be a DynamicParameter, which is recorded for deferred validation.
61+
*/
5362
public function dynamic(): self
5463
{
5564
$this->type = DynamicParameter::class . '|' . $this->type;
@@ -86,6 +95,9 @@ public function items(string|Schema $valueType = 'mixed', string|Schema|null $ke
8695
}
8796

8897

98+
/**
99+
* Sets a regex pattern the string value must match entirely (anchored to start and end).
100+
*/
89101
public function pattern(?string $pattern): self
90102
{
91103
$this->pattern = $pattern;

src/Schema/Expect.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,38 @@ public static function __callStatic(string $name, array $args): Type
4242
}
4343

4444

45+
/**
46+
* Creates a schema for a custom type expression (e.g., 'int|string', 'null|float').
47+
*/
4548
public static function type(string $type): Type
4649
{
4750
return new Type($type);
4851
}
4952

5053

54+
/**
55+
* Creates a union schema that accepts any of the given values or sub-schemas.
56+
*/
5157
public static function anyOf(mixed ...$set): AnyOf
5258
{
5359
return new AnyOf(...$set);
5460
}
5561

5662

57-
/** @param Schema[] $shape */
63+
/**
64+
* Creates a structure schema with defined properties; output is stdClass.
65+
* @param Schema[] $shape
66+
*/
5867
public static function structure(array $shape): Structure
5968
{
6069
return new Structure($shape);
6170
}
6271

6372

64-
/** @param array<string, Schema> $items */
73+
/**
74+
* Generates a structure schema from a class instance by reflecting its properties or constructor parameters.
75+
* @param array<string, Schema> $items Optional overrides for specific properties.
76+
*/
6577
public static function from(object $object, array $items = []): Structure
6678
{
6779
$ro = new \ReflectionObject($object);
@@ -95,6 +107,8 @@ public static function from(object $object, array $items = []): Structure
95107

96108

97109
/**
110+
* Creates an array schema. When passed Schema elements, behaves like structure() but outputs an array.
111+
* Without Schema elements, creates a plain array type with the given default value.
98112
* @param mixed[] $shape
99113
*/
100114
public static function array(?array $shape = []): Structure|Type
@@ -106,12 +120,18 @@ public static function array(?array $shape = []): Structure|Type
106120
}
107121

108122

123+
/**
124+
* Creates an associative or indexed array schema where every value matches the given type.
125+
*/
109126
public static function arrayOf(string|Schema $valueType, string|Schema|null $keyType = null): Type
110127
{
111128
return (new Type('array'))->items($valueType, $keyType);
112129
}
113130

114131

132+
/**
133+
* Creates a list schema (sequentially indexed from 0) where every element matches the given type.
134+
*/
115135
public static function listOf(string|Schema $type): Type
116136
{
117137
return (new Type('list'))->items($type);

src/Schema/Helpers.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public static function merge(mixed $value, mixed $base): mixed
5454
}
5555

5656

57+
/**
58+
* Returns the type of a property or parameter as a string, or null if not determinable.
59+
*/
5760
public static function getPropertyType(\ReflectionProperty|\ReflectionParameter $prop): ?string
5861
{
5962
if ($type = Nette\Utils\Type::fromReflection($prop)) {
@@ -89,6 +92,9 @@ public static function parseAnnotation(\ReflectionClass|\ReflectionProperty $ref
8992
}
9093

9194

95+
/**
96+
* Formats a value for use in error messages (e.g., 'hello', true, object stdClass).
97+
*/
9298
public static function formatValue(mixed $value): string
9399
{
94100
if ($value instanceof DynamicParameter) {
@@ -105,6 +111,9 @@ public static function formatValue(mixed $value): string
105111
}
106112

107113

114+
/**
115+
* Adds a TypeMismatch error to the context if the value does not match the expected type.
116+
*/
108117
public static function validateType(mixed $value, string $expected, Context $context): void
109118
{
110119
if (!Nette\Utils\Validators::is($value, $expected)) {
@@ -119,7 +128,10 @@ public static function validateType(mixed $value, string $expected, Context $con
119128
}
120129

121130

122-
/** @param array{?float, ?float} $range */
131+
/**
132+
* Adds a range error to the context if the value (or its length for strings/arrays) is outside the given range.
133+
* @param array{?float, ?float} $range
134+
*/
123135
public static function validateRange(mixed $value, array $range, Context $context, string $types = ''): void
124136
{
125137
if (is_array($value) || is_string($value)) {
@@ -146,14 +158,20 @@ public static function validateRange(mixed $value, array $range, Context $contex
146158
}
147159

148160

149-
/** @param array{?float, ?float} $range */
161+
/**
162+
* Checks whether a value falls within the given [min, max] range (null means no bound).
163+
* @param array{?float, ?float} $range
164+
*/
150165
public static function isInRange(mixed $value, array $range): bool
151166
{
152167
return ($range[0] === null || $value >= $range[0])
153168
&& ($range[1] === null || $value <= $range[1]);
154169
}
155170

156171

172+
/**
173+
* Adds a PatternMismatch error to the context if the value does not match the pattern.
174+
*/
157175
public static function validatePattern(string $value, string $pattern, Context $context): void
158176
{
159177
if (!preg_match("\x01^(?:$pattern)$\x01Du", $value)) {
@@ -166,7 +184,10 @@ public static function validatePattern(string $value, string $pattern, Context $
166184
}
167185

168186

169-
/** @return \Closure(mixed): mixed */
187+
/**
188+
* Returns a closure that casts a value to the given type (built-in, class with constructor, or plain class).
189+
* @return \Closure(mixed): mixed
190+
*/
170191
public static function getCastStrategy(string $type): \Closure
171192
{
172193
if (Nette\Utils\Validators::isBuiltinType($type)) {

src/Schema/Message.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use function implode, preg_replace_callback;
1111

1212

13+
/**
14+
* Represents a single validation error or warning with a message template, error code, path, and variables.
15+
*/
1316
final class Message
1417
{
1518
/** variables: {value: mixed, expected: string} */
@@ -72,6 +75,9 @@ public function __construct(
7275
}
7376

7477

78+
/**
79+
* Formats the message template by substituting %variable% placeholders with their values.
80+
*/
7581
public function toString(): string
7682
{
7783
$vars = $this->variables;

src/Schema/Processor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ final class Processor
2121
private bool $skipDefaults = false;
2222

2323

24+
/**
25+
* When enabled, properties with default values are omitted from the output.
26+
*/
2427
public function skipDefaults(bool $value = true): void
2528
{
2629
$this->skipDefaults = $value;
@@ -65,7 +68,10 @@ public function processMultiple(Schema $schema, array $dataset): mixed
6568
}
6669

6770

68-
/** @return list<string> */
71+
/**
72+
* Returns all deprecation warnings collected during the last processing run.
73+
* @return list<string>
74+
*/
6975
public function getWarnings(): array
7076
{
7177
$res = [];

0 commit comments

Comments
 (0)