From 052cd66421d76d01b778ae46ff7777772003c180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 12 Dec 2023 22:20:36 +0100 Subject: [PATCH 1/2] Enhancement: Use attributes --- example/src/Entity/Avatar.php | 34 +++----- example/src/Entity/CodeOfConduct.php | 46 +++++------ example/src/Entity/Organization.php | 79 +++++++----------- example/src/Entity/Project.php | 48 +++++------ example/src/Entity/Repository.php | 80 +++++++------------ example/src/Entity/User.php | 67 ++++++---------- example/test/Unit/AbstractTestCase.php | 14 ++-- rector.php | 2 + .../Doctrine/ORM/EntityManagerFactory.php | 14 ++-- 9 files changed, 146 insertions(+), 238 deletions(-) diff --git a/example/src/Entity/Avatar.php b/example/src/Entity/Avatar.php index 8bdc8a42..392dc3d1 100644 --- a/example/src/Entity/Avatar.php +++ b/example/src/Entity/Avatar.php @@ -15,33 +15,25 @@ use Doctrine\ORM; -/** - * @ORM\Mapping\Embeddable - */ +#[ORM\Mapping\Embeddable()] final class Avatar { - /** - * @ORM\Mapping\Column( - * name="url", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'url', + type: 'string', + )] private string $url = ''; - /** - * @ORM\Mapping\Column( - * name="width", - * type="integer" - * ) - */ + #[ORM\Mapping\Column( + name: 'width', + type: 'integer', + )] private int $width = 0; - /** - * @ORM\Mapping\Column( - * name="height", - * type="integer" - * ) - */ + #[ORM\Mapping\Column( + name: 'height', + type: 'integer', + )] private int $height = 0; public function url(): string diff --git a/example/src/Entity/CodeOfConduct.php b/example/src/Entity/CodeOfConduct.php index f879d8f1..cea60770 100644 --- a/example/src/Entity/CodeOfConduct.php +++ b/example/src/Entity/CodeOfConduct.php @@ -15,42 +15,32 @@ use Doctrine\ORM; -/** - * @ORM\Mapping\Entity - * - * @ORM\Mapping\Table(name="code_of_conduct") - */ +#[ORM\Mapping\Entity()] +#[ORM\Mapping\Table(name: 'code_of_conduct')] class CodeOfConduct { - /** - * @ORM\Mapping\Id - * - * @ORM\Mapping\Column(type="string") - */ + #[ORM\Mapping\Id()] + #[ORM\Mapping\Column( + type: 'string', + )] private string $key; - /** - * @ORM\Mapping\Column( - * name="name", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'name', + type: 'string', + )] private string $name; - /** - * @ORM\Mapping\Column( - * name="url", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'url', + type: 'string', + )] private string $url; - /** - * @ORM\Mapping\Column( - * name="body", - * type="text" - * ) - */ + #[ORM\Mapping\Column( + name: 'body', + type: 'text', + )] private string $body; public function __construct( diff --git a/example/src/Entity/Organization.php b/example/src/Entity/Organization.php index 70c72368..78ba221e 100644 --- a/example/src/Entity/Organization.php +++ b/example/src/Entity/Organization.php @@ -17,68 +17,47 @@ use Doctrine\ORM; use Ramsey\Uuid; -/** - * @ORM\Mapping\Entity - * - * @ORM\Mapping\Table(name="organization") - */ +#[ORM\Mapping\Entity()] +#[ORM\Mapping\Table(name: 'organization')] class Organization { - /** - * @ORM\Mapping\Id - * - * @ORM\Mapping\GeneratedValue(strategy="NONE") - * - * @ORM\Mapping\Column( - * type="string", - * length=36 - * ) - */ + #[ORM\Mapping\Id()] + #[ORM\Mapping\GeneratedValue(strategy: 'NONE')] + #[ORM\Mapping\Column( + type: 'string', + length: 36, + )] private string $id; - /** - * @ORM\Mapping\Column( - * name="is_verified", - * type="boolean" - * ) - */ + #[ORM\Mapping\Column( + name: 'is_verified', + type: 'boolean', + )] private bool $isVerified = false; - /** - * @ORM\Mapping\Column( - * name="name", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'name', + type: 'string', + )] private string $name; - /** - * @ORM\Mapping\Column( - * name="url", - * type="string", - * nullable=true - * ) - */ + #[ORM\Mapping\Column( + name: 'url', + type: 'string', + nullable: true, + )] private ?string $url = null; - /** - * @ORM\Mapping\OneToMany( - * targetEntity="Example\Entity\Repository", - * mappedBy="organization" - * ) - * - * @var Common\Collections\Collection - */ + #[ORM\Mapping\OneToMany( + targetEntity: 'Example\Entity\Repository', + mappedBy: 'organization', + )] private Common\Collections\Collection $repositories; - /** - * @ORM\Mapping\ManyToMany( - * targetEntity="Example\Entity\User", - * inversedBy="organizations" - * ) - * - * @var Common\Collections\Collection - */ + #[ORM\Mapping\ManyToMany( + targetEntity: 'Example\Entity\User', + inversedBy: 'organizations', + )] private Common\Collections\Collection $members; private bool $constructorWasCalled = false; diff --git a/example/src/Entity/Project.php b/example/src/Entity/Project.php index 80449003..29e2b3ad 100644 --- a/example/src/Entity/Project.php +++ b/example/src/Entity/Project.php @@ -16,42 +16,30 @@ use Doctrine\ORM; use Ramsey\Uuid; -/** - * @ORM\Mapping\Entity - * - * @ORM\Mapping\Table(name="project") - */ +#[ORM\Mapping\Entity()] +#[ORM\Mapping\Table(name: 'project')] class Project { - /** - * @ORM\Mapping\Id - * - * @ORM\Mapping\GeneratedValue(strategy="NONE") - * - * @ORM\Mapping\Column( - * type="string", - * length=36 - * ) - */ + #[ORM\Mapping\Id()] + #[ORM\Mapping\GeneratedValue(strategy: 'NONE')] + #[ORM\Mapping\Column( + type: 'string', + length: 36, + )] private string $id; - /** - * @ORM\Mapping\Column( - * name="name", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'name', + type: 'string', + )] private string $name; - /** - * @ORM\Mapping\ManyToOne(targetEntity="Example\Entity\Repository") - * - * @ORM\Mapping\JoinColumn( - * name="repository_id", - * referencedColumnName="id", - * nullable=false - * ) - */ + #[ORM\Mapping\ManyToOne(targetEntity: Repository::class)] + #[ORM\Mapping\JoinColumn( + name: 'repository_id', + referencedColumnName: 'id', + nullable: false, + )] private Repository $repository; public function __construct( diff --git a/example/src/Entity/Repository.php b/example/src/Entity/Repository.php index 3294c8c4..36f9ca3c 100644 --- a/example/src/Entity/Repository.php +++ b/example/src/Entity/Repository.php @@ -16,65 +16,47 @@ use Doctrine\ORM; use Ramsey\Uuid; -/** - * @ORM\Mapping\Entity - * - * @ORM\Mapping\Table(name="repository") - */ +#[ORM\Mapping\Entity()] +#[ORM\Mapping\Table(name: 'repository')] class Repository { - /** - * @ORM\Mapping\Id - * - * @ORM\Mapping\GeneratedValue(strategy="NONE") - * - * @ORM\Mapping\Column( - * name="id", - * type="string" - * ) - */ + #[ORM\Mapping\Id()] + #[ORM\Mapping\GeneratedValue(strategy: 'NONE')] + #[ORM\Mapping\Column( + name: 'id', + type: 'string', + )] private string $id; - /** - * @ORM\Mapping\Column( - * name="name", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'name', + type: 'string', + )] private string $name; - /** - * @ORM\Mapping\ManyToOne( - * targetEntity="Example\Entity\Organization", - * inversedBy="repositories" - * ) - * - * @ORM\Mapping\JoinColumn( - * name="organization_id", - * referencedColumnName="id", - * nullable=false - * ) - */ + #[ORM\Mapping\ManyToOne( + targetEntity: Organization::class, + inversedBy: 'repositories', + )] + #[ORM\Mapping\JoinColumn( + name: 'organization_id', + referencedColumnName: 'id', + nullable: false, + )] private Organization $organization; - /** - * @ORM\Mapping\ManyToOne(targetEntity="Example\Entity\Repository") - * - * @ORM\Mapping\JoinColumn( - * name="template_id", - * referencedColumnName="id" - * ) - */ + #[ORM\Mapping\ManyToOne(targetEntity: self::class)] + #[ORM\Mapping\JoinColumn( + name: 'template_id', + referencedColumnName: 'id', + )] private ?Repository $template; - /** - * @ORM\Mapping\ManyToOne(targetEntity="Example\Entity\CodeOfConduct") - * - * @ORM\Mapping\JoinColumn( - * name="code_of_conduct_key", - * referencedColumnName="key" - * ) - */ + #[ORM\Mapping\ManyToOne(targetEntity: CodeOfConduct::class)] + #[ORM\Mapping\JoinColumn( + name: 'code_of_conduct_key', + referencedColumnName: 'key', + )] private ?CodeOfConduct $codeOfConduct; public function __construct( diff --git a/example/src/Entity/User.php b/example/src/Entity/User.php index 88ac484f..cde0c805 100644 --- a/example/src/Entity/User.php +++ b/example/src/Entity/User.php @@ -17,58 +17,41 @@ use Doctrine\ORM; use Ramsey\Uuid; -/** - * @ORM\Mapping\Entity - * - * @ORM\Mapping\Table(name="user") - */ +#[ORM\Mapping\Entity()] +#[ORM\Mapping\Table(name: 'user')] class User { - /** - * @ORM\Mapping\Id - * - * @ORM\Mapping\GeneratedValue(strategy="NONE") - * - * @ORM\Mapping\Column( - * type="string", - * length=36 - * ) - */ + #[ORM\Mapping\Id()] + #[ORM\Mapping\GeneratedValue(strategy: 'NONE')] + #[ORM\Mapping\Column( + type: 'string', + length: 36, + )] private string $id; - /** - * @ORM\Mapping\Column( - * name="login", - * type="string" - * ) - */ + #[ORM\Mapping\Column( + name: 'login', + type: 'string', + )] private string $login; - /** - * @ORM\Mapping\Column( - * name="location", - * type="string", - * nullable=true - * ) - */ + #[ORM\Mapping\Column( + name: 'location', + type: 'string', + nullable: true, + )] private ?string $location; - /** - * @ORM\Mapping\Embedded( - * class="Example\Entity\Avatar", - * columnPrefix="avatar" - * ) - */ + #[ORM\Mapping\Embedded( + class: Avatar::class, + columnPrefix: 'avatar', + )] private Avatar $avatar; - /** - * @ORM\Mapping\ManyToMany( - * targetEntity="Example\Entity\Organization", - * mappedBy="members" - * ) - * - * @var Common\Collections\Collection - */ + #[ORM\Mapping\ManyToMany( + targetEntity: Organization::class, + mappedBy: 'members', + )] private Common\Collections\Collection $organizations; public function __construct( diff --git a/example/test/Unit/AbstractTestCase.php b/example/test/Unit/AbstractTestCase.php index 249ba268..8f49c765 100644 --- a/example/test/Unit/AbstractTestCase.php +++ b/example/test/Unit/AbstractTestCase.php @@ -23,15 +23,11 @@ abstract class AbstractTestCase extends Framework\TestCase { final protected static function entityManager(): ORM\EntityManagerInterface { - $configuration = ORM\Tools\Setup::createAnnotationMetadataConfiguration( - [ - __DIR__ . '/../../src/Entity', - ], - true, - null, - null, - false, - ); + $configuration = ORM\Tools\Setup::createConfiguration(true); + + $configuration->setMetadataDriverImpl(new ORM\Mapping\Driver\AttributeDriver([ + __DIR__ . '/../../src/Entity', + ])); $entityManager = ORM\EntityManager::create( [ diff --git a/rector.php b/rector.php index 351fc84c..1da55297 100644 --- a/rector.php +++ b/rector.php @@ -13,6 +13,7 @@ use Rector\Config; use Rector\Core; +use Rector\Doctrine; use Rector\PHPUnit; return static function (Config\RectorConfig $rectorConfig): void { @@ -29,6 +30,7 @@ $rectorConfig->phpVersion(Core\ValueObject\PhpVersion::PHP_81); $rectorConfig->sets([ + Doctrine\Set\DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES, PHPUnit\Set\PHPUnitSetList::PHPUNIT_100, ]); }; diff --git a/test/Util/Doctrine/ORM/EntityManagerFactory.php b/test/Util/Doctrine/ORM/EntityManagerFactory.php index 3394e085..bd546fa4 100644 --- a/test/Util/Doctrine/ORM/EntityManagerFactory.php +++ b/test/Util/Doctrine/ORM/EntityManagerFactory.php @@ -19,15 +19,11 @@ final class EntityManagerFactory { public static function create(): ORM\EntityManagerInterface { - $configuration = ORM\Tools\Setup::createAnnotationMetadataConfiguration( - [ - __DIR__ . '/../../../../example/src/Entity', - ], - true, - null, - null, - false, - ); + $configuration = ORM\Tools\Setup::createConfiguration(true); + + $configuration->setMetadataDriverImpl(new ORM\Mapping\Driver\AttributeDriver([ + __DIR__ . '/../../../../example/src/Entity', + ])); return ORM\EntityManager::create( [ From 323028df149d77da8a53d6b9f9f9fac7cbc3f329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 13 Dec 2023 07:20:50 +0100 Subject: [PATCH 2/2] Fix: Run 'make static-code-analysis-baseline' --- phpstan-baseline.neon | 49 +++++++++++++++++++++++++++++++++++++++++-- psalm-baseline.xml | 34 ++++++++++++++---------------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 26b2121c..9d5e065e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,10 +1,40 @@ parameters: ignoreErrors: + - + message: "#^Method Example\\\\Entity\\\\Organization\\:\\:members\\(\\) should return array\\ but returns array\\.$#" + count: 1 + path: example/src/Entity/Organization.php + + - + message: "#^Method Example\\\\Entity\\\\Organization\\:\\:repositories\\(\\) should return array\\ but returns array\\.$#" + count: 1 + path: example/src/Entity/Organization.php + - message: "#^Method Example\\\\Entity\\\\Organization\\:\\:url\\(\\) has a nullable return type declaration\\.$#" count: 1 path: example/src/Entity/Organization.php + - + message: "#^Property Example\\\\Entity\\\\Organization\\:\\:\\$members type mapping mismatch\\: property can contain Doctrine\\\\Common\\\\Collections\\\\Collection but database expects Doctrine\\\\Common\\\\Collections\\\\Collection&iterable\\\\.$#" + count: 1 + path: example/src/Entity/Organization.php + + - + message: "#^Property Example\\\\Entity\\\\Organization\\:\\:\\$members with generic interface Doctrine\\\\Common\\\\Collections\\\\Collection does not specify its types\\: TKey, T$#" + count: 1 + path: example/src/Entity/Organization.php + + - + message: "#^Property Example\\\\Entity\\\\Organization\\:\\:\\$repositories type mapping mismatch\\: property can contain Doctrine\\\\Common\\\\Collections\\\\Collection but database expects Doctrine\\\\Common\\\\Collections\\\\Collection&iterable\\\\.$#" + count: 1 + path: example/src/Entity/Organization.php + + - + message: "#^Property Example\\\\Entity\\\\Organization\\:\\:\\$repositories with generic interface Doctrine\\\\Common\\\\Collections\\\\Collection does not specify its types\\: TKey, T$#" + count: 1 + path: example/src/Entity/Organization.php + - message: "#^Constructor in Example\\\\Entity\\\\Repository has parameter \\$codeOfConduct with default value\\.$#" count: 1 @@ -65,6 +95,21 @@ parameters: count: 1 path: example/src/Entity/User.php + - + message: "#^Method Example\\\\Entity\\\\User\\:\\:organizations\\(\\) should return array\\ but returns array\\.$#" + count: 1 + path: example/src/Entity/User.php + + - + message: "#^Property Example\\\\Entity\\\\User\\:\\:\\$organizations type mapping mismatch\\: property can contain Doctrine\\\\Common\\\\Collections\\\\Collection but database expects Doctrine\\\\Common\\\\Collections\\\\Collection&iterable\\\\.$#" + count: 1 + path: example/src/Entity/User.php + + - + message: "#^Property Example\\\\Entity\\\\User\\:\\:\\$organizations with generic interface Doctrine\\\\Common\\\\Collections\\\\Collection does not specify its types\\: TKey, T$#" + count: 1 + path: example/src/Entity/User.php + - message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertIsArray\\(\\) with array\\ will always evaluate to true\\.$#" count: 1 @@ -85,7 +130,7 @@ parameters: - message: """ - #^Call to method createAnnotationMetadataConfiguration\\(\\) of deprecated class Doctrine\\\\ORM\\\\Tools\\\\Setup\\: + #^Call to method createConfiguration\\(\\) of deprecated class Doctrine\\\\ORM\\\\Tools\\\\Setup\\: Use \\{@see ORMSetup\\} instead\\.$# """ count: 1 @@ -516,7 +561,7 @@ parameters: - message: """ - #^Call to method createAnnotationMetadataConfiguration\\(\\) of deprecated class Doctrine\\\\ORM\\\\Tools\\\\Setup\\: + #^Call to method createConfiguration\\(\\) of deprecated class Doctrine\\\\ORM\\\\Tools\\\\Setup\\: Use \\{@see ORMSetup\\} instead\\.$# """ count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 9dc54f87..a6e0466e 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -16,6 +16,16 @@ + + + + + + members->toArray()]]> + repositories->toArray()]]> + ]]> + ]]> + __construct id @@ -37,6 +47,10 @@ + + organizations->toArray()]]> + ]]> + __construct id @@ -58,15 +72,7 @@ - + ORM\Tools\Setup::createConfiguration(true) - + ORM\Tools\Setup::createConfiguration(true)