|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PhpDoc; |
| 4 | + |
| 5 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 6 | +use PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 7 | +use PHPStan\Rules\Rule; |
| 8 | +use PHPStan\Testing\RuleTestCase; |
| 9 | +use function array_merge; |
| 10 | + |
| 11 | +/** |
| 12 | + * @extends RuleTestCase<InvalidPhpDocTagValueRule> |
| 13 | + */ |
| 14 | +class InvalidPhpDocTagValueRuleNoBleedingEdgeTest extends RuleTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + private bool $checkAllInvalidPhpDocs; |
| 18 | + |
| 19 | + protected function getRule(): Rule |
| 20 | + { |
| 21 | + return new InvalidPhpDocTagValueRule( |
| 22 | + self::getContainer()->getByType(Lexer::class), |
| 23 | + self::getContainer()->getByType(PhpDocParser::class), |
| 24 | + $this->checkAllInvalidPhpDocs, |
| 25 | + false, |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public function dataRule(): iterable |
| 30 | + { |
| 31 | + $errors = [ |
| 32 | + [ |
| 33 | + 'PHPDoc tag @param has invalid value (): Unexpected token "\n * ", expected type at offset 13', |
| 34 | + 25, |
| 35 | + ], |
| 36 | + [ |
| 37 | + 'PHPDoc tag @param has invalid value (A & B | C $paramNameA): Unexpected token "|", expected variable at offset 72', |
| 38 | + 25, |
| 39 | + ], |
| 40 | + [ |
| 41 | + 'PHPDoc tag @param has invalid value ((A & B $paramNameB): Unexpected token "$paramNameB", expected \')\' at offset 105', |
| 42 | + 25, |
| 43 | + ], |
| 44 | + [ |
| 45 | + 'PHPDoc tag @param has invalid value (~A & B $paramNameC): Unexpected token "~A", expected type at offset 127', |
| 46 | + 25, |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'PHPDoc tag @var has invalid value (): Unexpected token "\n * ", expected type at offset 156', |
| 50 | + 25, |
| 51 | + ], |
| 52 | + [ |
| 53 | + 'PHPDoc tag @var has invalid value ($invalid): Unexpected token "$invalid", expected type at offset 165', |
| 54 | + 25, |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'PHPDoc tag @var has invalid value ($invalid Foo): Unexpected token "$invalid", expected type at offset 182', |
| 58 | + 25, |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'PHPDoc tag @return has invalid value (): Unexpected token "\n * ", expected type at offset 208', |
| 62 | + 25, |
| 63 | + ], |
| 64 | + [ |
| 65 | + 'PHPDoc tag @return has invalid value ([int, string]): Unexpected token "[", expected type at offset 220', |
| 66 | + 25, |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'PHPDoc tag @return has invalid value (A & B | C): Unexpected token "|", expected TOKEN_OTHER at offset 251', |
| 70 | + 25, |
| 71 | + ], |
| 72 | + [ |
| 73 | + 'PHPDoc tag @var has invalid value (\\\Foo|\Bar $test): Unexpected token "\\\\\\\Foo|\\\Bar", expected type at offset 9', |
| 74 | + 29, |
| 75 | + ], |
| 76 | + [ |
| 77 | + 'PHPDoc tag @var has invalid value ((Foo|Bar): Unexpected token "*/", expected \')\' at offset 18', |
| 78 | + 62, |
| 79 | + ], |
| 80 | + [ |
| 81 | + 'PHPDoc tag @throws has invalid value ((\Exception): Unexpected token "*/", expected \')\' at offset 24', |
| 82 | + 72, |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'PHPDoc tag @var has invalid value ((Foo|Bar): Unexpected token "*/", expected \')\' at offset 18', |
| 86 | + 81, |
| 87 | + ], |
| 88 | + [ |
| 89 | + 'PHPDoc tag @var has invalid value ((Foo&): Unexpected token "*/", expected type at offset 15', |
| 90 | + 89, |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'PHPDoc tag @var has invalid value ((Foo&): Unexpected token "*/", expected type at offset 15', |
| 94 | + 92, |
| 95 | + ], |
| 96 | + ]; |
| 97 | + |
| 98 | + yield [false, $errors]; |
| 99 | + yield [true, array_merge($errors, [ |
| 100 | + [ |
| 101 | + 'PHPDoc tag @var has invalid value ((Foo&): Unexpected token "*/", expected type at offset 15', |
| 102 | + 102, |
| 103 | + ], |
| 104 | + ])]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @dataProvider dataRule |
| 109 | + * @param list<array{0: string, 1: int, 2?: string}> $expectedErrors |
| 110 | + */ |
| 111 | + public function testRule(bool $checkAllInvalidPhpDocs, array $expectedErrors): void |
| 112 | + { |
| 113 | + $this->checkAllInvalidPhpDocs = $checkAllInvalidPhpDocs; |
| 114 | + $this->analyse([__DIR__ . '/data/invalid-phpdoc.php'], $expectedErrors); |
| 115 | + } |
| 116 | + |
| 117 | + public function testBug4731(): void |
| 118 | + { |
| 119 | + $this->checkAllInvalidPhpDocs = true; |
| 120 | + $this->analyse([__DIR__ . '/data/bug-4731.php'], []); |
| 121 | + } |
| 122 | + |
| 123 | + public function testBug4731WithoutFirstTag(): void |
| 124 | + { |
| 125 | + $this->checkAllInvalidPhpDocs = true; |
| 126 | + $this->analyse([__DIR__ . '/data/bug-4731-no-first-tag.php'], []); |
| 127 | + } |
| 128 | + |
| 129 | + public function testInvalidTypeInTypeAlias(): void |
| 130 | + { |
| 131 | + $this->checkAllInvalidPhpDocs = true; |
| 132 | + $this->analyse([__DIR__ . '/data/invalid-type-type-alias.php'], [ |
| 133 | + [ |
| 134 | + 'PHPDoc tag @phpstan-type InvalidFoo has invalid value: Unexpected token "{", expected TOKEN_PHPDOC_EOL at offset 65', |
| 135 | + 12, |
| 136 | + ], |
| 137 | + ]); |
| 138 | + } |
| 139 | + |
| 140 | + public static function getAdditionalConfigFiles(): array |
| 141 | + { |
| 142 | + // reset bleedingEdge |
| 143 | + return []; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments