|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Traits; |
| 4 | + |
| 5 | +use PHPStan\Php\PhpVersion; |
| 6 | +use PHPStan\Rules\AttributesCheck; |
| 7 | +use PHPStan\Rules\ClassCaseSensitivityCheck; |
| 8 | +use PHPStan\Rules\Classes\ClassAttributesRule; |
| 9 | +use PHPStan\Rules\ClassForbiddenNameCheck; |
| 10 | +use PHPStan\Rules\ClassNameCheck; |
| 11 | +use PHPStan\Rules\FunctionCallParametersCheck; |
| 12 | +use PHPStan\Rules\NullsafeCheck; |
| 13 | +use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; |
| 14 | +use PHPStan\Rules\Properties\PropertyReflectionFinder; |
| 15 | +use PHPStan\Rules\Rule; |
| 16 | +use PHPStan\Rules\RuleLevelHelper; |
| 17 | +use PHPStan\Rules\Traits\TraitAttributesRule; |
| 18 | +use PHPStan\Testing\RuleTestCase; |
| 19 | +use const PHP_VERSION_ID; |
| 20 | + |
| 21 | +/** |
| 22 | + * @extends RuleTestCase<TraitAttributesRule> |
| 23 | + */ |
| 24 | +class TraitAttributesRuleTest extends RuleTestCase |
| 25 | +{ |
| 26 | + |
| 27 | + private bool $checkExplicitMixed = false; |
| 28 | + |
| 29 | + private bool $checkImplicitMixed = false; |
| 30 | + |
| 31 | + protected function getRule(): Rule |
| 32 | + { |
| 33 | + $reflectionProvider = $this->createReflectionProvider(); |
| 34 | + return new TraitAttributesRule( |
| 35 | + new AttributesCheck( |
| 36 | + $reflectionProvider, |
| 37 | + new FunctionCallParametersCheck( |
| 38 | + new RuleLevelHelper($reflectionProvider, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, true, false), |
| 39 | + new NullsafeCheck(), |
| 40 | + new PhpVersion(80000), |
| 41 | + new UnresolvableTypeHelper(), |
| 42 | + new PropertyReflectionFinder(), |
| 43 | + true, |
| 44 | + true, |
| 45 | + true, |
| 46 | + true, |
| 47 | + ), |
| 48 | + new ClassNameCheck( |
| 49 | + new ClassCaseSensitivityCheck($reflectionProvider, false), |
| 50 | + new ClassForbiddenNameCheck(self::getContainer()), |
| 51 | + ), |
| 52 | + true, |
| 53 | + ), |
| 54 | + $reflectionProvider, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testRule(): void |
| 59 | + { |
| 60 | + $this->analyse([__DIR__ . '/data/trait-attributes.php'], [ |
| 61 | + [ |
| 62 | + 'Attribute class TraitAttributes\AbstractAttribute is abstract.', |
| 63 | + 8, |
| 64 | + ], |
| 65 | + [ |
| 66 | + 'Attribute class TraitAttributes\MyTargettedAttribute does not have the class target.', |
| 67 | + 20, |
| 68 | + ], |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + public function testBug12011(): void |
| 73 | + { |
| 74 | + if (PHP_VERSION_ID < 80300) { |
| 75 | + $this->markTestSkipped('Test requires PHP 8.3.'); |
| 76 | + } |
| 77 | + |
| 78 | + $this->checkExplicitMixed = true; |
| 79 | + $this->checkImplicitMixed = true; |
| 80 | + |
| 81 | + $this->analyse([__DIR__ . '/data/bug-12011.php'], [ |
| 82 | + [ |
| 83 | + 'Parameter #1 $name of attribute class Bug12011Trait\Table constructor expects string|null, int given.', |
| 84 | + 8, |
| 85 | + ], |
| 86 | + ]); |
| 87 | + } |
| 88 | + |
| 89 | + public function testBug12281(): void |
| 90 | + { |
| 91 | + $this->analyse([__DIR__ . '/data/bug-12281.php'], [ |
| 92 | + [ |
| 93 | + 'Attribute class AllowDynamicProperties cannot be used with trait.', |
| 94 | + 11, |
| 95 | + ], |
| 96 | + ]); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments