Skip to content

Commit 2c4e70a

Browse files
committed
Regression test
Closes #383
1 parent bc50361 commit 2c4e70a

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

tests/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ protected function getRule(): Rule
1919

2020
public static function getAdditionalConfigFiles(): array
2121
{
22-
return [__DIR__ . '/../../../extension.neon'];
22+
return [
23+
__DIR__ . '/../../../extension.neon',
24+
__DIR__ . '/entity-manager.neon',
25+
];
2326
}
2427

2528
public function testRule(): void
@@ -47,4 +50,29 @@ public function testRule(): void
4750
]);
4851
}
4952

53+
public function testBug383(): void
54+
{
55+
if (PHP_VERSION_ID < 80100) {
56+
self::markTestSkipped('Test requires PHP 8.1.');
57+
}
58+
59+
$this->analyse([__DIR__ . '/data/bug-383.php'], [
60+
[
61+
'Property PHPStan\Rules\Doctrine\ORMAttributes\Bug383\Campus::$id is never written, only read.',
62+
13,
63+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
64+
],
65+
[
66+
'Property PHPStan\Rules\Doctrine\ORMAttributes\Bug383\Campus::$students is never written, only read.',
67+
19,
68+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
69+
],
70+
[
71+
'Property PHPStan\Rules\Doctrine\ORMAttributes\Bug383\Student::$campus is unused.',
72+
29,
73+
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
74+
],
75+
]);
76+
}
77+
5078
}

tests/Rules/DeadCode/data/bug-383.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PHPStan\Rules\Doctrine\ORMAttributes\Bug383;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Doctrine\Common\Collections\Collection;
7+
8+
#[ORM\Entity]
9+
class Campus {
10+
11+
#[ORM\Column(type: 'int')]
12+
#[ORM\Id]
13+
private int $id;
14+
15+
/**
16+
* @var Collection<int, Student>
17+
*/
18+
#[ORM\OneToMany(mappedBy: 'campus', targetEntity: Student::class)]
19+
private Collection $students;
20+
21+
// .......
22+
}
23+
24+
#[ORM\Entity]
25+
class Student {
26+
// ......
27+
#[ORM\ManyToOne(targetEntity: Campus::class, inversedBy: 'students')]
28+
#[ORM\JoinColumn(nullable: false)]
29+
private Campus $campus;
30+
// .......
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
doctrine:
3+
objectManagerLoader: entity-manager.php
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Doctrine\ORM\Configuration;
4+
use Doctrine\ORM\EntityManager;
5+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
6+
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
7+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
8+
use Symfony\Component\Cache\DoctrineProvider;
9+
10+
$config = new Configuration();
11+
$config->setProxyDir(__DIR__);
12+
$config->setProxyNamespace('PHPstan\Doctrine\OrmProxies');
13+
$config->setMetadataCacheImpl(new DoctrineProvider(new ArrayAdapter()));
14+
15+
$metadataDriver = new MappingDriverChain();
16+
if (PHP_VERSION_ID >= 80100) {
17+
$metadataDriver->addDriver(
18+
new AttributeDriver([__DIR__ . '/data']),
19+
'PHPStan\\Rules\\Doctrine\\ORMAttributes\\'
20+
);
21+
}
22+
23+
$config->setMetadataDriverImpl($metadataDriver);
24+
25+
return EntityManager::create(
26+
[
27+
'driver' => 'pdo_sqlite',
28+
'memory' => true,
29+
],
30+
$config
31+
);

0 commit comments

Comments
 (0)