Skip to content

Commit 0be4518

Browse files
author
=
committed
Fix codestyle
1 parent 408813a commit 0be4518

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/Schema/Exception/TypeMismatch.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
namespace League\OpenAPIValidation\Schema\Exception;
66

77
use function gettype;
8+
use function implode;
89
use function sprintf;
910

1011
// Validation for 'type' keyword failed against a given data
1112
class TypeMismatch extends KeywordMismatch
1213
{
1314
/**
14-
* @param mixed $value
15+
* @param string[] $expected
16+
* @param mixed $value
1517
*
1618
* @return TypeMismatch
1719
*/
1820
public static function becauseTypeDoesNotMatch(array $expected, $value): self
1921
{
20-
$exception = new self(sprintf("Value expected to be '%s', but '%s' given.", implode(', ', $expected), gettype($value)));
21-
$exception->data = $value;
22+
$exception = new self(sprintf("Value expected to be '%s', but '%s' given.", implode(', ', $expected), gettype($value)));
23+
$exception->data = $value;
2224
$exception->keyword = 'type';
2325

2426
return $exception;

src/Schema/Keywords/Nullable.php

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

77
use League\OpenAPIValidation\Schema\Exception\KeywordMismatch;
88

9+
use function in_array;
10+
use function is_string;
11+
912
class Nullable extends BaseKeyword
1013
{
1114
/**
@@ -17,16 +20,13 @@ class Nullable extends BaseKeyword
1720
*/
1821
public function validate($data, bool $nullable): void
1922
{
20-
if (! $nullable && ($data === null) && !$this->nullableByType()) {
23+
if (! $nullable && ($data === null) && ! $this->nullableByType()) {
2124
throw KeywordMismatch::fromKeyword('nullable', $data, 'Value cannot be null');
2225
}
2326
}
2427

25-
/**
26-
* @return bool
27-
*/
2828
public function nullableByType(): bool
2929
{
30-
return !is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
30+
return ! is_string($this->parentSchema->type) && in_array('null', $this->parentSchema->type);
3131
}
3232
}

src/Schema/Keywords/Type.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use League\OpenAPIValidation\Schema\Exception\TypeMismatch;
1212
use League\OpenAPIValidation\Schema\TypeFormats\FormatsContainer;
1313
use RuntimeException;
14+
use TypeError;
1415

1516
use function class_exists;
1617
use function is_array;
@@ -32,68 +33,78 @@ class Type extends BaseKeyword
3233
* An instance matches successfully if its primitive type is one of the
3334
* types defined by keyword. Recall: "number" includes "integer".
3435
*
35-
* @param mixed $data
36-
* @param string|array $types
36+
* @param mixed $data
37+
* @param string|string[] $types
3738
*
3839
* @throws TypeMismatch
3940
*/
4041
public function validate($data, $types, ?string $format = null): void
4142
{
42-
if (!is_array($types) && !is_string($types)) {
43-
throw new \TypeError('$types only can be array or string');
43+
if (! is_array($types) && ! is_string($types)) {
44+
throw new TypeError('$types only can be array or string');
4445
}
45-
if (!is_array($types)) {
46+
47+
if (! is_array($types)) {
4648
$types = [$types];
4749
}
50+
4851
$matchedType = false;
4952
foreach ($types as $type) {
5053
switch ($type) {
5154
case CebeType::OBJECT:
5255
if (! is_object($data) && ! (is_array($data) && ArrayHelper::isAssoc($data)) && $data !== []) {
5356
break;
5457
}
58+
5559
$matchedType = $type;
5660
break;
5761
case CebeType::ARRAY:
5862
if (! is_array($data) || ArrayHelper::isAssoc($data)) {
5963
break;
6064
}
65+
6166
$matchedType = $type;
6267
break;
6368
case CebeType::BOOLEAN:
6469
if (! is_bool($data)) {
6570
break;
6671
}
72+
6773
$matchedType = $type;
6874
break;
6975
case CebeType::NUMBER:
7076
if (is_string($data) || ! is_numeric($data)) {
7177
break;
7278
}
79+
7380
$matchedType = $type;
7481
break;
7582
case CebeType::INTEGER:
7683
if (! is_int($data)) {
7784
break;
7885
}
86+
7987
$matchedType = $type;
8088
break;
8189
case CebeType::STRING:
8290
if (! is_string($data)) {
8391
break;
8492
}
93+
8594
$matchedType = $type;
8695
break;
8796
case CebeType::NULL:
88-
if (! is_null($data)) {
97+
if ($data !== null) {
8998
break;
9099
}
100+
91101
$matchedType = $type;
92102
break;
93103
default:
94104
throw InvalidSchema::becauseTypeIsNotKnown($type);
95105
}
96106
}
107+
97108
if ($matchedType === false) {
98109
throw TypeMismatch::becauseTypeDoesNotMatch($types, $data);
99110
}

0 commit comments

Comments
 (0)