Skip to content

Commit 4944caf

Browse files
committed
fix phpstan
1 parent 435665f commit 4944caf

16 files changed

+55
-68
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"psr/simple-cache": "^3.0"
2626
},
2727
"require-dev" : {
28-
"phpstan/phpstan": "^2.0.2",
28+
"phpstan/phpstan": "^2.0.2",
2929
"friendsofphp/php-cs-fixer": "^3.0",
3030
"mockery/mockery": "^1.6",
3131
"phpbench/phpbench": "^1.2",

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
parameters:
22
level: 5
3-
mockery:
4-
class: DataCollection
5-
mockClass: Mockery\MockInterface
63
treatPhpDocTypesAsCertain: false
74
paths:
85
- src

src/Annotations/Faker/FakerMethod.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use ReflectionClass;
1111
use ReflectionException;
1212
use ReflectionMethod;
13+
use ReflectionNamedType;
1314
use ReflectionParameter;
15+
use RuntimeException;
1416

1517
#[Attribute(Attribute::TARGET_PROPERTY)]
1618
class FakerMethod implements FakerCastInterface
@@ -30,11 +32,11 @@ public function __construct(
3032
public function resolve(DataCollection $collection): mixed
3133
{
3234
if (!class_exists($this->className)) {
33-
throw new Exception("Class $this->className not found");
35+
throw new RuntimeException("Class $this->className not found");
3436
}
3537

3638
if (!method_exists($this->className, $this->methodName)) {
37-
throw new Exception("Method $this->methodName not found from class $this->className");
39+
throw new RuntimeException("Method $this->methodName not found from class $this->className");
3840
}
3941

4042
$instance = $this->createInstanceWithResolvedConstructor($this->className);
@@ -65,7 +67,7 @@ private function createInstanceWithResolvedConstructor(string $className, array
6567
{
6668
if (in_array($className, $dependencyChain, true)) {
6769
$chain = implode(' -> ', array_merge($dependencyChain, [$className]));
68-
throw new Exception("Circular dependency detected: $chain");
70+
throw new RuntimeException("Circular dependency detected: $chain");
6971
}
7072

7173
$dependencyChain[] = $className;
@@ -79,14 +81,20 @@ private function createInstanceWithResolvedConstructor(string $className, array
7981

8082
return $reflectionClass->newInstanceArgs(array_map(function ($param) use ($dependencyChain) {
8183
$paramType = $param->getType();
82-
$name = $paramType->getName();
84+
85+
if(!$paramType instanceof ReflectionNamedType){
86+
throw new \http\Exception\RuntimeException("$paramType is not ReflectionNamedType");
87+
}
88+
89+
$typeName = $paramType->getName();
90+
8391
return match(true) {
84-
!$paramType->isBuiltin() && class_exists($name) => $this->createInstanceWithResolvedConstructor($name, $dependencyChain),
92+
!$paramType->isBuiltin() && class_exists($typeName) => $this->createInstanceWithResolvedConstructor($typeName, $dependencyChain),
8593
$param->isDefaultValueAvailable() => $param->getDefaultValue(),
8694
default => null,
8795
};
8896

89-
}, $constructor->getParameters()));
97+
}, $constructor?->getParameters()));
9098
}
9199

92100
/**
@@ -103,7 +111,12 @@ private function resolveMethodParameters(ReflectionMethod $method): array
103111

104112
return array_map(function (ReflectionParameter $param) {
105113
$paramType = $param->getType();
106-
$name = $param->getName();
114+
115+
if(!$paramType instanceof ReflectionNamedType){
116+
throw new \http\Exception\RuntimeException("$paramType is not ReflectionNamedType");
117+
}
118+
119+
$name = $param->getName();
107120
$typeName = $paramType->getName();
108121

109122
return match(true) {
@@ -134,7 +147,7 @@ private function extractNestedValue(mixed $result, string $path): mixed
134147
} elseif (is_object($result) && property_exists($result, $key)) {
135148
$result = $result->$key;
136149
} else {
137-
throw new Exception("Unable to extract path '$path' from result");
150+
throw new RuntimeException("Unable to extract path '$path' from result");
138151
}
139152
}
140153
return $result;

src/Annotations/Input/InputDateFormat.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Attribute;
1111
use DateInvalidTimeZoneException;
1212
use DateTime;
13+
use DateTimeImmutable;
1314
use DateTimeInterface;
1415
use DateTimeZone;
1516

@@ -29,18 +30,27 @@ public function match(mixed $value, DataCollection $collection, InputValueContex
2930
return is_string($value) || is_numeric($value);
3031
}
3132

33+
34+
3235
/**
3336
* @throws DateInvalidTimeZoneException
3437
*/
3538
public function resolve(mixed $value, DataCollection $collection, InputValueContext $context): string|DateTime
3639
{
3740
$timezone = $this->timezone ? new DateTimeZone($this->timezone) : null;
41+
$types = $collection->getTypes();
42+
if (!$types || count($types) !== 1) {
43+
$dateTime = DateTime::createFromFormat($this->inputFormat, (string)$value, $timezone);
44+
return $dateTime !== false ? $dateTime->format($this->outFormat) : (string)$value;
45+
}
46+
47+
$className = current($types)->className;
3848

3949
if (!$this->outFormat
40-
&& count($collection->getTypes()) === 1
41-
&& is_subclass_of(current($collection->getTypes())?->className, DateTimeInterface::class)
50+
&& in_array($className, [DateTime::class, DateTimeImmutable::class], true)
51+
&& method_exists($className, 'createFromFormat')
4252
) {
43-
return (current($collection->getTypes())?->className)::createFromFormat($this->inputFormat, (string)$value, $timezone);
53+
return $className::createFromFormat($this->inputFormat, (string)$value, $timezone);
4454
}
4555

4656
$dateTime = DateTime::createFromFormat($this->inputFormat, (string)$value, $timezone);

src/Casts/InputValue/InputValueEnumCast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Astral\Serialize\Enums\TypeKindEnum;
99
use Astral\Serialize\Exceptions\ValueCastError;
1010
use Astral\Serialize\Support\Collections\DataCollection;
11+
use Astral\Serialize\Support\Collections\TypeCollection;
1112
use Astral\Serialize\Support\Context\InputValueContext;
1213
use BackedEnum;
1314
use UnitEnum;
@@ -24,7 +25,6 @@ public function match(mixed $value, DataCollection $collection, InputValueContex
2425
*/
2526
public function resolve(mixed $value, DataCollection $collection, InputValueContext $context): UnitEnum|string
2627
{
27-
2828
$types = $collection->getTypes();
2929
foreach ($types as $type) {
3030
if ($type->kind !== TypeKindEnum::ENUM) {
@@ -41,7 +41,7 @@ public function resolve(mixed $value, DataCollection $collection, InputValueCont
4141
sprintf(
4242
'Enum value "%s" not found in EnumClass: %s',
4343
$value,
44-
current($types)?->className
44+
current($types)->className
4545
)
4646
);
4747
}

src/Enums/TypeKindEnum.php

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

33
namespace Astral\Serialize\Enums;
44

5+
use http\Exception\RuntimeException;
6+
57
enum TypeKindEnum
68
{
79
case MIXED;
@@ -63,6 +65,7 @@ public static function getNameTo(string $type, ?string $className = null): self
6365
'array' => self::ARRAY,
6466
'object' => self::OBJECT,
6567
'mixed' => self::MIXED,
68+
default => throw new RuntimeException("not found type $type"),
6669
};
6770
}
6871
}

src/Faker/FakerCastResolver.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ public function resolve(DataCollection $dataCollection): mixed
3131
*/
3232
private function resolveCast(object $cast, DataCollection $dataCollection): mixed
3333
{
34-
if (!is_object($cast)) {
35-
throw new InvalidArgumentException(sprintf(
36-
'Expected an object, but got %s.',
37-
gettype($cast)
38-
));
39-
}
40-
4134
if (is_subclass_of($cast, FakerCastInterface::class)) {
4235
return $cast->resolve($dataCollection);
4336
}

src/Resolvers/Casts/DataCollectionCastResolver.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ public function resolve(DataCollection $dataCollection): void
4040
*/
4141
private function resolveCast(object $cast, DataCollection $dataCollection): void
4242
{
43-
if (!is_object($cast)) {
44-
throw new InvalidArgumentException(sprintf(
45-
'Expected an object, but got %s.',
46-
gettype($cast)
47-
));
48-
}
49-
5043
if (is_subclass_of($cast, DataCollectionCastInterface::class)) {
5144
$cast->resolve($dataCollection);
5245
}

src/Resolvers/Casts/InputValueCastResolver.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ private function applyCastsByConfigManager(mixed $value, DataCollection $collect
5353
*/
5454
private function applyCast(object $cast, DataCollection $collection, mixed $value, InputValueContext $context): mixed
5555
{
56-
if (!is_object($cast)) {
57-
throw new InvalidArgumentException(sprintf(
58-
'Expected an object, but got %s.',
59-
gettype($cast)
60-
));
61-
}
62-
6356
if (!is_subclass_of($cast, InputValueCastInterface::class)) {
6457
return $value;
6558
}

src/Resolvers/Casts/OutputCastResolver.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ private function applyCastsByConfigManager(mixed $value, DataCollection $collect
5353
*/
5454
private function applyCast(object $cast, DataCollection $collection, mixed $value, OutContext $context): mixed
5555
{
56-
if (!is_object($cast)) {
57-
throw new InvalidArgumentException(sprintf(
58-
'Expected an object, but got %s.',
59-
gettype($cast)
60-
));
61-
}
62-
6356
if (!is_subclass_of($cast, OutValueCastInterface::class)) {
6457
return $value;
6558
}

0 commit comments

Comments
 (0)