Skip to content

Commit b601378

Browse files
authored
Merge pull request #377 from norkunas/rm-doctrine-annotations
Remove `doctrine/annotations` support
2 parents bc3b1d9 + 60338b6 commit b601378

24 files changed

+150
-511
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,15 @@ jobs:
6262
max-parallel: 10
6363
fail-fast: false
6464
matrix:
65-
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
66-
sf_version: [ '5.4.*', '6.4.*', '7.2.*', '7.3.*' ]
65+
php: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
66+
sf_version: [ '6.4.*', '7.2.*', '7.3.*' ]
6767
exclude:
68-
- php: '7.4'
69-
sf_version: '6.4.*'
7068
- php: '8.0'
7169
sf_version: '6.4.*'
72-
- php: '7.4'
73-
sf_version: '7.2.*'
7470
- php: '8.0'
7571
sf_version: '7.2.*'
7672
- php: '8.1'
7773
sf_version: '7.2.*'
78-
- php: '7.4'
79-
sf_version: '7.3.*'
8074
- php: '8.0'
8175
sf_version: '7.3.*'
8276
- php: '8.1'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
composer.lock
22
phpunit.xml
3+
/config/reference.php
34
vendor/
45
.php-cs-fixer.cache
56
.phpunit.result.cache

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
->setRules([
1010
'@Symfony' => true,
1111
'no_superfluous_phpdoc_tags' => false,
12+
'phpdoc_to_comment' => ['ignored_tags' => ['var']], // phpstan errors pops up without this
1213
])
1314
->setFinder($finder)
1415
;

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require": {
17-
"php": "^7.4 || ^8.0",
17+
"php": "^8.1",
1818
"geocoder-php/plugin": "^1.5",
1919
"php-http/discovery": "^1.14",
2020
"symfony/console": "^5.4 || ^6.4 || ^7.0",
@@ -23,9 +23,8 @@
2323
"willdurand/geocoder": "^4.6|^5.0"
2424
},
2525
"require-dev": {
26-
"doctrine/annotations": "^1.11.1 || ^2.0",
2726
"doctrine/doctrine-bundle": "^2.3",
28-
"doctrine/orm": "^2.8 || ^3.0",
27+
"doctrine/orm": "^2.20 || ^3.0",
2928
"fakerphp/faker": "^1.20",
3029
"friendsofphp/php-cs-fixer": "^3.13",
3130
"geocoder-php/algolia-places-provider": "^0.4",
@@ -96,7 +95,7 @@
9695
"prefer-stable": true,
9796
"extra": {
9897
"branch-alias": {
99-
"dev-master": "5.0-dev"
98+
"dev-master": "6.0-dev"
10099
}
101100
}
102101
}

doc/doctrine.md

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,18 @@ First of all, update your entity:
1010

1111
```php
1212

13-
use Bazinga\GeocoderBundle\Mapping\Annotations as Geocoder;
13+
use Bazinga\GeocoderBundle\Mapping\Attributes as Geocoder;
1414

15-
/**
16-
* @Geocoder\Geocodeable
17-
*/
15+
#[Geocoder\Geocodeable()]
1816
class User
1917
{
20-
/**
21-
* @Geocoder\Address
22-
*/
18+
#[Geocoder\Address()]
2319
private $address;
2420

25-
/**
26-
* @Geocoder\Latitude
27-
*/
21+
#[Geocoder\Latitude()]
2822
private $latitude;
2923

30-
/**
31-
* @Geocoder\Longitude
32-
*/
24+
#[Geocoder\Longitude()]
3325
private $longitude;
3426
}
3527
```
@@ -38,47 +30,36 @@ Instead of annotating a property, you can also annotate a getter:
3830

3931
```php
4032

41-
use Bazinga\GeocoderBundle\Mapping\Annotations as Geocoder;
33+
use Bazinga\GeocoderBundle\Mapping\Attributes as Geocoder;
4234

43-
/**
44-
* @Geocoder\Geocodeable
45-
*/
35+
#[Geocoder\Geocodeable()]
4636
class User
4737
{
48-
/**
49-
* @Geocoder\Latitude
50-
*/
38+
#[Geocoder\Latitude()]
5139
private $latitude;
5240

53-
/**
54-
* @Geocoder\Longitude
55-
*/
41+
#[Geocoder\Longitude()]
5642
private $longitude;
5743

58-
/**
59-
* @Geocoder\Address
60-
*/
44+
#[Geocoder\Address()]
6145
public function getAddress(): string
6246
{
6347
// Your code...
6448
}
6549
}
6650
```
6751

68-
Secondly, register the Doctrine event listener and its dependencies in your `config/services.yaml` file.
52+
Secondly, register the Doctrine event listener and its dependencies in your `config/services.yaml` or `config/services.php` file.
6953
You have to indicate which provider to use to reverse geocode the address. Here we use `acme` provider we declared in bazinga_geocoder configuration earlier.
7054

7155
```yaml
72-
Bazinga\GeocoderBundle\Mapping\Driver\AnnotationDriver:
73-
class: Bazinga\GeocoderBundle\Mapping\Driver\AnnotationDriver
74-
arguments:
75-
- '@annotations.reader'
56+
Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver: ~
7657

7758
Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener:
7859
class: Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener
7960
arguments:
8061
- '@bazinga_geocoder.provider.acme'
81-
- '@Bazinga\GeocoderBundle\Mapping\Driver\AnnotationDriver'
62+
- '@Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver'
8263
tags:
8364
- { name: doctrine.event_listener, event: onFlush }
8465
```
@@ -96,40 +77,3 @@ $em->flush();
9677
echo $user->getLatitude(); // will output 52.516325
9778
echo $user->getLongitude(); // will output 13.377264
9879
```
99-
100-
## PHP 8
101-
102-
If you are using PHP 8, you can use [Attributes](https://www.php.net/manual/en/language.attributes.overview.php) in your entity:
103-
104-
```php
105-
106-
use Bazinga\GeocoderBundle\Mapping\Annotations as Geocoder;
107-
108-
#[Geocoder\Geocodeable()]
109-
class User
110-
{
111-
#[Geocoder\Address()]
112-
private $address;
113-
114-
#[Geocoder\Latitude()]
115-
private $latitude;
116-
117-
#[Geocoder\Longitude()]
118-
private $longitude;
119-
}
120-
```
121-
122-
Then update your service configuration to register the `AttributeDriver`:
123-
124-
```yaml
125-
Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver:
126-
class: Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver
127-
128-
Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener:
129-
class: Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener
130-
arguments:
131-
- '@bazinga_geocoder.provider.acme'
132-
- '@Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver'
133-
tags:
134-
- { name: doctrine.event_listener, event: onFlush }
135-
```

phpstan-baseline.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@
141141
'count' => 1,
142142
'path' => __DIR__.'/src/DependencyInjection/BazingaGeocoderExtension.php',
143143
];
144-
$ignoreErrors[] = [
145-
// identifier: method.notFound
146-
'message' => '#^Call to an undefined method Doctrine\\\\ORM\\\\Event\\\\OnFlushEventArgs\\:\\:getEntityManager\\(\\)\\.$#',
147-
'count' => 1,
148-
'path' => __DIR__.'/src/Doctrine/ORM/GeocoderListener.php',
149-
];
150144
$ignoreErrors[] = [
151145
// identifier: method.nonObject
152146
'message' => '#^Cannot call method getLatitude\\(\\) on Geocoder\\\\Model\\\\Coordinates\\|null\\.$#',
@@ -159,18 +153,6 @@
159153
'count' => 1,
160154
'path' => __DIR__.'/src/Doctrine/ORM/GeocoderListener.php',
161155
];
162-
$ignoreErrors[] = [
163-
// identifier: missingType.generics
164-
'message' => '#^Method Bazinga\\\\GeocoderBundle\\\\Mapping\\\\Driver\\\\AnnotationDriver\\:\\:getReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#',
165-
'count' => 1,
166-
'path' => __DIR__.'/src/Mapping/Driver/AnnotationDriver.php',
167-
];
168-
$ignoreErrors[] = [
169-
// identifier: missingType.generics
170-
'message' => '#^Method Bazinga\\\\GeocoderBundle\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:getReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#',
171-
'count' => 1,
172-
'path' => __DIR__.'/src/Mapping/Driver/AttributeDriver.php',
173-
];
174156
$ignoreErrors[] = [
175157
// identifier: argument.type
176158
'message' => '#^Parameter \\#1 \\$text of method Geocoder\\\\Query\\\\GeocodeQuery\\:\\:withText\\(\\) expects string, string\\|null given\\.$#',

src/Doctrine/ORM/GeocoderListener.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@
2626
*/
2727
class GeocoderListener implements EventSubscriber
2828
{
29-
private DriverInterface $driver;
30-
private Provider $geocoder;
31-
32-
public function __construct(Provider $geocoder, DriverInterface $driver)
33-
{
34-
$this->driver = $driver;
35-
$this->geocoder = $geocoder;
29+
public function __construct(
30+
private readonly Provider $geocoder,
31+
private readonly DriverInterface $driver,
32+
) {
3633
}
3734

3835
/**
@@ -45,20 +42,16 @@ public function getSubscribedEvents(): array
4542
];
4643
}
4744

48-
/**
49-
* @return void
50-
*/
51-
public function onFlush(OnFlushEventArgs $args)
45+
public function onFlush(OnFlushEventArgs $args): void
5246
{
53-
$em = method_exists($args, 'getObjectManager') ? $args->getObjectManager() : $args->getEntityManager();
47+
$em = $args->getObjectManager();
5448
$uow = $em->getUnitOfWork();
5549

5650
foreach ($uow->getScheduledEntityInsertions() as $entity) {
5751
if (!$this->driver->isGeocodeable($entity)) {
5852
continue;
5953
}
6054

61-
/** @var ClassMetadata $metadata */
6255
$metadata = $this->driver->loadMetadataFromObject($entity);
6356

6457
$this->geocodeEntity($metadata, $entity);
@@ -74,7 +67,6 @@ public function onFlush(OnFlushEventArgs $args)
7467
continue;
7568
}
7669

77-
/** @var ClassMetadata $metadata */
7870
$metadata = $this->driver->loadMetadataFromObject($entity);
7971

8072
if (!$this->shouldGeocode($metadata, $uow, $entity)) {
@@ -90,17 +82,14 @@ public function onFlush(OnFlushEventArgs $args)
9082
}
9183
}
9284

93-
/**
94-
* @param object $entity
95-
*
96-
* @return void
97-
*/
98-
private function geocodeEntity(ClassMetadata $metadata, $entity)
85+
private function geocodeEntity(ClassMetadata $metadata, object $entity): void
9986
{
10087
if (null !== $metadata->addressGetter) {
10188
$address = $metadata->addressGetter->invoke($entity);
102-
} else {
89+
} elseif (null !== $metadata->addressProperty) {
10390
$address = $metadata->addressProperty->getValue($entity);
91+
} else {
92+
$address = '';
10493
}
10594

10695
if (empty($address) || !is_string($address)) {
@@ -111,22 +100,19 @@ private function geocodeEntity(ClassMetadata $metadata, $entity)
111100

112101
if (!$results->isEmpty()) {
113102
$result = $results->first();
114-
$metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude());
115-
$metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude());
103+
$metadata->latitudeProperty?->setValue($entity, $result->getCoordinates()->getLatitude());
104+
$metadata->longitudeProperty?->setValue($entity, $result->getCoordinates()->getLongitude());
116105
}
117106
}
118107

119-
/**
120-
* @param object $entity
121-
*/
122-
private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, $entity): bool
108+
private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, object $entity): bool
123109
{
124110
if (null !== $metadata->addressGetter) {
125111
return true;
126112
}
127113

128114
$changeSet = $unitOfWork->getEntityChangeSet($entity);
129115

130-
return isset($changeSet[$metadata->addressProperty->getName()]);
116+
return isset($changeSet[$metadata->addressProperty?->getName() ?? '']);
131117
}
132118
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
* @license MIT License
1111
*/
1212

13-
namespace Bazinga\GeocoderBundle\Mapping\Annotations;
13+
namespace Bazinga\GeocoderBundle\Mapping\Attributes;
1414

15-
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
1615
/**
1716
* @author Markus Bachmann <[email protected]>
18-
*
19-
* @Annotation
2017
*/
18+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
2119
class Address
2220
{
2321
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
* @license MIT License
1111
*/
1212

13-
namespace Bazinga\GeocoderBundle\Mapping\Annotations;
13+
namespace Bazinga\GeocoderBundle\Mapping\Attributes;
1414

15-
#[\Attribute(\Attribute::TARGET_CLASS)]
1615
/**
1716
* @author Markus Bachmann <[email protected]>
18-
*
19-
* @Annotation
2017
*/
18+
#[\Attribute(\Attribute::TARGET_CLASS)]
2119
class Geocodeable
2220
{
2321
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
* @license MIT License
1111
*/
1212

13-
namespace Bazinga\GeocoderBundle\Mapping\Annotations;
13+
namespace Bazinga\GeocoderBundle\Mapping\Attributes;
1414

15-
#[\Attribute(\Attribute::TARGET_PROPERTY)]
1615
/**
1716
* @author Markus Bachmann <[email protected]>
18-
*
19-
* @Annotation
2017
*/
18+
#[\Attribute(\Attribute::TARGET_PROPERTY)]
2119
class Latitude
2220
{
2321
}

0 commit comments

Comments
 (0)