|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PhpDoc; |
| 4 | + |
| 5 | +use PHPStan\Rules\Rule; |
| 6 | +use PHPStan\Rules\RuleLevelHelper; |
| 7 | +use PHPStan\Testing\RuleTestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * @extends RuleTestCase<VarTagReflectsUsagesRule> |
| 11 | + */ |
| 12 | +class VarTagReflectsUsagesRuleTest extends RuleTestCase |
| 13 | +{ |
| 14 | + |
| 15 | + private bool $checkUnionTypes = true; |
| 16 | + |
| 17 | + private bool $checkMixed = false; |
| 18 | + |
| 19 | + protected function getRule(): Rule |
| 20 | + { |
| 21 | + return new VarTagReflectsUsagesRule(new RuleLevelHelper( |
| 22 | + self::createReflectionProvider(), |
| 23 | + checkNullables: true, |
| 24 | + checkThisOnly: false, |
| 25 | + checkUnionTypes: $this->checkUnionTypes, |
| 26 | + checkExplicitMixed: $this->checkMixed, |
| 27 | + checkImplicitMixed: $this->checkMixed, |
| 28 | + checkBenevolentUnionTypes: false, |
| 29 | + discoveringSymbolsTip: true, |
| 30 | + )); |
| 31 | + } |
| 32 | + |
| 33 | + public function testRule(): void |
| 34 | + { |
| 35 | + $this->analyse([__DIR__ . '/data/var-tag-reflects-usages.php'], [ |
| 36 | + [ |
| 37 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-list<\'x\'|int> assigned to $a.', |
| 38 | + 56, |
| 39 | + ], |
| 40 | + [ |
| 41 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Collection<int|string> does not accept type VarTagReflectsUsages\\Collection<int> assigned to $c.', |
| 42 | + 83, |
| 43 | + ], |
| 44 | + [ |
| 45 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-list<\'x\'|int> assigned to $a.', |
| 46 | + 99, |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-list<\'x\'|int> assigned to $a.', |
| 50 | + 109, |
| 51 | + ], |
| 52 | + [ |
| 53 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Foo|null does not accept type VarTagReflectsUsages\\Bar assigned to $a.', |
| 54 | + 156, |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'PHPDoc tag @var with type array<string, int> does not accept type array<string, int|string> assigned to $cache.', |
| 58 | + 179, |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'PHPDoc tag @var with type int|null does not accept type string assigned to $instance.', |
| 62 | + 200, |
| 63 | + ], |
| 64 | + [ |
| 65 | + 'PHPDoc tag @var with type array<int> does not accept type array<int|string> assigned to $a.', |
| 66 | + 251, |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'PHPDoc tag @var with type array<1|2> does not accept type non-empty-array<1|2|3> assigned to $a.', |
| 70 | + 269, |
| 71 | + ], |
| 72 | + [ |
| 73 | + 'PHPDoc tag @var with type array<int> does not accept type array<int|string> assigned to $a.', |
| 74 | + 288, |
| 75 | + ], |
| 76 | + [ |
| 77 | + 'PHPDoc tag @var with type array<int> does not accept type array<float|int> assigned to $a.', |
| 78 | + 290, |
| 79 | + ], |
| 80 | + [ |
| 81 | + 'PHPDoc tag @var with type array<int> does not accept type array<int|string> assigned to $a.', |
| 82 | + 293, |
| 83 | + ], |
| 84 | + [ |
| 85 | + 'PHPDoc tag @var with type \'a\'|\'b\' does not accept type \'ax\'|\'bx\' assigned to $s.', |
| 86 | + 302, |
| 87 | + ], |
| 88 | + [ |
| 89 | + 'PHPDoc tag @var with type \'a\'|\'b\' does not accept type \'ay\'|\'by\' assigned to $s.', |
| 90 | + 303, |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'PHPDoc tag @var with type int<0, 5> does not accept type int<1, 6> assigned to $n.', |
| 94 | + 311, |
| 95 | + ], |
| 96 | + [ |
| 97 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-array<int<0, max>, int> assigned to $l.', |
| 98 | + 320, |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'PHPDoc tag @var with type int does not accept type string assigned to $d.', |
| 102 | + 329, |
| 103 | + ], |
| 104 | + ]); |
| 105 | + } |
| 106 | + |
| 107 | + public function testRuleWithoutUnionTypes(): void |
| 108 | + { |
| 109 | + $this->checkUnionTypes = false; |
| 110 | + $this->analyse([__DIR__ . '/data/var-tag-reflects-usages.php'], [ |
| 111 | + [ |
| 112 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Collection<int|string> does not accept type VarTagReflectsUsages\\Collection<int> assigned to $c.', |
| 113 | + 83, |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Foo|null does not accept type VarTagReflectsUsages\\Bar assigned to $a.', |
| 117 | + 156, |
| 118 | + ], |
| 119 | + [ |
| 120 | + 'PHPDoc tag @var with type int|null does not accept type string assigned to $instance.', |
| 121 | + 200, |
| 122 | + ], |
| 123 | + [ |
| 124 | + 'PHPDoc tag @var with type array<int> does not accept type array<float|int> assigned to $a.', |
| 125 | + 290, |
| 126 | + ], |
| 127 | + [ |
| 128 | + 'PHPDoc tag @var with type \'a\'|\'b\' does not accept type \'ax\'|\'bx\' assigned to $s.', |
| 129 | + 302, |
| 130 | + ], |
| 131 | + [ |
| 132 | + 'PHPDoc tag @var with type \'a\'|\'b\' does not accept type \'ay\'|\'by\' assigned to $s.', |
| 133 | + 303, |
| 134 | + ], |
| 135 | + [ |
| 136 | + 'PHPDoc tag @var with type int does not accept type string assigned to $d.', |
| 137 | + 329, |
| 138 | + ], |
| 139 | + ]); |
| 140 | + } |
| 141 | + |
| 142 | + public function testRuleWithMixed(): void |
| 143 | + { |
| 144 | + $this->checkMixed = true; |
| 145 | + $this->analyse([__DIR__ . '/data/var-tag-reflects-usages.php'], [ |
| 146 | + [ |
| 147 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-list<\'x\'|int> assigned to $a.', |
| 148 | + 56, |
| 149 | + ], |
| 150 | + [ |
| 151 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Collection<int|string> does not accept type VarTagReflectsUsages\\Collection<int> assigned to $c.', |
| 152 | + 83, |
| 153 | + ], |
| 154 | + [ |
| 155 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-list<\'x\'|int> assigned to $a.', |
| 156 | + 99, |
| 157 | + ], |
| 158 | + [ |
| 159 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-list<\'x\'|int> assigned to $a.', |
| 160 | + 109, |
| 161 | + ], |
| 162 | + [ |
| 163 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Foo|null does not accept type VarTagReflectsUsages\\Bar assigned to $a.', |
| 164 | + 156, |
| 165 | + ], |
| 166 | + [ |
| 167 | + 'PHPDoc tag @var with type array<string, VarTagReflectsUsages\\Foo> does not accept type array<mixed> assigned to $map.', |
| 168 | + 167, |
| 169 | + ], |
| 170 | + [ |
| 171 | + 'PHPDoc tag @var with type array<string, int> does not accept type array<string, int|string> assigned to $cache.', |
| 172 | + 179, |
| 173 | + ], |
| 174 | + [ |
| 175 | + 'PHPDoc tag @var with type int|null does not accept type string assigned to $instance.', |
| 176 | + 200, |
| 177 | + ], |
| 178 | + [ |
| 179 | + 'PHPDoc tag @var with type VarTagReflectsUsages\\Entity<int> does not accept type VarTagReflectsUsages\\Entity<mixed> assigned to $entity.', |
| 180 | + 242, |
| 181 | + ], |
| 182 | + [ |
| 183 | + 'PHPDoc tag @var with type array<int> does not accept type array<int|string> assigned to $a.', |
| 184 | + 251, |
| 185 | + ], |
| 186 | + [ |
| 187 | + 'PHPDoc tag @var with type array<1|2> does not accept type non-empty-array<1|2|3> assigned to $a.', |
| 188 | + 269, |
| 189 | + ], |
| 190 | + [ |
| 191 | + 'PHPDoc tag @var with type array<int> does not accept type array<int|string> assigned to $a.', |
| 192 | + 288, |
| 193 | + ], |
| 194 | + [ |
| 195 | + 'PHPDoc tag @var with type array<int> does not accept type array<float|int> assigned to $a.', |
| 196 | + 290, |
| 197 | + ], |
| 198 | + [ |
| 199 | + 'PHPDoc tag @var with type array<int> does not accept type array<int|string> assigned to $a.', |
| 200 | + 293, |
| 201 | + ], |
| 202 | + [ |
| 203 | + 'PHPDoc tag @var with type \'a\'|\'b\' does not accept type \'ax\'|\'bx\' assigned to $s.', |
| 204 | + 302, |
| 205 | + ], |
| 206 | + [ |
| 207 | + 'PHPDoc tag @var with type \'a\'|\'b\' does not accept type \'ay\'|\'by\' assigned to $s.', |
| 208 | + 303, |
| 209 | + ], |
| 210 | + [ |
| 211 | + 'PHPDoc tag @var with type int<0, 5> does not accept type int<1, 6> assigned to $n.', |
| 212 | + 311, |
| 213 | + ], |
| 214 | + [ |
| 215 | + 'PHPDoc tag @var with type list<int> does not accept type non-empty-array<int<0, max>, int> assigned to $l.', |
| 216 | + 320, |
| 217 | + ], |
| 218 | + [ |
| 219 | + 'PHPDoc tag @var with type int does not accept type string assigned to $d.', |
| 220 | + 329, |
| 221 | + ], |
| 222 | + ]); |
| 223 | + } |
| 224 | + |
| 225 | + public function testBug14198(): void |
| 226 | + { |
| 227 | + $this->analyse([__DIR__ . '/data/bug-14198.php'], [ |
| 228 | + [ |
| 229 | + 'PHPDoc tag @var with type string does not accept type array assigned to $name.', |
| 230 | + 9, |
| 231 | + ], |
| 232 | + ]); |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments