Skip to content

Commit ca4921b

Browse files
committed
Apply coding standard
1 parent c717371 commit ca4921b

File tree

12 files changed

+111
-29
lines changed

12 files changed

+111
-29
lines changed

src/JsonMapper.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
use ReflectionParameter;
1515
use stdClass;
1616

17+
use function array_map;
18+
use function count;
19+
use function get_class;
20+
use function gettype;
21+
use function implode;
22+
use function in_array;
23+
use function is_array;
24+
use function is_float;
25+
use function is_int;
26+
use function is_string;
27+
use function json_decode;
28+
use function property_exists;
29+
use function sprintf;
30+
31+
use const JSON_THROW_ON_ERROR;
32+
1733
/**
1834
* @psalm-suppress MixedAssignment
1935
*/
@@ -167,7 +183,7 @@ private function getParameterValue(
167183
): mixed {
168184
$parameterType = $this->reflector->getParameterType($reflectionParameter);
169185

170-
if (!property_exists($jsonData, $jsonPropertyName)) {
186+
if (! property_exists($jsonData, $jsonPropertyName)) {
171187
if ($this->onMissingProperties === OnMissingProperties::SET_NULL) {
172188
if ($parameterType->allowsNull) {
173189
return null;
@@ -188,7 +204,7 @@ private function getParameterValue(
188204
OnMissingProperties::SET_NULL => 'The parameter does not allow null.',
189205
OnMissingProperties::SET_DEFAULT => 'The parameter does not have a default value.',
190206
OnMissingProperties::THROW_EXCEPTION => 'If you want to allow missing properties, change the $onMissingProperties option.',
191-
}
207+
},
192208
]);
193209
}
194210

@@ -328,11 +344,14 @@ private function getJsonObjectValue(
328344

329345
if (! $matches) {
330346
throw new JsonMapperException(
331-
"JSON object does not match any of the allowed PHP classes:\n" . implode("\n", array_map(
332-
fn (array $error) => sprintf(' - %s: %s', ...$error),
333-
$errors,
347+
"JSON object does not match any of the allowed PHP classes:\n" . implode(
348+
"\n",
349+
array_map(
350+
fn (array $error) => sprintf(' - %s: %s', ...$error),
351+
$errors,
352+
),
334353
),
335-
));
354+
);
336355
}
337356

338357
if (count($matches) === 1) {

src/JsonMapperException.php

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

77
use Exception;
88

9+
use function implode;
10+
use function is_string;
11+
912
final class JsonMapperException extends Exception
1013
{
1114
/**

src/NameMapper/CamelCaseToSnakeCaseMapper.php

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

77
use Brick\JsonMapper\NameMapper;
88

9+
use function preg_replace_callback;
10+
use function strtolower;
11+
912
final class CamelCaseToSnakeCaseMapper implements NameMapper
1013
{
1114
public function mapName(string $name): string

src/NameMapper/SnakeCaseToCamelCaseMapper.php

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

77
use Brick\JsonMapper\NameMapper;
88

9+
use function preg_replace_callback;
10+
use function strtoupper;
11+
912
final class SnakeCaseToCamelCaseMapper implements NameMapper
1013
{
1114
public function mapName(string $name): string

src/Reflection/Reflector.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222
use stdClass;
2323
use UnitEnum;
2424

25+
use function array_map;
26+
use function count;
27+
use function implode;
28+
use function in_array;
29+
use function is_a;
30+
use function is_string;
31+
use function preg_match;
32+
use function preg_match_all;
33+
use function preg_replace;
34+
use function sprintf;
35+
use function strtolower;
36+
37+
use const PREG_SET_ORDER;
38+
2539
/**
2640
* @internal This class is not part of the public API, and may change without notice.
2741
*/
@@ -50,13 +64,6 @@ final class Reflector
5064
'callable',
5165
];
5266

53-
public function __construct(
54-
public readonly bool $allowUntypedArrays = false,
55-
public readonly bool $allowUntypedObjects = false,
56-
public readonly bool $allowMixed = false,
57-
) {
58-
}
59-
6067
/**
6168
* Cache of parameter types, indexed by cache key.
6269
*
@@ -67,6 +74,13 @@ public function __construct(
6774
*/
6875
private array $parameterTypes = [];
6976

77+
public function __construct(
78+
public readonly bool $allowUntypedArrays = false,
79+
public readonly bool $allowUntypedObjects = false,
80+
public readonly bool $allowMixed = false,
81+
) {
82+
}
83+
7084
/**
7185
* @throws JsonMapperException
7286
*/
@@ -321,7 +335,7 @@ private function convertNamedType(string $namedType, bool $isReflection, Reflect
321335
'Parameter %s contains type "mixed" which is not allowed by default.',
322336
$this->getParameterNameWithDeclaringFunctionName($reflectionParameter),
323337
),
324-
'If you want to allow this, and receive the raw JSON value, set $allowMixed to true.'
338+
'If you want to allow this, and receive the raw JSON value, set $allowMixed to true.',
325339
]);
326340
}
327341

src/Reflection/Type/ArrayType.php

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

