Skip to content

Commit 9299b56

Browse files
authored
Merge pull request #1556 from scyzoryck/doctrine-3-fix-phpstan
fix(doctrine 3): fix phpstan issues with doctrine 3.x
2 parents b71da8c + 3b7ded9 commit 9299b56

File tree

8 files changed

+53
-70
lines changed

8 files changed

+53
-70
lines changed

src/Metadata/Driver/AbstractDoctrineTypeDriver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
8989
}
9090

9191
\assert($classMetadata instanceof ClassMetadata);
92-
9392
// Abort if the given class is not a mapped entity
9493
if (!$doctrineMetadata = $this->tryLoadingDoctrineMetadata($class->name)) {
9594
return $classMetadata;

src/Metadata/Driver/DoctrineTypeDriver.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace JMS\Serializer\Metadata\Driver;
66

7-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7+
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata as ORMClassMetadata;
8+
use Doctrine\ORM\Mapping\ClassMetadata as ODMClassMetadata;
89
use Doctrine\Persistence\Mapping\ClassMetadata as DoctrineClassMetadata;
910
use JMS\Serializer\Metadata\ClassMetadata;
1011
use JMS\Serializer\Metadata\PropertyMetadata;
@@ -15,12 +16,9 @@
1516
*/
1617
class DoctrineTypeDriver extends AbstractDoctrineTypeDriver
1718
{
18-
/**
19-
* @param ClassMetadataInfo $doctrineMetadata
20-
* @param ClassMetadata $classMetadata
21-
*/
2219
protected function setDiscriminator(DoctrineClassMetadata $doctrineMetadata, ClassMetadata $classMetadata): void
2320
{
21+
assert($doctrineMetadata instanceof ORMClassMetadata || $doctrineMetadata instanceof ODMClassMetadata);
2422
if (
2523
empty($classMetadata->discriminatorMap) && !$classMetadata->discriminatorDisabled
2624
&& !empty($doctrineMetadata->discriminatorMap) && $doctrineMetadata->isRootEntity()
@@ -51,7 +49,7 @@ protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, Prop
5149
// For inheritance schemes, we cannot add any type as we would only add the super-type of the hierarchy.
5250
// On serialization, this would lead to only the supertype being serialized, and properties of subtypes
5351
// being ignored.
54-
if ($targetMetadata instanceof ClassMetadataInfo && !$targetMetadata->isInheritanceTypeNone()) {
52+
if ($targetMetadata instanceof ODMClassMetadata && !$targetMetadata->isInheritanceTypeNone()) {
5553
return;
5654
}
5755

tests/Fixtures/Doctrine/Entity/BlogPost.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\DBAL\Types\Types;
99
use Doctrine\ORM\Mapping as ORM;
10+
use Doctrine\ORM\Mapping\DiscriminatorColumn;
11+
use Doctrine\ORM\Mapping\DiscriminatorMap;
12+
use Doctrine\ORM\Mapping\InheritanceType;
1013
use JMS\Serializer\Annotation as Serializer;
1114
use JMS\Serializer\Annotation\Groups;
1215
use JMS\Serializer\Annotation\SerializedName;
@@ -18,10 +21,16 @@
1821
/**
1922
* @ORM\Entity
2023
*
24+
* @InheritanceType("SINGLE_TABLE")
25+
* @DiscriminatorColumn(name="discr", type="string")
26+
* @DiscriminatorMap({"simple" = "BlogPost", "extended" = "ExtendedPost"})
2127
* @XmlRoot("blog-post")
2228
*/
2329
#[ORM\Entity]
30+
#[InheritanceType('SINGLE_TABLE')]
2431
#[XmlRoot(name: 'blog-post')]
32+
#[DiscriminatorColumn(name: 'discr', type: 'string')]
33+
#[DiscriminatorMap(['simple' => BlogPost::class, 'extended' => ExtendedPost::class])]
2534
class BlogPost
2635
{
2736
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JMS\Serializer\Tests\Fixtures\Doctrine\Entity;
6+
7+
use Doctrine\ORM\Mapping\Entity;
8+
9+
/**
10+
* @Entity
11+
*/
12+
#[Entity]
13+
class ExtendedPost extends BlogPost
14+
{
15+
}

tests/Metadata/Driver/DoctrineDriverTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
1717
use JMS\Serializer\Metadata\Driver\NullDriver;
1818
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
19+
use JMS\Serializer\Tests\Fixtures\BlogPost;
1920
use JMS\Serializer\Tests\Fixtures\Doctrine\Embeddable\BlogPostWithEmbedded;
21+
use JMS\Serializer\Tests\Fixtures\Doctrine\Entity\Author;
22+
use JMS\Serializer\Tests\Fixtures\Doctrine\Entity\BlogPost as DoctrineBlogPost;
23+
use JMS\Serializer\Tests\Fixtures\Doctrine\Entity\Comment;
24+
use JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor;
2025
use Metadata\Driver\DriverChain;
2126
use PHPUnit\Framework\TestCase;
2227

2328
class DoctrineDriverTest extends TestCase
2429
{
2530
public function getMetadata()
2631
{
27-
$refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Doctrine\Entity\BlogPost');
32+
$refClass = new \ReflectionClass(DoctrineBlogPost::class);
2833

2934
return $this->getDoctrineDriver()->loadMetadataForClass($refClass);
3035
}
@@ -50,7 +55,7 @@ public function testSingleValuedAssociationIsProperlyHinted()
5055
{
5156
$metadata = $this->getMetadata();
5257
self::assertEquals(
53-
['name' => 'JMS\Serializer\Tests\Fixtures\Doctrine\Entity\Author', 'params' => []],
58+
['name' => Author::class, 'params' => []],
5459
$metadata->propertyMetadata['author']->type,
5560
);
5661
}
@@ -63,7 +68,7 @@ public function testMultiValuedAssociationIsProperlyHinted()
6368
[
6469
'name' => 'ArrayCollection',
6570
'params' => [
66-
['name' => 'JMS\Serializer\Tests\Fixtures\Doctrine\Entity\Comment', 'params' => []],
71+
['name' => Comment::class, 'params' => []],
6772
],
6873
],
6974
$metadata->propertyMetadata['comments']->type,
@@ -91,7 +96,7 @@ public function testNonDoctrineEntityClassIsNotModified()
9196
{
9297
// Note: Using regular BlogPost fixture here instead of Doctrine fixture
9398
// because it has no Doctrine metadata.
94-
$refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost');
99+
$refClass = new \ReflectionClass(BlogPost::class);
95100

96101
$plainMetadata = $this->getMetadataDriver()->loadMetadataForClass($refClass);
97102
$doctrineMetadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);
@@ -107,7 +112,7 @@ public function testNonDoctrineEntityClassIsNotModified()
107112
public function testExcludePropertyNoPublicAccessorException()
108113
{
109114
$first = $this->getMetadataDriver()
110-
->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));
115+
->loadMetadataForClass(new \ReflectionClass(ExcludePublicAccessor::class));
111116

112117
self::assertArrayHasKey('id', $first->propertyMetadata);
113118
self::assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
@@ -129,7 +134,7 @@ public function testGuidPropertyIsGivenStringType()
129134
);
130135
}
131136

132-
protected function getEntityManager()
137+
protected function getEntityManager(): EntityManager
133138
{
134139
$config = new Configuration();
135140
$config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
@@ -140,6 +145,7 @@ protected function getEntityManager()
140145
new DoctrineAttributeDriver([__DIR__ . '/../../Fixtures/Doctrine'], true),
141146
);
142147
} else {
148+
assert(class_exists(DoctrineAnnotationDriver::class));
143149
$config->setMetadataDriverImpl(
144150
new DoctrineAnnotationDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/Doctrine'),
145151
);

tests/Metadata/Driver/DoctrinePHPCRDriverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ protected function getDocumentManager()
101101
$config = new Configuration();
102102
$config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
103103
$config->setProxyNamespace('JMS\Tests\Proxies');
104+
assert(class_exists(DoctrinePHPCRDriver::class));
104105
$config->setMetadataDriverImpl(
105106
new DoctrinePHPCRDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/DoctrinePHPCR'),
106107
);

tests/Serializer/Doctrine/IntegrationTest.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Doctrine\ORM\EntityManager;
1313
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
1414
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
15-
use Doctrine\ORM\ORMException;
1615
use Doctrine\ORM\Tools\SchemaTool;
1716
use Doctrine\Persistence\AbstractManagerRegistry;
1817
use Doctrine\Persistence\Proxy;
@@ -143,6 +142,7 @@ private function createEntityManager(Connection $con)
143142
]));
144143
AnnotationReader::addGlobalIgnoredNamespace('Doctrine\ORM\Mapping');
145144
} else {
145+
assert(class_exists(AnnotationDriver::class));
146146
$cfg->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), [
147147
__DIR__ . '/../../Fixtures/Doctrine/SingleTableInheritance',
148148
]));
@@ -158,10 +158,10 @@ private function createEntityManager(Connection $con)
158158

