|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Generics; |
| 4 | + |
| 5 | +use PHPStan\Rules\Rule; |
| 6 | +use PHPStan\Testing\RuleTestCase; |
| 7 | +use const PHP_VERSION_ID; |
| 8 | + |
| 9 | +/** |
| 10 | + * @extends RuleTestCase<PropertyVarianceRule> |
| 11 | + */ |
| 12 | +class PropertyVarianceRuleTest extends RuleTestCase |
| 13 | +{ |
| 14 | + |
| 15 | + protected function getRule(): Rule |
| 16 | + { |
| 17 | + return new PropertyVarianceRule( |
| 18 | + self::getContainer()->getByType(VarianceCheck::class), |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + public function testRule(): void |
| 23 | + { |
| 24 | + $this->analyse([__DIR__ . '/data/property-variance.php'], [ |
| 25 | + [ |
| 26 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\B::$a.', |
| 27 | + 51, |
| 28 | + ], |
| 29 | + [ |
| 30 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\B::$b.', |
| 31 | + 54, |
| 32 | + ], |
| 33 | + [ |
| 34 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\B::$c.', |
| 35 | + 57, |
| 36 | + ], |
| 37 | + [ |
| 38 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\B::$d.', |
| 39 | + 60, |
| 40 | + ], |
| 41 | + [ |
| 42 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\C::$a.', |
| 43 | + 80, |
| 44 | + ], |
| 45 | + [ |
| 46 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\C::$b.', |
| 47 | + 83, |
| 48 | + ], |
| 49 | + [ |
| 50 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\C::$c.', |
| 51 | + 86, |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\C::$d.', |
| 55 | + 89, |
| 56 | + ], |
| 57 | + ]); |
| 58 | + } |
| 59 | + |
| 60 | + public function testPromoted(): void |
| 61 | + { |
| 62 | + if (PHP_VERSION_ID < 80000) { |
| 63 | + $this->markTestSkipped('Test requires PHP 8.0.'); |
| 64 | + } |
| 65 | + |
| 66 | + $this->analyse([__DIR__ . '/data/property-variance-promoted.php'], [ |
| 67 | + [ |
| 68 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\Promoted\B::$a.', |
| 69 | + 58, |
| 70 | + ], |
| 71 | + [ |
| 72 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\Promoted\B::$b.', |
| 73 | + 59, |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\Promoted\B::$c.', |
| 77 | + 60, |
| 78 | + ], |
| 79 | + [ |
| 80 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\Promoted\B::$d.', |
| 81 | + 61, |
| 82 | + ], |
| 83 | + [ |
| 84 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\Promoted\C::$a.', |
| 85 | + 84, |
| 86 | + ], |
| 87 | + [ |
| 88 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\Promoted\C::$b.', |
| 89 | + 85, |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\Promoted\C::$c.', |
| 93 | + 86, |
| 94 | + ], |
| 95 | + [ |
| 96 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\Promoted\C::$d.', |
| 97 | + 87, |
| 98 | + ], |
| 99 | + ]); |
| 100 | + } |
| 101 | + |
| 102 | + public function testReadOnly(): void |
| 103 | + { |
| 104 | + if (PHP_VERSION_ID < 80100) { |
| 105 | + $this->markTestSkipped('Test requires PHP 8.1.'); |
| 106 | + } |
| 107 | + |
| 108 | + $this->analyse([__DIR__ . '/data/property-variance-readonly.php'], [ |
| 109 | + [ |
| 110 | + 'Template type X is declared as covariant, but occurs in contravariant position in property PropertyVariance\ReadOnly\B::$b.', |
| 111 | + 45, |
| 112 | + ], |
| 113 | + [ |
| 114 | + 'Template type X is declared as covariant, but occurs in invariant position in property PropertyVariance\ReadOnly\B::$d.', |
| 115 | + 51, |
| 116 | + ], |
| 117 | + [ |
| 118 | + 'Template type X is declared as contravariant, but occurs in covariant position in property PropertyVariance\ReadOnly\C::$a.', |
| 119 | + 62, |
| 120 | + ], |
| 121 | + [ |
| 122 | + 'Template type X is declared as contravariant, but occurs in covariant position in property PropertyVariance\ReadOnly\C::$c.', |
| 123 | + 68, |
| 124 | + ], |
| 125 | + [ |
| 126 | + 'Template type X is declared as contravariant, but occurs in invariant position in property PropertyVariance\ReadOnly\C::$d.', |
| 127 | + 71, |
| 128 | + ], |
| 129 | + [ |
| 130 | + 'Template type X is declared as contravariant, but occurs in covariant position in property PropertyVariance\ReadOnly\D::$a.', |
| 131 | + 86, |
| 132 | + ], |
| 133 | + ]); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments