-
Notifications
You must be signed in to change notification settings - Fork 112
Add failing tests for MissingReadOnlyPropertyAssignRule
#339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b87508f
04314d4
c3834aa
6d135ac
909516a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\DeadCode; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<UnusedPrivatePropertyRule> | ||
| */ | ||
| class UnusedPrivatePropertyRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return self::getContainer()->getByType(UnusedPrivatePropertyRule::class); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [__DIR__ . '/../../../extension.neon']; | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| if (PHP_VERSION_ID < 70400) { | ||
| self::markTestSkipped('Test requires PHP 7.4.'); | ||
| } | ||
|
|
||
| $this->analyse([__DIR__ . '/data/unused-private-property.php'], [ | ||
| [ | ||
| 'Property UnusedPrivateProperty\EntityWithAGeneratedId::$unused is never written, only read.', | ||
| 23, | ||
| 'See: https://phpstan.org/developing-extensions/always-read-written-properties', | ||
| ], | ||
| [ | ||
| 'Property UnusedPrivateProperty\EntityWithAGeneratedId::$unused2 is unused.', | ||
| 25, | ||
| 'See: https://phpstan.org/developing-extensions/always-read-written-properties', | ||
| ], | ||
| [ | ||
| 'Property UnusedPrivateProperty\ReadOnlyEntityWithConstructor::$id is never written, only read.', | ||
| 53, | ||
| 'See: https://phpstan.org/developing-extensions/always-read-written-properties', | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php // lint >= 7.4 | ||
|
|
||
| namespace UnusedPrivateProperty; | ||
|
|
||
| use Doctrine\ORM\Mapping as ORM; | ||
|
|
||
| /** | ||
| * @ORM\Entity | ||
| */ | ||
| class EntityWithAGeneratedId | ||
| { | ||
|
|
||
| /** | ||
| * @ORM\Id | ||
| * @ORM\GeneratedValue | ||
| * @ORM\Column | ||
| */ | ||
| private int $id; // ok, ID is generated | ||
|
|
||
| /** | ||
| * @ORM\Column | ||
| */ | ||
| private int $unused; | ||
|
|
||
| private int $unused2; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity(readOnly=true) | ||
| */ | ||
| class ReadOnlyEntity | ||
| { | ||
|
|
||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column | ||
| */ | ||
| private int $id; // ok, entity is read only | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity(readOnly=true) | ||
| */ | ||
| class ReadOnlyEntityWithConstructor | ||
| { | ||
|
|
||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column | ||
| */ | ||
| private int $id; | ||
|
|
||
| public function __construct() | ||
| { | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Properties; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<MissingReadOnlyByPhpDocPropertyAssignRule> | ||
| */ | ||
| class MissingReadOnlyByPhpDocPropertyAssignRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return self::getContainer()->getByType(MissingReadOnlyByPhpDocPropertyAssignRule::class); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [__DIR__ . '/../../../extension.neon']; | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| if (PHP_VERSION_ID < 70400) { | ||
| self::markTestSkipped('Test requires PHP 7.4.'); | ||
| } | ||
|
|
||
| $this->analyse([__DIR__ . '/data/missing-readonly-property-assign-phpdoc.php'], [ | ||
| [ | ||
| 'Class MissingReadOnlyPropertyAssignPhpDoc\EntityWithAGeneratedId has an uninitialized @readonly property $unassigned. Assign it in the constructor.', | ||
| 25, | ||
| ], | ||
| [ | ||
| '@readonly property MissingReadOnlyPropertyAssignPhpDoc\EntityWithAGeneratedId::$doubleAssigned is already assigned.', | ||
| 36, | ||
| ], | ||
| [ | ||
| 'Class MissingReadOnlyPropertyAssignPhpDoc\ReadOnlyEntityWithConstructor has an uninitialized @readonly property $id. Assign it in the constructor.', | ||
| 67, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Properties; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<MissingReadOnlyPropertyAssignRule> | ||
| */ | ||
| class MissingReadOnlyPropertyAssignRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return self::getContainer()->getByType(MissingReadOnlyPropertyAssignRule::class); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [__DIR__ . '/../../../extension.neon']; | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| if (PHP_VERSION_ID < 80100) { | ||
| self::markTestSkipped('Test requires PHP 8.1.'); | ||
| } | ||
|
|
||
| $this->analyse([__DIR__ . '/data/missing-readonly-property-assign.php'], [ | ||
| [ | ||
| 'Class MissingReadOnlyPropertyAssign\EntityWithAGeneratedId has an uninitialized readonly property $unassigned. Assign it in the constructor.', | ||
| 17, | ||
| ], | ||
| [ | ||
| 'Readonly property MissingReadOnlyPropertyAssign\EntityWithAGeneratedId::$doubleAssigned is already assigned.', | ||
| 25, | ||
| ], | ||
| [ | ||
| 'Class MissingReadOnlyPropertyAssign\ReadOnlyEntityWithConstructor has an uninitialized readonly property $id. Assign it in the constructor.', | ||
| 46, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php // lint >= 7.4 | ||
|
|
||
| namespace MissingReadOnlyPropertyAssignPhpDoc; | ||
|
|
||
| use Doctrine\ORM\Mapping as ORM; | ||
|
|
||
| /** | ||
| * @ORM\Entity | ||
| */ | ||
| class EntityWithAGeneratedId | ||
| { | ||
|
|
||
| /** | ||
| * @ORM\Id | ||
| * @ORM\GeneratedValue | ||
| * @ORM\Column | ||
| * @readonly | ||
| */ | ||
| private int $id; // ok, ID is generated | ||
|
|
||
| /** | ||
| * @ORM\Column | ||
| * @readonly | ||
| */ | ||
| private int $unassigned; | ||
|
|
||
| /** | ||
| * @ORM\Column | ||
| * @readonly | ||
| */ | ||
| private int $doubleAssigned; | ||
|
|
||
| public function __construct(int $doubleAssigned) | ||
| { | ||
| $this->doubleAssigned = $doubleAssigned; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just got an idea for a rule improvement in phpstan/src. If an extension says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting, I guess I can take a look at this in phpstan-src |
||
| $this->doubleAssigned = 17; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity(readOnly=true) | ||
| */ | ||
| class ReadOnlyEntity | ||
| { | ||
|
|
||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column | ||
| * @readonly | ||
| */ | ||
| private int $id; // ok, entity is read only | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity(readOnly=true) | ||
| */ | ||
| class ReadOnlyEntityWithConstructor | ||
| { | ||
|
|
||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column | ||
| * @readonly | ||
| */ | ||
| private int $id; | ||
|
|
||
| public function __construct() | ||
| { | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.