Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Capability/Discovery/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,12 @@ private function mapPhpTypeToJsonSchemaType(string $phpTypeString): array
return ['array'];
}

// PRIORITY 3: Handle unions (recursive)
// PRIORITY 3: Treat PHPStan/Psalm integer ranges as their base type
if (preg_match('/^int\s*<\s*[^<>]+\s*>$/i', $normalizedType)) {
return ['integer'];
}

// PRIORITY 4: Handle unions (recursive)
if (str_contains($normalizedType, '|')) {
$types = explode('|', $normalizedType);
$jsonTypes = [];
Expand All @@ -689,7 +694,7 @@ private function mapPhpTypeToJsonSchemaType(string $phpTypeString): array
return array_values(array_unique($jsonTypes));
}

// PRIORITY 4: Handle simple built-in types
// PRIORITY 5: Handle simple built-in types
return match ($normalizedType) {
'string', 'scalar' => ['string'],
'?string' => ['null', 'string'],
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Capability/Discovery/SchemaGeneratorFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ public function typeHintDocBlockAndParameterSchema(
): void {
}

/**
* PHPStan/Psalm integer ranges should keep their base integer type.
*
* @param int<0, max> $offset Positive offset
* @param int<min, -1> $negative Negative value
* @param int<-5, 10> $bounded Bounded value
*/
public function integerRangeTypes(
#[Schema(minimum: 0)]
int $offset,
int $negative,
int $bounded,
): void {
}

// ===== ENUM SCENARIOS =====

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/Capability/Discovery/SchemaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ public function testCombinesPhpTypeHintsDocBlockDescriptionsAndParameterLevelSch
$this->assertEqualsCanonicalizing(['username', 'priority'], $schema['required']);
}

public function testMapsPhpStanAndPsalmIntegerRangesToBaseIntegerType(): void
{
$method = new \ReflectionMethod(SchemaGeneratorFixture::class, 'integerRangeTypes');
$schema = $this->schemaGenerator->generate($method);

$this->assertEquals(['type' => 'integer', 'description' => 'Positive offset', 'minimum' => 0], $schema['properties']['offset']);
$this->assertEquals(['type' => 'integer', 'description' => 'Negative value'], $schema['properties']['negative']);
$this->assertEquals(['type' => 'integer', 'description' => 'Bounded value'], $schema['properties']['bounded']);
}

public function testGeneratesCorrectSchemaForEnumParameters(): void
{
$method = new \ReflectionMethod(SchemaGeneratorFixture::class, 'enumParameters');
Expand Down