77
use Stringable;
88

9+
use function count;
10+
911
/**
1012
* @internal This class is not part of the public API, and may change without notice.
1113
*/

src/Reflection/Type/UnionType.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
use Brick\JsonMapper\JsonMapperException;
88
use Stringable;
99

10+
use function array_count_values;
11+
use function array_filter;
12+
use function array_map;
13+
use function array_unique;
14+
use function array_values;
15+
use function count;
16+
use function implode;
17+
use function in_array;
18+
use function sprintf;
19+
1020
/**
1121
* Represents a combination of supported types.
1222
* For simplicity, even a single type is represented as a union type with a single element.
@@ -16,13 +26,21 @@
1626
final class UnionType implements Stringable
1727
{
1828
public readonly bool $allowsInt;
29+
1930
public readonly bool $allowsFloat;
31+
2032
public readonly bool $allowsString;
33+
2134
public readonly bool $allowsTrue;
35+
2236
public readonly bool $allowsFalse;
37+
2338
public readonly bool $allowsNull;
39+
2440
public readonly bool $allowsRawArray;
41+
2542
public readonly bool $allowsRawObject;
43+
2644
public readonly bool $allowsMixed;
2745

2846
/**
@@ -146,6 +164,14 @@ public function __construct(
146164
}
147165
}
148166

167+
public function __toString(): string
168+
{
169+
return implode('|', array_map(
170+
fn (Stringable $value) => (string) $value,
171+
$this->types,
172+
));
173+
}
174+
149175
private function ensureNotEmpty(): void
150176
{
151177
if (! $this->types) {
@@ -183,12 +209,4 @@ private function filterTypes(string $className): array
183209
fn (SimpleType|ClassType|EnumType|ArrayType $type) => $type instanceof $className,
184210
));
185211
}
186-
187-
public function __toString(): string
188-
{
189-
return implode('|', array_map(
190-
fn (Stringable $value) => (string) $value,
191-
$this->types,
192-
));
193-
}
194212
}

src/Reflection/TypeParser.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
use Brick\JsonMapper\JsonMapperException;
88

9+
use function array_map;
10+
use function array_merge;
11+
use function count;
12+
use function implode;
13+
use function in_array;
14+
use function is_array;
15+
use function sprintf;
16+
917
/**
1018
* Parses the given string and returns a nested representation of the types.
1119
*
@@ -29,7 +37,6 @@ final class TypeParser
2937
{
3038
/**
3139
* @var TypeToken[]
32-
*
3340
* @psalm-var list<TypeToken>
3441
*/
3542
private array $tokens = [];
@@ -67,7 +74,7 @@ private function parseUnion(bool $nested): array
6774
{
6875
$values = [];
6976

70-
for (;;) {
77+
for (; ;) {
7178
$value = $this->parseValue();
7279

7380
if ($this->isNextToken('[]')) {
@@ -96,6 +103,7 @@ private function parseUnion(bool $nested): array
96103

97104
if ($peekToken->value === '|') {
98105
$this->advance();
106+
99107
continue;
100108
}
101109

@@ -186,7 +194,7 @@ private function isNextToken(string $value): bool
186194
*/
187195
private function failExpectation(array $expected, ?TypeToken $actual): never
188196
{
189-
$expected = implode(' or ' , array_map(
197+
$expected = implode(' or ', array_map(
190198
fn (string $value) => in_array($value, ['(', ')', '|', '[]'], true) ? "\"$value\"" : $value,
191199
$expected,
192200
));

src/Reflection/TypeTokenizer.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
use Brick\JsonMapper\JsonMapperException;
88

9+
use function is_int;
10+
use function preg_match_all;
11+
use function sprintf;
12+
13+
use const PREG_OFFSET_CAPTURE;
14+
use const PREG_SET_ORDER;
15+
916
/**
1017
* Splits a string into individual tokens.
1118
*
@@ -35,9 +42,9 @@ final class TypeTokenizer
3542
/**
3643
* @return TypeToken[]
3744
*
38-
* @psalm-return list<TypeToken>
39-
*
4045
* @throws JsonMapperException
46+
*
47+
* @psalm-return list<TypeToken>
4148
*/
4249
public static function tokenize(string $type): array
4350
{
@@ -64,7 +71,7 @@ public static function tokenize(string $type): array
6471
throw new JsonMapperException(match ($value) {
6572
'[' => sprintf('Char "[" is not followed by "]" at offset %d.', $offset),
6673
']' => sprintf('Char "]" is not preceded by "[" at offset %d.', $offset),
67-
default => sprintf('Unexpected "%s" at offset %d.', $value, $offset),
74+
default => sprintf('Unexpected "%s" at offset %d.', $value, $offset),
6875
});
6976
}
7077

tests/JsonMapperTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Brick\JsonMapper\Tests\Classes\Shop\OrderStatus;
1818
use Brick\JsonMapper\Tests\Classes\Shop\Person;
1919
use PHPUnit\Framework\TestCase;
20-
use stdClass;
2120

2221
final class JsonMapperTest extends TestCase
2322
{

0 commit comments

Comments
 (0)