fix: resolve single/double quotes when parsing doc-block type

This commit is contained in:
Romain Canon 2021-11-29 11:33:23 +01:00
parent 6cdea31bc2
commit 1c628b6675
3 changed files with 54 additions and 14 deletions

View File

@ -92,10 +92,10 @@ final class Reflection
{
if ($reflection instanceof ReflectionProperty) {
$docComment = $reflection->getDocComment() ?: '';
$regex = '@var\s+([\w\s?|&<>,-\[\]{}:\\\\]+)';
$regex = '@var\s+([\w\s?|&<>\'",-\[\]{}:\\\\]+)';
} else {
$docComment = $reflection->getDeclaringFunction()->getDocComment() ?: '';
$regex = "@param\s+([\w\s?|&<>,-\[\]{}:\\\\]+)\s+\\$$reflection->name";
$regex = "@param\s+([\w\s?|&<>'\",-\[\]{}:\\\\]+)\s+\\$$reflection->name\s+";
}
if (! preg_match("/$regex/", $docComment, $matches)) {
@ -109,7 +109,7 @@ final class Reflection
{
$docComment = $reflection->getDocComment() ?: '';
if (! preg_match('/@return\s+([\w\s?|&<>,-\[\]{}:\\\\]+)/', $docComment, $matches)) {
if (! preg_match('/@return\s+([\w\s?|&<>\'",-\[\]{}:\\\\]+)/', $docComment, $matches)) {
return null;
}

View File

@ -25,8 +25,11 @@ final class ScalarValuesMappingTest extends IntegrationTest
'integer' => 1337,
'positiveInteger' => 1337,
'negativeInteger' => -1337,
'integerValue' => 42,
'string' => 'foo',
'nonEmptyString' => 'bar',
'stringValueWithSingleQuote' => 'baz',
'stringValueWithDoubleQuote' => 'fiz',
'classString' => self::class,
'classStringOfDateTime' => DateTimeImmutable::class,
'classStringOfAlias' => stdClass::class,
@ -44,8 +47,11 @@ final class ScalarValuesMappingTest extends IntegrationTest
self::assertSame(1337, $result->integer);
self::assertSame(1337, $result->positiveInteger);
self::assertSame(-1337, $result->negativeInteger);
self::assertSame(42, $result->integerValue);
self::assertSame('foo', $result->string);
self::assertSame('bar', $result->nonEmptyString);
self::assertSame('baz', $result->stringValueWithSingleQuote);
self::assertSame('fiz', $result->stringValueWithDoubleQuote);
self::assertSame(self::class, $result->classString);
self::assertSame(DateTimeImmutable::class, $result->classStringOfDateTime);
self::assertSame(stdClass::class, $result->classStringOfAlias);
@ -103,11 +109,20 @@ class ScalarValues
/** @var negative-int */
public int $negativeInteger = -1;
/** @var 42 */
public int $integerValue;
public string $string = 'Schwifty!';
/** @var non-empty-string */
public string $nonEmptyString = 'Schwifty!';
/** @var 'baz' */
public string $stringValueWithSingleQuote;
/** @var "fiz" */
public string $stringValueWithDoubleQuote;
/** @var class-string */
public string $classString = stdClass::class;
@ -123,7 +138,10 @@ class ScalarValuesWithConstructor extends ScalarValues
/**
* @param positive-int $positiveInteger
* @param negative-int $negativeInteger
* @param 42 $integerValue
* @param non-empty-string $nonEmptyString
* @param 'baz' $stringValueWithSingleQuote
* @param "fiz" $stringValueWithDoubleQuote
* @param class-string $classString
* @param class-string<DateTimeInterface> $classStringOfDateTime
* @param class-string<ObjectAlias> $classStringOfAlias
@ -134,8 +152,11 @@ class ScalarValuesWithConstructor extends ScalarValues
int $integer,
int $positiveInteger,
int $negativeInteger,
int $integerValue,
string $string,
string $nonEmptyString,
string $stringValueWithSingleQuote,
string $stringValueWithDoubleQuote,
string $classString,
string $classStringOfDateTime,
string $classStringOfAlias
@ -145,8 +166,11 @@ class ScalarValuesWithConstructor extends ScalarValues
$this->integer = $integer;
$this->positiveInteger = $positiveInteger;
$this->negativeInteger = $negativeInteger;
$this->integerValue = $integerValue;
$this->string = $string;
$this->nonEmptyString = $nonEmptyString;
$this->stringValueWithSingleQuote = $stringValueWithSingleQuote;
$this->stringValueWithDoubleQuote = $stringValueWithDoubleQuote;
$this->classString = $classString;
$this->classStringOfDateTime = $classStringOfDateTime;
$this->classStringOfAlias = $classStringOfAlias;

View File

@ -20,6 +20,9 @@ final class UnionValuesMappingTest extends IntegrationTest
'scalarWithString' => 'foo',
'nullableWithString' => 'bar',
'nullableWithNull' => null,
'integerValue' => 1337,
'stringValueWithSingleQuote' => 'bar',
'stringValueWithDoubleQuote' => 'fiz',
];
$classes = [UnionValues::class, UnionValuesWithConstructor::class];
@ -42,17 +45,12 @@ final class UnionValuesMappingTest extends IntegrationTest
self::assertSame('foo', $result->scalarWithString);
self::assertSame('bar', $result->nullableWithString);
self::assertSame(null, $result->nullableWithNull);
}
}
public function values_are_mapped_properly_data_provider(): iterable
{
yield [UnionValues::class];
yield [UnionValuesWithConstructor::class];
if (PHP_VERSION_ID >= 8_00_00) {
yield [NativeUnionValues::class];
yield [NativeUnionValuesWithConstructor::class];
if ($result instanceof UnionValues) {
self::assertSame(1337, $result->integerValue);
self::assertSame('bar', $result->stringValueWithSingleQuote);
self::assertSame('fiz', $result->stringValueWithDoubleQuote);
}
}
}
}
@ -76,6 +74,15 @@ class UnionValues
/** @var string|null|float */
public $nullableWithNull = 'Schwifty!';
/** @var 42|1337 */
public int $integerValue = 42;
/** @var 'foo'|'bar' */
public string $stringValueWithSingleQuote;
/** @var "baz"|"fiz" */
public string $stringValueWithDoubleQuote;
}
class UnionValuesWithConstructor extends UnionValues
@ -87,6 +94,9 @@ class UnionValuesWithConstructor extends UnionValues
* @param bool|float|int|string $scalarWithString
* @param string|null|float $nullableWithString
* @param string|null|float $nullableWithNull
* @param 42|1337 $integerValue
* @param 'foo'|'bar' $stringValueWithSingleQuote
* @param "baz"|"fiz" $stringValueWithDoubleQuote
*/
public function __construct(
$scalarWithBoolean = 'Schwifty!',
@ -94,7 +104,10 @@ class UnionValuesWithConstructor extends UnionValues
$scalarWithInteger = 'Schwifty!',
$scalarWithString = 'Schwifty!',
$nullableWithString = 'Schwifty!',
$nullableWithNull = 'Schwifty!'
$nullableWithNull = 'Schwifty!',
int $integerValue = 42,
string $stringValueWithSingleQuote = 'foo',
string $stringValueWithDoubleQuote = 'baz'
) {
$this->scalarWithBoolean = $scalarWithBoolean;
$this->scalarWithFloat = $scalarWithFloat;
@ -102,5 +115,8 @@ class UnionValuesWithConstructor extends UnionValues
$this->scalarWithString = $scalarWithString;
$this->nullableWithString = $nullableWithString;
$this->nullableWithNull = $nullableWithNull;
$this->integerValue = $integerValue;
$this->stringValueWithSingleQuote = $stringValueWithSingleQuote;
$this->stringValueWithDoubleQuote = $stringValueWithDoubleQuote;
}
}