Skip to content

Commit 3f686df

Browse files
committed
Add mago results
1 parent 837cfbd commit 3f686df

30 files changed

Lines changed: 474 additions & 22 deletions

‎libs/component/diagnostics/src/Printer/Value/PublicValuePrinter.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace TypeLang\Mapper\Diagnostics\Printer\Value;
66

7+
use TypeLang\Mapper\Diagnostics\RedactedValue;
8+
79
/**
810
* Prints a value for a reader outside of the application: an object as
911
* `object`, a case of an enum as its value or name, a resource as
10-
* `resource`. Scalars and arrays are printed by the inner printer.
12+
* `resource`, a hidden value as `*redacted*`. Scalars and arrays are
13+
* printed by the inner printer.
1114
*/
1215
final class PublicValuePrinter implements ValuePrinterInterface
1316
{
@@ -18,6 +21,7 @@ public function __construct(
1821
public function print(mixed $value): string
1922
{
2023
return match (true) {
24+
$value instanceof RedactedValue => (string) $value,
2125
$value instanceof \BackedEnum => $this->printer->print($value->value),
2226
$value instanceof \UnitEnum => $this->printer->print($value->name),
2327
\is_object($value) => 'object',

‎libs/component/diagnostics/src/Printer/Value/ShortValuePrinter.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace TypeLang\Mapper\Diagnostics\Printer\Value;
66

7+
use TypeLang\Mapper\Diagnostics\RedactedValue;
8+
79
/**
810
* Prints a value for a developer: scalars and strings by value, arrays and
9-
* objects by kind (`array(3)`, `object(App\User)`).
11+
* objects by kind (`array(3)`, `object(App\User)`), a hidden value as
12+
* `*redacted*`.
1013
*/
1114
final class ShortValuePrinter implements ValuePrinterInterface
1215
{
@@ -35,6 +38,7 @@ public function print(mixed $value): string
3538
\is_float($value) => self::printFloat($value),
3639
\is_string($value) => $this->printString($value),
3740
\is_array($value) => \sprintf('array(%d)', \count($value)),
41+
$value instanceof RedactedValue => (string) $value,
3842
$value instanceof \UnitEnum => $value::class . '::' . $value->name,
3943
\is_object($value) => \sprintf('object(%s)', \get_debug_type($value)),
4044
\is_resource($value) => \sprintf('resource(%s)', \get_resource_type($value)),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Diagnostics;
6+
7+
/**
8+
* Rejected value of a sensitive field in place of the value itself.
9+
*/
10+
final class RedactedValue implements \Stringable
11+
{
12+
/**
13+
* @return non-empty-string
14+
*/
15+
public function __toString(): string
16+
{
17+
return '*redacted*';
18+
}
19+
}

‎libs/component/diagnostics/src/Violation.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,18 @@ public function withExpected(?TypeNode $expected): self
119119
$this->cause,
120120
);
121121
}
122+
123+
public function withValue(mixed $value): self
124+
{
125+
return new self(
126+
$this->code,
127+
$this->parameters,
128+
$this->path,
129+
$value,
130+
$this->template,
131+
$this->declared,
132+
$this->expected,
133+
$this->cause,
134+
);
135+
}
122136
}

‎libs/component/diagnostics/tests/Printer/Value/PublicValuePrinterTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use TypeLang\Mapper\Diagnostics\Printer\Value\PublicValuePrinter;
99
use TypeLang\Mapper\Diagnostics\Printer\Value\ShortValuePrinter;
10+
use TypeLang\Mapper\Diagnostics\RedactedValue;
1011
use TypeLang\Mapper\Diagnostics\Tests\Stub\PriorityStub;
1112
use TypeLang\Mapper\Diagnostics\Tests\Stub\StatusStub;
1213
use TypeLang\Mapper\Diagnostics\Tests\Stub\UserStub;
@@ -27,6 +28,7 @@ public static function valueDataProvider(): iterable
2728
yield 'int' => [42, '42'];
2829
yield 'array' => [[1, 2], 'array(2)'];
2930
yield 'null' => [null, 'null'];
31+
yield 'redacted' => [new RedactedValue(), '*redacted*'];
3032
}
3133

3234
#[DataProvider('valueDataProvider')]

‎libs/component/diagnostics/tests/Printer/Value/ShortValuePrinterTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\DataProvider;
88
use TypeLang\Mapper\Diagnostics\Printer\Value\ShortValuePrinter;
9+
use TypeLang\Mapper\Diagnostics\RedactedValue;
910
use TypeLang\Mapper\Diagnostics\Tests\Stub\PriorityStub;
1011
use TypeLang\Mapper\Diagnostics\Tests\Stub\StatusStub;
1112
use TypeLang\Mapper\Diagnostics\Tests\Stub\StringableStub;
@@ -39,6 +40,11 @@ public function testScalarIsPrintedByValue(mixed $value, string $expected): void
3940
self::assertSame($expected, (new ShortValuePrinter())->print($value));
4041
}
4142

