Skip to content

Commit 37e27d0

Browse files
authored
chore: Make types final and various CI updates (#8)
1 parent 08111bb commit 37e27d0

36 files changed

+429
-432
lines changed

phpstan.dist.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ includes:
22
- vendor/phpstan/phpstan-strict-rules/rules.neon
33

44
parameters:
5-
level: 9
5+
level: 10
66
paths:
77
- src
88
tmpDir: .phpstan-cache
9+
checkBenevolentUnionTypes: true

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
instanceOf: true,
2424
earlyReturn: true,
2525
strictBooleans: true,
26+
naming: true,
2627
)
2728
->withFluentCallNewLine();

src/Converters/ClassConverter.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function __construct(
3636

3737
public function convert(): ObjectSchema
3838
{
39-
$schema = new ObjectSchema();
39+
$objectSchema = new ObjectSchema();
4040

4141
// Get the description from the doc parser
4242
$description = $this->getDocParser($this->reflection)?->description() ?? null;
4343

4444
// Add the description to the schema if it exists
4545
if ($description !== null) {
46-
$schema->description($description);
46+
$objectSchema->description($description);
4747
}
4848

4949
$properties = $this->reflection->getProperties(
@@ -52,26 +52,26 @@ public function convert(): ObjectSchema
5252

5353
// Add the properties to the object schema
5454
foreach ($properties as $property) {
55-
$schema->properties(self::getSchemaFromReflectionProperty($property));
55+
$objectSchema->properties(self::getSchemaFromReflectionProperty($property));
5656
}
5757

58-
return $schema;
58+
return $objectSchema;
5959
}
6060

6161
/**
6262
* Create a schema from a given type.
6363
*/
6464
protected function getSchemaFromReflectionProperty(
65-
ReflectionProperty $property,
65+
ReflectionProperty $reflectionProperty,
6666
): Schema {
67-
$type = $property->getType();
67+
$type = $reflectionProperty->getType();
6868

6969
// @phpstan-ignore argument.type
7070
$schema = self::getSchemaFromReflectionType($type);
7171

72-
$schema->title($property->getName());
72+
$schema->title($reflectionProperty->getName());
7373

74-
$variable = $this->getDocParser($property)?->variable();
74+
$variable = $this->getDocParser($reflectionProperty)?->variable();
7575

7676
// Add the description to the schema if it exists
7777
if ($variable?->description !== null) {
@@ -82,8 +82,8 @@ protected function getSchemaFromReflectionProperty(
8282
$schema->nullable();
8383
}
8484

85-
if ($property->hasDefaultValue()) {
86-
$defaultValue = $property->getDefaultValue();
85+
if ($reflectionProperty->hasDefaultValue()) {
86+
$defaultValue = $reflectionProperty->getDefaultValue();
8787

8888
// If the default value is a backed enum, use its value
8989
if ($defaultValue instanceof BackedEnum) {
@@ -100,9 +100,9 @@ protected function getSchemaFromReflectionProperty(
100100
$typeName = $type->getName();
101101

102102
if (enum_exists($typeName)) {
103-
$reflection = new ReflectionEnum($typeName);
103+
$reflectionEnum = new ReflectionEnum($typeName);
104104

105-
if ($reflection->isBacked()) {
105+
if ($reflectionEnum->isBacked()) {
106106
/** @var non-empty-array<int, string|int> $values */
107107
$values = array_column($typeName::cases(), 'value');
108108
$schema->enum($values);

src/Converters/ClosureConverter.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,44 @@ public function __construct(
3131

3232
public function convert(): ObjectSchema
3333
{
34-
$schema = new ObjectSchema();
34+
$objectSchema = new ObjectSchema();
3535

3636
// Get the description from the doc parser
3737
$description = $this->getDocParser()?->description() ?? null;
3838

3939
// Add the description to the schema if it exists
4040
if ($description !== null) {
41-
$schema->description($description);
41+
$objectSchema->description($description);
4242
}
4343

4444
// Get the parameters from the doc parser
4545
$params = $this->getDocParser()?->params();
4646

4747
// Add the parameters to the objectschema
4848
foreach ($this->reflection->getParameters() as $parameter) {
49-
$schema->properties(self::getSchemaFromReflectionParameter($parameter, $params));
49+
$objectSchema->properties(self::getSchemaFromReflectionParameter($parameter, $params));
5050
}
5151

52-
return $schema;
52+
return $objectSchema;
5353
}
5454

5555
/**
5656
* Create a schema from a given type.
5757
*
58-
* @param \Cortex\JsonSchema\Support\NodeCollection<array-key, \Cortex\JsonSchema\Support\NodeData> $docParams
58+
* @param \Cortex\JsonSchema\Support\NodeCollection<array-key, \Cortex\JsonSchema\Support\NodeData> $nodeCollection
5959
*/
6060
protected function getSchemaFromReflectionParameter(
61-
ReflectionParameter $parameter,
62-
?NodeCollection $docParams = null,
61+
ReflectionParameter $reflectionParameter,
62+
?NodeCollection $nodeCollection = null,
6363
): Schema {
64-
$type = $parameter->getType();
64+
$type = $reflectionParameter->getType();
6565

6666
// @phpstan-ignore argument.type
6767
$schema = self::getSchemaFromReflectionType($type);
6868

69-
$schema->title($parameter->getName());
69+
$schema->title($reflectionParameter->getName());
7070

71-
$docParam = $docParams?->get($parameter->getName());
71+
$docParam = $nodeCollection?->get($reflectionParameter->getName());
7272

7373
// Add the description to the schema if it exists
7474
if ($docParam?->description !== null) {
@@ -79,8 +79,8 @@ protected function getSchemaFromReflectionParameter(
7979
$schema->nullable();
8080
}
8181

82-
if ($parameter->isDefaultValueAvailable() && ! $parameter->isDefaultValueConstant()) {
83-
$defaultValue = $parameter->getDefaultValue();
82+
if ($reflectionParameter->isDefaultValueAvailable() && ! $reflectionParameter->isDefaultValueConstant()) {
83+
$defaultValue = $reflectionParameter->getDefaultValue();
8484

8585
// If the default value is a backed enum, use its value
8686
if ($defaultValue instanceof BackedEnum) {
@@ -90,7 +90,7 @@ protected function getSchemaFromReflectionParameter(
9090
$schema->default($defaultValue);
9191
}
9292

93-
if (! $parameter->isOptional()) {
93+
if (! $reflectionParameter->isOptional()) {
9494
$schema->required();
9595
}
9696

@@ -99,9 +99,9 @@ protected function getSchemaFromReflectionParameter(
9999
$typeName = $type->getName();
100100

101101
if (enum_exists($typeName)) {
102-
$reflection = new ReflectionEnum($typeName);
102+
$reflectionEnum = new ReflectionEnum($typeName);
103103

104-
if ($reflection->isBacked()) {
104+
if ($reflectionEnum->isBacked()) {
105105
/** @var non-empty-array<int, string|int> $values */
106106
$values = array_column($typeName::cases(), 'value');
107107
$schema->enum($values);

src/Converters/Concerns/InteractsWithTypes.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ protected static function getSchemaFromReflectionType(
2424
$schemaTypes = match (true) {
2525
$type instanceof ReflectionUnionType, $type instanceof ReflectionIntersectionType => array_map(
2626
// @phpstan-ignore argument.type
27-
fn(ReflectionNamedType $t): SchemaType => self::resolveSchemaType($t),
27+
fn(ReflectionNamedType $reflectionNamedType): SchemaType => self::resolveSchemaType(
28+
$reflectionNamedType,
29+
),
2830
$type->getTypes(),
2931
),
3032
// If the parameter is not typed or explicitly typed as mixed, we use all schema types
@@ -40,16 +42,16 @@ protected static function getSchemaFromReflectionType(
4042
/**
4143
* Resolve the schema type from the given reflection type.
4244
*/
43-
protected static function resolveSchemaType(ReflectionNamedType $type): SchemaType
45+
protected static function resolveSchemaType(ReflectionNamedType $reflectionNamedType): SchemaType
4446
{
45-
$typeName = $type->getName();
47+
$typeName = $reflectionNamedType->getName();
4648

4749
if (enum_exists($typeName)) {
48-
$reflection = new ReflectionEnum($typeName);
49-
$typeName = $reflection->getBackingType()?->getName();
50+
$reflectionEnum = new ReflectionEnum($typeName);
51+
$typeName = $reflectionEnum->getBackingType()?->getName();
5052

5153
if ($typeName === null) {
52-
throw new SchemaException('Enum type has no backing type: ' . $reflection->getName());
54+
throw new SchemaException('Enum type has no backing type: ' . $reflectionEnum->getName());
5355
}
5456
}
5557

src/Exceptions/SchemaException.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ class SchemaException extends Exception
1212
{
1313
protected ValidationError $error;
1414

15-
public static function failedValidation(ValidationError $error): self
15+
public static function failedValidation(ValidationError $validationError): self
1616
{
1717
$exception = new self(
18-
(new ErrorFormatter())->formatErrorMessage($error),
18+
(new ErrorFormatter())->formatErrorMessage($validationError),
1919
);
2020

21-
$exception->setError($error);
21+
$exception->setError($validationError);
2222

2323
return $exception;
2424
}
2525

26-
public function setError(ValidationError $error): void
26+
public function setError(ValidationError $validationError): void
2727
{
28-
$this->error = $error;
28+
$this->error = $validationError;
2929
}
3030

3131
public function getError(): ValidationError
@@ -34,10 +34,15 @@ public function getError(): ValidationError
3434
}
3535

3636
/**
37-
* @return array<string, string>
37+
* @return array<string, string|array<string, string>>
3838
*/
3939
public function getErrors(): array
4040
{
41-
return (new ErrorFormatter())->format($this->error);
41+
$errorFormatter = new ErrorFormatter();
42+
43+
/** @var array<string, string|array<string, string>> $errors */
44+
$errors = $errorFormatter->format($this->error);
45+
46+
return $errors;
4247
}
4348
}

src/Support/DocParser.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public function params(): NodeCollection
6565
public function variable(): ?NodeData
6666
{
6767
$vars = array_map(
68-
static fn(VarTagValueNode $var): NodeData => new NodeData(
69-
name: ltrim($var->variableName, '$'),
70-
types: self::mapValueNodeToTypes($var),
71-
description: $var->description === '' ? null : $var->description,
68+
static fn(VarTagValueNode $varTagValueNode): NodeData => new NodeData(
69+
name: ltrim($varTagValueNode->variableName, '$'),
70+
types: self::mapValueNodeToTypes($varTagValueNode),
71+
description: $varTagValueNode->description === '' ? null : $varTagValueNode->description,
7272
),
7373
$this->parse()->getVarTagValues(),
7474
);
@@ -91,7 +91,7 @@ protected static function mapValueNodeToTypes(
9191

9292
return match (true) {
9393
$param->type instanceof UnionTypeNode => array_map(
94-
fn(TypeNode $type): string => (string) $type,
94+
fn(TypeNode $typeNode): string => (string) $typeNode,
9595
$param->type->types,
9696
),
9797
$param->type instanceof NullableTypeNode => [
@@ -114,11 +114,11 @@ protected function parse(): PhpDocNode
114114

115115
protected function getParser(): PhpDocParser
116116
{
117-
$config = $this->getConfig();
118-
$constExprParser = new ConstExprParser($config);
119-
$typeParser = new TypeParser($config, $constExprParser);
117+
$parserConfig = $this->getConfig();
118+
$constExprParser = new ConstExprParser($parserConfig);
119+
$typeParser = new TypeParser($parserConfig, $constExprParser);
120120

121-
return new PhpDocParser($config, $typeParser, $constExprParser);
121+
return new PhpDocParser($parserConfig, $typeParser, $constExprParser);
122122
}
123123

124124
protected function getConfig(): ParserConfig
@@ -140,7 +140,7 @@ protected function getTextNodes(array $children): array
140140
{
141141
return array_filter(
142142
$children,
143-
static fn(PhpDocChildNode $child): bool => $child instanceof PhpDocTextNode,
143+
static fn(PhpDocChildNode $phpDocChildNode): bool => $phpDocChildNode instanceof PhpDocTextNode,
144144
);
145145
}
146146
}

src/Support/NodeCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ public function __construct(
1919

2020
public function get(string $name): ?NodeData
2121
{
22-
$nodes = array_values(array_filter($this->nodes, fn(NodeData $node): bool => $node->name === $name));
22+
$nodes = array_values(
23+
array_filter(
24+
$this->nodes,
25+
fn(NodeData $nodeData): bool => $nodeData->name === $name,
26+
),
27+
);
2328

2429
return $nodes[0] ?? null;
2530
}

src/Support/NodeData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class NodeData
88
{
99
/**
10-
* @param array<int, string> $types
10+
* @param array<array-key, string> $types
1111
*/
1212
public function __construct(
1313
public string $name,

src/Types/AbstractSchema.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class AbstractSchema implements Schema
3737
protected string $schemaVersion = 'http://json-schema.org/draft-07/schema#';
3838

3939
/**
40-
* @param \Cortex\JsonSchema\Enums\SchemaType|array<int, \Cortex\JsonSchema\Enums\SchemaType> $type
40+
* @param \Cortex\JsonSchema\Enums\SchemaType|array<array-key, \Cortex\JsonSchema\Enums\SchemaType> $type
4141
*/
4242
public function __construct(
4343
protected SchemaType|array $type,
@@ -56,7 +56,7 @@ public function nullable(): static
5656
}
5757

5858
if (is_array($this->type)) {
59-
$this->type[] = SchemaType::Null;
59+
$this->type[] = SchemaType::Null; // @phpstan-ignore assign.propertyType
6060
} else {
6161
$this->type = [
6262
$this->type,
@@ -76,7 +76,7 @@ public function toArray(bool $includeSchemaRef = true, bool $includeTitle = true
7676
{
7777
$schema = [
7878
'type' => is_array($this->type)
79-
? array_map(static fn(SchemaType $type) => $type->value, $this->type)
79+
? array_map(static fn(SchemaType $schemaType) => $schemaType->value, $this->type)
8080
: $this->type->value,
8181
];
8282

0 commit comments

Comments
 (0)