Skip to content

Commit

Permalink
fix: resolve single/double quotes when parsing doc-block type
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Nov 29, 2021
1 parent 6cdea31 commit 1c628b6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/Utility/Reflection/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public static function docBlockType(Reflector $reflection): ?string
{
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)) {
Expand All @@ -109,7 +109,7 @@ public static function docBlockReturnType(ReflectionFunctionAbstract $reflection
{
$docComment = $reflection->getDocComment() ?: '';

if (! preg_match('/@return\s+([\w\s?|&<>,-\[\]{}:\\\\]+)/', $docComment, $matches)) {
if (! preg_match('/@return\s+([\w\s?|&<>\'",-\[\]{}:\\\\]+)/', $docComment, $matches)) {
return null;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Mapping/Type/ScalarValuesMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ public function test_values_are_mapped_properly(): void
'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,
Expand All @@ -44,8 +47,11 @@ public function test_values_are_mapped_properly(): void
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);
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -134,8 +152,11 @@ public function __construct(
int $integer,
int $positiveInteger,
int $negativeInteger,
int $integerValue,
string $string,
string $nonEmptyString,
string $stringValueWithSingleQuote,
string $stringValueWithDoubleQuote,
string $classString,
string $classStringOfDateTime,
string $classStringOfAlias
Expand All @@ -145,8 +166,11 @@ public function __construct(
$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;
Expand Down
38 changes: 27 additions & 11 deletions tests/Integration/Mapping/Type/UnionValuesMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function test_values_are_mapped_properly(): void
'scalarWithString' => 'foo',
'nullableWithString' => 'bar',
'nullableWithNull' => null,
'integerValue' => 1337,
'stringValueWithSingleQuote' => 'bar',
'stringValueWithDoubleQuote' => 'fiz',
];

$classes = [UnionValues::class, UnionValuesWithConstructor::class];
Expand All @@ -42,17 +45,12 @@ public function test_values_are_mapped_properly(): void
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);
}
}
}
}
Expand All @@ -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
Expand All @@ -87,20 +94,29 @@ 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!',
$scalarWithFloat = 'Schwifty!',
$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;
$this->scalarWithInteger = $scalarWithInteger;
$this->scalarWithString = $scalarWithString;
$this->nullableWithString = $nullableWithString;
$this->nullableWithNull = $nullableWithNull;
$this->integerValue = $integerValue;
$this->stringValueWithSingleQuote = $stringValueWithSingleQuote;
$this->stringValueWithDoubleQuote = $stringValueWithDoubleQuote;
}
}

0 comments on commit 1c628b6

Please sign in to comment.