159159
class SimpleManagerRegistry extends AbstractManagerRegistry
160160
{
161-
private $services = [];
162-
private $serviceCreator;
161+
private array $services = [];
162+
private \Closure $serviceCreator;
163163

164-
public function __construct($serviceCreator, $name = 'anonymous', array $connections = ['default' => 'default_connection'], array $managers = ['default' => 'default_manager'], $defaultConnection = null, $defaultManager = null, $proxyInterface = Proxy::class)
164+
public function __construct(\Closure $serviceCreator, $name = 'anonymous', array $connections = ['default' => 'default_connection'], array $managers = ['default' => 'default_manager'], $defaultConnection = null, $defaultManager = null, $proxyInterface = Proxy::class)
165165
{
166166
if (null === $defaultConnection) {
167167
$defaultConnection = key($connections);
@@ -173,20 +173,12 @@ public function __construct($serviceCreator, $name = 'anonymous', array $connect
173173

174174
parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, $proxyInterface);
175175

176-
if (!is_callable($serviceCreator)) {
177-
throw new \InvalidArgumentException('$serviceCreator must be a valid callable.');
178-
}
179-
180176
$this->serviceCreator = $serviceCreator;
181177
}
182178

183179
public function getService($name)
184180
{
185-
if (isset($this->services[$name])) {
186-
return $this->services[$name];
187-
}
188-
189-
return $this->services[$name] = call_user_func($this->serviceCreator, $name);
181+
return $this->services[$name] ??= call_user_func($this->serviceCreator, $name);
190182
}
191183

192184
public function resetService($name)
@@ -196,20 +188,6 @@ public function resetService($name)
196188

197189
public function getAliasNamespace($alias)
198190
{
199-
foreach (array_keys($this->getManagers()) as $name) {
200-
$manager = $this->getManager($name);
201-
202-
if ($manager instanceof EntityManager) {
203-
try {
204-
return $manager->getConfiguration()->getEntityNamespace($alias);
205-
} catch (ORMException $ex) {
206-
// Probably mapped by another entity manager, or invalid, just ignore this here.
207-
}
208-
} else {
209-
throw new \LogicException(sprintf('Unsupported manager type "%s".', get_class($manager)));
210-
}
211-
}
212-
213-
throw new \RuntimeException(sprintf('The namespace alias "%s" is not known to any manager.', $alias));
191+
return $alias;
214192
}
215193
}

tests/Serializer/Doctrine/ObjectConstructorTest.php

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
1616
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
1717
use Doctrine\ORM\Mapping\Driver\XmlDriver;
18-
use Doctrine\ORM\ORMException;
1918
use Doctrine\ORM\PersistentCollection;
2019
use Doctrine\ORM\Tools\SchemaTool;
2120
use Doctrine\ORM\UnitOfWork;
@@ -51,7 +50,6 @@
5150
use JMS\Serializer\Tests\Fixtures\Doctrine\PersistendCollection\SmartPhone;
5251
use JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Author as DoctrinePHPCRAuthor;
5352
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
54-
use LogicException;
5553
use Metadata\Driver\AdvancedDriverInterface;
5654
use Metadata\MetadataFactoryInterface;
5755
use PHPUnit\Framework\Attributes\DataProvider;
@@ -536,6 +534,7 @@ private function createEntityManager(Connection $con, ?Configuration $cfg = null
536534
__DIR__ . '/../../Fixtures/Doctrine/PersistendCollection',
537535
]));
538536
} else {
537+
assert(class_exists(AnnotationDriver::class));
539538
$cfg->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), [
540539
__DIR__ . '/../../Fixtures/Doctrine/Entity',
541540
__DIR__ . '/../../Fixtures/Doctrine/IdentityFields',
@@ -597,10 +596,10 @@ private function createSerializerWithDoctrineObjectConstructor()
597596

598597
class SimpleBaseManagerRegistry extends AbstractManagerRegistry
599598
{
600-
private $services = [];
601-
private $serviceCreator;
599+
private array $services = [];
600+
private \Closure $serviceCreator;
602601

603-
public function __construct($serviceCreator, $name = 'anonymous', array $connections = ['default' => 'default_connection'], array $managers = ['default' => 'default_manager'], $defaultConnection = null, $defaultManager = null, $proxyInterface = Proxy::class)
602+
public function __construct(\Closure $serviceCreator, $name = 'anonymous', array $connections = ['default' => 'default_connection'], array $managers = ['default' => 'default_manager'], $defaultConnection = null, $defaultManager = null, $proxyInterface = Proxy::class)
604603
{
605604
if (null === $defaultConnection) {
606605
$defaultConnection = key($connections);
@@ -612,20 +611,12 @@ public function __construct($serviceCreator, $name = 'anonymous', array $connect
612611

613612
parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, $proxyInterface);
614613

615-
if (!is_callable($serviceCreator)) {
616-
throw new \InvalidArgumentException('$serviceCreator must be a valid callable.');
617-
}
618-
619614
$this->serviceCreator = $serviceCreator;
620615
}
621616

622617
public function getService($name)
623618
{
624-
if (isset($this->services[$name])) {
625-
return $this->services[$name];
626-
}
627-
628-
return $this->services[$name] = call_user_func($this->serviceCreator, $name);
619+
return $this->services[$name] ??= call_user_func($this->serviceCreator, $name);
629620
}
630621

631622
public function resetService($name)
@@ -635,20 +626,6 @@ public function resetService($name)
635626

636627
public function getAliasNamespace($alias)
637628
{
638-
foreach (array_keys($this->getManagers()) as $name) {
639-
$manager = $this->getManager($name);
640-
641-
if ($manager instanceof EntityManager) {
642-
try {
643-
return $manager->getConfiguration()->getEntityNamespace($alias);
644-
} catch (ORMException $ex) {
645-
// Probably mapped by another entity manager, or invalid, just ignore this here.
646-
}
647-
} else {
648-
throw new LogicException(sprintf('Unsupported manager type "%s".', get_class($manager)));
649-
}
650-
}
651-
652-
throw new RuntimeException(sprintf('The namespace alias "%s" is not known to any manager.', $alias));
629+
return $alias;
653630
}
654631
}

0 commit comments

Comments
 (0)