Skip to content

Commit 5e1c858

Browse files
committed
fix phpstan RC2
1 parent ab7f5d1 commit 5e1c858

File tree

8 files changed

+16
-556
lines changed

8 files changed

+16
-556
lines changed

src/OpenApi/Annotations/RequestBody.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class RequestBody
1212
{
1313
public function __construct(
14-
/** @var class-string $className */
14+
/** @var class-string|string $className */
1515
public string $className = '',
1616
public ContentTypeEnum $contentType = ContentTypeEnum::JSON,
1717
public array|null $group = null

src/OpenApi/Collections/OpenApiCollection.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Astral\Serialize\Support\Factories\ContextFactory;
2020
use Psr\SimpleCache\InvalidArgumentException;
2121
use ReflectionMethod;
22+
use ReflectionNamedType;
2223

2324
class OpenApiCollection
2425
{
@@ -73,7 +74,8 @@ public function buildRequestBodyByParameters(): RequestBodyStorage
7374
{
7475
$openAPIRequestBody = new RequestBodyStorage(ContentTypeEnum::JSON);
7576
$methodParam = $this->reflectionMethod->getParameters()[0] ?? null;
76-
$requestBodyClass = $methodParam ? $methodParam->getType()?->getName() : '';
77+
$type = $methodParam?->getType();
78+
$requestBodyClass = $type instanceof ReflectionNamedType ? $type->getName() : '';
7779
if (is_subclass_of($requestBodyClass, Serialize::class)) {
7880
$schemaStorage = (new SchemaStorage())->build($this->buildRequestBodyParameterCollections($requestBodyClass),$node);
7981
$openAPIRequestBody->withParameter($schemaStorage);
@@ -96,7 +98,7 @@ public function buildResponse(): ResponseStorage
9698
/**
9799
* @param string $className
98100
* @param array $groups
99-
* @return array<string, ParameterCollection>
101+
* @return array<ParameterCollection>
100102
* @throws InvalidArgumentException
101103
*/
102104
public function buildRequestBodyParameterCollections(string $className, array $groups = ['default']): array
@@ -111,9 +113,9 @@ public function buildRequestBodyParameterCollections(string $className, array $g
111113
$vol = new ParameterCollection(
112114
className: $className,
113115
name: current($property->getInputNamesByGroups($groups,$className)),
114-
descriptions: '',
115116
types: $property->getTypes(),
116117
type: ParameterTypeEnum::getByTypes($property->getTypes()),
118+
descriptions: '',
117119
required: !$property->isNullable(),
118120
ignore: false,
119121
);
@@ -132,7 +134,7 @@ className: $className,
132134
}
133135

134136
/**
135-
* @return array<string, ParameterCollection>
137+
* @return array<ParameterCollection>
136138
* @throws InvalidArgumentException
137139
*/
138140
public function buildResponseParameterCollections(): array
@@ -157,10 +159,10 @@ public function buildResponseParameterCollections(): array
157159
foreach ($properties as $property){
158160
$vol = new ParameterCollection(
159161
className: $responseClass,
160-
name:current($property->getOutNamesByGroups($groups,$responseClass)),
161-
descriptions: '',
162+
name: current($property->getOutNamesByGroups($groups,$responseClass)),
162163
types: $property->getTypes(),
163164
type: ParameterTypeEnum::getByTypes($property->getTypes()),
165+
descriptions: '',
164166
required: !$property->isNullable(),
165167
ignore: false,
166168
);

src/OpenApi/Collections/ParameterChildrenCollection.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/OpenApi/Collections/ParameterCollection.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,17 @@ class ParameterCollection
1313
{
1414
public function __construct(
1515
public string $className,
16-
/** @var string 元素变量名 */
1716
public string $name,
18-
/** @var string descriptions */
19-
public string $descriptions = '',
2017
/** @var TypeCollection[] $types */
2118
public array $types,
2219
public ParameterTypeEnum $type,
23-
/** @var mixed 示例值 */
20+
public string $descriptions = '',
2421
public mixed $example = '',
25-
/** @var bool 是否必填 */
2622
public bool $required = false,
27-
/** @var bool 是否忽略显示 */
2823
public bool $ignore = false,
2924
/** @var array<ParameterCollection[]> $children */
3025
public array $children = [],
3126
){
3227
}
33-
34-
public function addChildren(array $collections,ParameterTypeEnum $type = ParameterTypeEnum::STRING): void
35-
{
36-
$children = new ParameterChildrenCollection();
37-
$children->type = $type;
38-
$children->children = $collections;
39-
$this->children[] = $children;
40-
}
4128
}
4229

src/OpenApi/Enum/ParameterTypeEnum.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ public function isOf(): bool
3232
return $this === self::ONE_OF || $this === self::ANY_OF || $this === self::ALL_OF;
3333
}
3434

35-
public static function getBaseEnumByTypeKindEnum(TypeCollection $collection, string $className = null): ?ParameterTypeEnum
35+
public static function getBaseEnumByTypeKindEnum(TypeCollection $collection): ?ParameterTypeEnum
3636
{
3737
return match (true){
38-
$collection->kind === TypeKindEnum::STRING && !$className => self::STRING,
39-
$collection->kind === TypeKindEnum::INT && !$className => self::INTEGER,
40-
$collection->kind === TypeKindEnum::FLOAT && !$className => self::NUMBER,
41-
$collection->kind === TypeKindEnum::BOOLEAN && !$className=> self::BOOLEAN,
42-
in_array($collection->kind, [TypeKindEnum::CLASS_OBJECT, TypeKindEnum::OBJECT], true) && $className === $collection->className => self::OBJECT,
43-
in_array($collection->kind, [TypeKindEnum::ARRAY, TypeKindEnum::COLLECT_SINGLE_OBJECT, TypeKindEnum::COLLECT_UNION_OBJECT], true) && $className === $collection->className => self::ARRAY,
38+
$collection->kind === TypeKindEnum::STRING => self::STRING,
39+
$collection->kind === TypeKindEnum::INT => self::INTEGER,
40+
$collection->kind === TypeKindEnum::FLOAT => self::NUMBER,
41+
$collection->kind === TypeKindEnum::BOOLEAN => self::BOOLEAN,
4442
default => null,
4543
};
4644
}

src/OpenApi/Handler/Handler.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ protected function scanFolderRecursively(string $folder, string $namespace): voi
129129

130130
// 如果类尚未加载,则尝试 include
131131
if (! class_exists($className)) {
132-
include_once $path;
133-
@ob_clean();
134-
if (! class_exists($className)) {
135-
// 如果 include 后仍然不存在该类,跳过
136-
continue;
137-
}
132+
continue;
138133
}
139134

140135
// 调用子类实现的 buildByClass

src/OpenApi/Handler/ParserPartaker.php

Lines changed: 0 additions & 191 deletions
This file was deleted.

0 commit comments

Comments
 (0)