Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e36b309

Browse files
committed
Add test cases for UnusedPrivatePropertyRule
1 parent 6d135ac commit e36b309

File tree

3 files changed

+110
-9
lines changed

3 files changed

+110
-9
lines changed

‎src/Rules/Doctrine/ORM/PropertiesExtension.php‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public function isAlwaysWritten(PropertyReflection $property, string $propertyNa
4444
return false;
4545
}
4646

47-
if ($this->isGeneratedIdentifier($metadata, $propertyName)) {
48-
return true;
49-
}
50-
5147
if ($metadata->isReadOnly && !$declaringClass->hasConstructor()) {
5248
return true;
5349
}
@@ -96,11 +92,7 @@ public function isInitialized(PropertyReflection $property, string $propertyName
9692

9793
private function isGeneratedIdentifier(ClassMetadataInfo $metadata, string $propertyName): bool
9894
{
99-
if (!in_array($propertyName, $metadata->getIdentifierFieldNames(), true)) {
100-
return false;
101-
}
102-
103-
return $metadata->generatorType !== ClassMetadataInfo::GENERATOR_TYPE_NONE;
95+
return !in_array($propertyName, $metadata->getIdentifierFieldNames(), true) && $metadata->isIdentifierNatural();
10496
}
10597

10698
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\DeadCode;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use const PHP_VERSION_ID;
8+
9+
/**
10+
* @extends RuleTestCase<UnusedPrivatePropertyRule>
11+
*/
12+
class UnusedPrivatePropertyRuleTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return self::getContainer()->getByType(UnusedPrivatePropertyRule::class);
18+
}
19+
20+
public static function getAdditionalConfigFiles(): array
21+
{
22+
return [__DIR__ . '/../../../extension.neon'];
23+
}
24+
25+
public function testRule(): void
26+
{
27+
if (PHP_VERSION_ID < 70400) {
28+
self::markTestSkipped('Test requires PHP 7.4.');
29+
}
30+
31+
$this->analyse([__DIR__ . '/data/unused-private-property.php'], [
32+
[
33+
'Property UnusedPrivateProperty\EntityWithAGeneratedId::$unused is never written, only read.',
34+
23,
35+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
36+
],
37+
[
38+
'Property UnusedPrivateProperty\EntityWithAGeneratedId::$unused2 is unused.',
39+
25,
40+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
41+
],
42+
[
43+
'Property UnusedPrivateProperty\ReadOnlyEntityWithConstructor::$id is never written, only read.',
44+
53,
45+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
46+
],
47+
]);
48+
}
49+
50+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php // lint >= 7.4
2+
3+
namespace UnusedPrivateProperty;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity
9+
*/
10+
class EntityWithAGeneratedId
11+
{
12+
13+
/**
14+
* @ORM\Id
15+
* @ORM\GeneratedValue
16+
* @ORM\Column
17+
*/
18+
private int $id; // ok, ID is generated
19+
20+
/**
21+
* @ORM\Column
22+
*/
23+
private int $unused;
24+
25+
private int $unused2;
26+
27+
}
28+
29+
/**
30+
* @ORM\Entity(readOnly=true)
31+
*/
32+
class ReadOnlyEntity
33+
{
34+
35+
/**
36+
* @ORM\Id
37+
* @ORM\Column
38+
*/
39+
private int $id; // ok, entity is read only
40+
41+
}
42+
43+
/**
44+
* @ORM\Entity(readOnly=true)
45+
*/
46+
class ReadOnlyEntityWithConstructor
47+
{
48+
49+
/**
50+
* @ORM\Id
51+
* @ORM\Column
52+
*/
53+
private int $id;
54+
55+
public function __construct()
56+
{
57+
}
58+
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /