Skip to content

Commit

Permalink
fix: PhpdocArrayTypeFixer - JIT stack limit exhausted (#7895)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
  • Loading branch information
kubawerlos and mvorisek committed Mar 22, 2024
1 parent f9d2511 commit e980ab2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/DocBlock/TypeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ final class TypeExpression
(?:
\h*,\h*
(?&callable_argument)
)*
)*+
(?:\h*,\h*)?
|)
\h*\)
Expand All @@ -125,7 +125,7 @@ final class TypeExpression
(?:
\h*,\h*
(?&types_inner)
)*
)*+
(?:\h*,\h*)?
)
\h*>
Expand Down Expand Up @@ -196,13 +196,13 @@ final class TypeExpression
(\h*\[\h*\])*
)
(?:(?=1)0
(?<types_inner>
(?<types_inner>(?>
(?&type)
(?:
\h*[|&]\h*
(?&type)
)*+
)
))
|)
)';

Expand Down
27 changes: 13 additions & 14 deletions tests/DocBlock/TypeExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public static function provideGetTypesCases(): iterable

yield ['OBJECT { x: 1 }'];

yield ['array{a: int, b: int, with-dash: int}'];

yield ['callable'];

yield ['callable(string)'];
Expand Down Expand Up @@ -249,11 +251,11 @@ public static function provideGetTypesCases(): iterable

yield ['\'a\\\'s"\\\\\n\r\t\'|"b\\"s\'\\\\\n\r\t"', ['\'a\\\'s"\\\\\n\r\t\'', '"b\\"s\'\\\\\n\r\t"']];

yield ['array{a: int, b: int, c: int, d: int, e: int, f: int, g: int, h: int, i: int, j: int, with-dash: int}'];
yield ['string'.str_repeat('[]', 128)];

yield ['array{a: int, b: int, c: int, d: int, e: int, f: int, g: int, h: int, i: int, j: int, k: int, l: int, with-dash: int}'];
yield [str_repeat('array<', 128).'string'.str_repeat('>', 128)];

yield [self::createHugeArrayShapeType()];
yield [self::makeLongArrayShapeType()];
}

public static function provideGetConstTypesCases(): iterable
Expand Down Expand Up @@ -401,7 +403,7 @@ public static function provideParseInvalidExceptionCases(): iterable

yield 'generic Closure with non-identifier template argument' => ['Closure<A|B>(): void'];

yield [substr(self::createHugeArrayShapeType(), 0, -1)];
yield [substr(self::makeLongArrayShapeType(), 0, -1)];
}

public function testHugeType(): void
Expand Down Expand Up @@ -923,18 +925,15 @@ public static function provideSortTypesCases(): iterable
];
}

private static function createHugeArrayShapeType(): string
private static function makeLongArrayShapeType(): string
{
return sprintf(
'array{%s}',
implode(
', ',
array_map(
static fn (int $k): string => sprintf('key%sno%d: int', 0 === $k % 2 ? '-' : '_', $k),
range(1, 1_000),
),
return 'array{'.implode(
', ',
array_map(
static fn (int $k): string => sprintf('key%sno%d: int', 0 === $k % 2 ? '-' : '_', $k),
range(1, 1_000),
),
);
).'}';
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocArrayTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,16 @@ public static function provideFixCases(): iterable
'<?php /** @var (Foo&Bar)|array<Baz> */',
'<?php /** @var (Foo&Bar)|Baz[] */',
];

$expected = $input = 'string';
for ($i = 0; $i < 128; ++$i) {
$expected = 'array<'.$expected.'>';
$input .= '[]';
}

yield [
sprintf('<?php /** @var %s */', $expected),
sprintf('<?php /** @var %s */', $input),
];
}
}

0 comments on commit e980ab2

Please sign in to comment.