43+
public function testRedactedValueIsPrintedWithoutQuotes(): void
44+
{
45+
self::assertSame('*redacted*', (new ShortValuePrinter())->print(new RedactedValue()));
46+
}
47+
4248
/**
4349
* @return iterable<non-empty-string, array{string, non-empty-string}>
4450
*/

‎libs/component/mapping/src/Draft/MemberDraft.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ final class MemberDraft
9494
*/
9595
public bool $isOptional = false;
9696

97+
/**
98+
* Whether a rejected value of the field of the denormalization is
99+
* hidden from the errors
100+
*
101+
* @readonly
102+
*
103+
* @phpstan-readonly-allow-private-mutation
104+
*/
105+
public bool $isSensitive = false;
106+
97107
/**
98108
* Conditions the field of the normalization is left out of the data
99109
* under, any of them is enough
@@ -184,6 +194,7 @@ public function toInputField(): ?InputFieldMetadata
184194
isOptional: $this->isOptional || $this->default !== null,
185195
typeErrorMessage: $this->typeErrorMessage,
186196
undefinedErrorMessage: $this->undefinedErrorMessage,
197+
isSensitive: $this->isSensitive,
187198
);
188199
}
189200

@@ -339,6 +350,14 @@ public function withOptional(bool $isOptional): self
339350
return $self;
340351
}
341352

353+
public function withSensitive(bool $isSensitive): self
354+
{
355+
$self = clone $this;
356+
$self->isSensitive = $isSensitive;
357+
358+
return $self;
359+
}
360+
342361
/**
343362
* @param non-empty-string $message
344363
*/

‎libs/component/mapping/src/Metadata/Field/InputFieldMetadata.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function __construct(
5656
* @var non-empty-string|null
5757
*/
5858
public readonly ?string $undefinedErrorMessage = null,
59+
/**
60+
* Whether a rejected value is hidden from the errors
61+
*/
62+
public readonly bool $isSensitive = false,
5963
) {
6064
parent::__construct($name, $access, $type);
6165
}

‎libs/component/mapping/src/Reader/Access/AccessFactory.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use TypeLang\Mapper\Mapping\Metadata\Access\SlotAccessMetadata;
1515
use TypeLang\Mapper\Mapping\Metadata\SourceInfo;
1616
use TypeLang\Mapper\Mapping\Metadata\TypeMetadata;
17+
use TypeLang\Mapper\Mapping\Reader\Option\SensitiveOption;
1718
use TypeLang\Mapper\Mapping\Reader\Reflection\ClassProperties;
1819
use TypeLang\Mapper\Mapping\Reader\Reflection\PropertyHooks;
1920
use TypeLang\Mapper\Mapping\Reader\Source\ErrorCollector;
@@ -152,7 +153,7 @@ public function createMethodAccess(
152153
/**
153154
* Returns the member of the method, {@see null} in case of it has no
154155
* direction. A setter with an optional parameter makes the field
155-
* optional.
156+
* optional, with a {@see \SensitiveParameter} the field is sensitive.
156157
*/
157158
public static function createMethodMember(
158159
\ReflectionMethod $method,
@@ -170,7 +171,9 @@ public static function createMethodMember(
170171
return $member;
171172
}
172173

173-
return $member->withOptional($parameter->isOptional());
174+
return $member
175+
->withOptional($parameter->isOptional())
176+
->withSensitive(SensitiveOption::isSensitiveParameter($parameter));
174177
}
175178

176179
public static function isGetter(\ReflectionMethod $method): bool

‎libs/component/mapping/src/Reader/Option/Options.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static function createDefault(TypeParserInterface $parser, ?ExpressionLan
5858
new DefaultOption(),
5959
$skip,
6060
$messages,
61+
new SensitiveOption(),
6162
],
6263
classes: [
6364
new DiscriminatorOption($parser),

0 commit comments

Comments
 (0)