Skip to content

Commit 3395ce4

Browse files
authored
Merge pull request #78 from ray-di/remove-doctrine-annotations
Remove doctrine/annotations dependency
2 parents 93745e0 + cbd5dd8 commit 3395ce4

24 files changed

+1025
-257
lines changed

.scrutinizer.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
build:
2+
image: default-jammy
3+
environment:
4+
php: 8.4
5+
nodes:
6+
analysis:
7+
tests:
8+
override:
9+
- php-scrutinizer-run
10+
11+
filter:
12+
paths: ["src/*"]

ANNOTATION_TO_ATTRIBUTE.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Migrating from Doctrine Annotations to PHP 8 Attributes
2+
3+
## Overview
4+
5+
Ray.AuraSqlModule v2.x has removed the dependency on `doctrine/annotations` and now exclusively uses native PHP 8 attributes. This guide will help you migrate your application code from Doctrine annotations to PHP 8 attributes.
6+
7+
## Why Migrate?
8+
9+
- **doctrine/annotations is Abandoned**: The `doctrine/annotations` package has been officially abandoned by its maintainers. Continuing to use it poses security and compatibility risks as it will no longer receive updates or security patches.
10+
- **Native PHP Support**: PHP 8 attributes are a built-in language feature, providing first-class support without external dependencies
11+
- **Better Performance**: No runtime annotation parsing overhead - attributes are compiled and cached by PHP itself
12+
- **Modern Syntax**: Cleaner, more readable code that follows PHP 8+ best practices
13+
- **No Extra Dependencies**: Eliminates the need for the abandoned doctrine/annotations package
14+
- **Future-Proof**: Attributes are the official PHP standard for metadata, ensuring long-term compatibility
15+
16+
## Migration Steps
17+
18+
### Step 1: Update Ray.AuraSqlModule
19+
20+
First, ensure you're using Ray.AuraSqlModule v2.x:
21+
22+
```bash
23+
composer require ray/aura-sql-module:^2.0
24+
```
25+
26+
### Step 2: Install Rector (if not already installed)
27+
28+
Rector is an automated refactoring tool that can convert annotations to attributes:
29+
30+
```bash
31+
composer require --dev rector/rector
32+
```
33+
34+
### Step 3: Run Automated Migration
35+
36+
Ray.AuraSqlModule provides a Rector configuration file for automated migration:
37+
38+
```bash
39+
# Dry-run to preview changes
40+
vendor/bin/rector process src --config=vendor/ray/aura-sql-module/rector-migrate.php --dry-run
41+
42+
# Apply the changes
43+
vendor/bin/rector process src --config=vendor/ray/aura-sql-module/rector-migrate.php
44+
```
45+
46+
If you have tests that use annotations:
47+
48+
```bash
49+
vendor/bin/rector process tests --config=vendor/ray/aura-sql-module/rector-migrate.php
50+
```
51+
52+
### Step 4: Manual Review
53+
54+
Review the changes made by Rector and adjust if necessary. Pay special attention to:
55+
56+
- Multi-line annotations with complex values
57+
- Annotations with custom parameters
58+
- Import statements (Rector should handle these automatically)
59+
60+
### Step 5: Remove doctrine/annotations
61+
62+
After migration, you can safely remove the doctrine/annotations dependency:
63+
64+
```bash
65+
composer remove doctrine/annotations
66+
```
67+
68+
## Before and After Examples
69+
70+
### Transactional Annotation
71+
72+
**Before (Doctrine Annotation):**
73+
```php
74+
use Ray\AuraSqlModule\Annotation\Transactional;
75+
use Ray\AuraSqlModule\Annotation\WriteConnection;
76+
77+
class UserRepository
78+
{
79+
/**
80+
* @WriteConnection
81+
* @Transactional
82+
*/
83+
public function save(User $user): void
84+
{
85+
// ...
86+
}
87+
}
88+
```
89+
90+
**After (PHP 8 Attribute):**
91+
```php
92+
use Ray\AuraSqlModule\Annotation\Transactional;
93+
use Ray\AuraSqlModule\Annotation\WriteConnection;
94+
95+
class UserRepository
96+
{
97+
#[WriteConnection, Transactional]
98+
public function save(User $user): void
99+
{
100+
// ...
101+
}
102+
}
103+
```
104+
105+
### Read-Only Connection
106+
107+
**Before (Doctrine Annotation):**
108+
```php
109+
use Aura\Sql\ExtendedPdoInterface;
110+
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection;
111+
112+
class UserFinder
113+
{
114+
/**
115+
* @ReadOnlyConnection
116+
*/
117+
public function __construct(
118+
private ExtendedPdoInterface $pdo
119+
) {}
120+
}
121+
```
122+
123+
**After (PHP 8 Attribute):**
124+
```php
125+
use Aura\Sql\ExtendedPdoInterface;
126+
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection;
127+
128+
class UserFinder
129+
{
130+
#[ReadOnlyConnection]
131+
public function __construct(
132+
private ExtendedPdoInterface $pdo
133+
) {}
134+
}
135+
```
136+
137+
## Supported Annotations
138+
139+
The following Ray.AuraSqlModule annotations are automatically converted:
140+
141+
- `@Transactional``#[Transactional]`
142+
- `@WriteConnection``#[WriteConnection]`
143+
- `@ReadOnlyConnection``#[ReadOnlyConnection]`
144+
- `@Read``#[Read]`
145+
- `@AuraSql``#[AuraSql]`
146+
- `@EnvAuth``#[EnvAuth]`
147+
- `@HttpMethod``#[HttpMethod]`
148+
- `@PagerViewOption``#[PagerViewOption]`
149+
- `@AuraSqlQueryConfig``#[AuraSqlQueryConfig]`
150+
151+
**Note**: This migration tool only handles Ray.AuraSqlModule annotations. For Ray.Di annotations (such as `@Inject`, `@Named`, etc.), please refer to the [Ray.Di migration guide](https://github.com/ray-di/Ray.Di).
152+
153+
## Troubleshooting
154+
155+
### Rector doesn't find annotations
156+
157+
Make sure your code is using fully qualified class names in `use` statements:
158+
159+
```php
160+
// Correct
161+
use Ray\AuraSqlModule\Annotation\Transactional;
162+
163+
// Incorrect - Rector won't recognize this
164+
use Ray\AuraSqlModule\Annotation as DB;
165+
```
166+
167+
### Import statements not updated
168+
169+
Rector should automatically update import statements, but if it doesn't:
170+
171+
1. Manually verify `use` statements are present
172+
2. Run your IDE's "Optimize Imports" feature
173+
3. Use tools like PHP-CS-Fixer to clean up unused imports
174+
175+
### Complex annotation values
176+
177+
For annotations with complex array values, you may need to manually adjust the syntax:
178+
179+
**Before:**
180+
```php
181+
/**
182+
* @Transactional({"pdo1", "pdo2"})
183+
*/
184+
```
185+
186+
**After:**
187+
```php
188+
#[Transactional(["pdo1", "pdo2"])]
189+
```
190+
191+
### Testing the migration
192+
193+
After migration, ensure all tests pass:
194+
195+
```bash
196+
vendor/bin/phpunit
197+
```
198+
199+
## Manual Migration
200+
201+
If you prefer not to use Rector, you can manually convert annotations:
202+
203+
1. Replace `/** @AnnotationName */` with `#[AnnotationName]`
204+
2. Move attributes from docblocks to the line before the method/property/class
205+
+3. For parameter annotations, place the attribute before the parameter declaration
206+
4. Ensure all necessary `use` statements are present
207+
208+
## Need Help?
209+
210+
If you encounter issues during migration:
211+
212+
1. Check the [Ray.AuraSqlModule documentation](https://github.com/ray-di/Ray.AuraSqlModule)
213+
2. Review the [PHP 8 Attributes documentation](https://www.php.net/manual/en/language.attributes.php)
214+
3. [Open an issue](https://github.com/ray-di/Ray.AuraSqlModule/issues) on GitHub
215+
216+
## References
217+
218+
- [PHP 8 Attributes RFC](https://wiki.php.net/rfc/attributes_v2)
219+
- [Rector Documentation](https://github.com/rectorphp/rector)
220+
- [Ray.Di Documentation](https://ray-di.github.io/manuals/1.0/en/)

CHANGELOG.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
## [1.18.0]
1+
## [1.15.0] - 2025-11-09
22

33
### Changed
4-
- Updated Aura.Sql dependency from ^5.0 to ^6.0 for PHP 8.4 compatibility
5-
- Internal PDO handling improvements for PHP 8.4
4+
- **PHP 8.4 Requirement**: Updated minimum PHP version to ^8.4
5+
- **Aura.Sql v6**: Updated dependency from ^5.0 to ^6.0 for PHP 8.4 compatibility
6+
- **Removed doctrine/annotations**: Eliminated abandoned dependency, migrated to native PHP 8 attributes
7+
- **Code Modernization**: Applied Rector refactoring for cleaner, modern PHP 8.4 code
8+
9+
### Added
10+
- **Migration Tools**: Added `rector-migrate.php` for automated annotation-to-attribute migration
11+
- **Migration Guide**: Added `ANNOTATION_TO_ATTRIBUTE.md` with comprehensive migration instructions
12+
- **CI Enhancement**: Added Scrutinizer CI configuration for automated code quality analysis
613

714
### Note
8-
- **No code changes required** - all APIs remain identical
9-
- Automatic compatibility with both PHP 8.1-8.3 (Aura.Sql v5) and PHP 8.4+ (Aura.Sql v6)
15+
- **Migration Required**: Applications using annotations must migrate to PHP 8 attributes
16+
- Use provided Rector configuration for automated migration: `vendor/bin/rector process src --config=vendor/ray/aura-sql-module/rector-migrate.php`
17+
- See `ANNOTATION_TO_ATTRIBUTE.md` for detailed migration guide

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"aura/sql": "^6.0",
1616
"pagerfanta/pagerfanta": "^3.5 || ^4.7",
1717
"rize/uri-template": "^0.4",
18-
"doctrine/annotations": "^1.11 || ^2.0",
1918
"psr/log": "^1.1 || ^2.0 || ^3.0",
2019
"aura/sqlquery": "^3.0"
2120
},

rector-migrate.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
7+
use Rector\Php80\ValueObject\AnnotationToAttribute;
8+
9+
/**
10+
* Rector configuration for migrating from Doctrine Annotations to PHP 8 Attributes
11+
*
12+
* This configuration helps users migrate their code from doctrine/annotations
13+
* to native PHP 8 attributes for Ray.AuraSqlModule annotations.
14+
*
15+
* Usage:
16+
* vendor/bin/rector process src --config=rector-migrate.php --dry-run
17+
* vendor/bin/rector process src --config=rector-migrate.php
18+
*/
19+
return RectorConfig::configure()
20+
->withPaths([
21+
__DIR__ . '/src',
22+
__DIR__ . '/tests',
23+
])
24+
->withConfiguredRule(
25+
AnnotationToAttributeRector::class,
26+
[
27+
// Ray.AuraSqlModule Annotations
28+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\Transactional'),
29+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\WriteConnection'),
30+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\ReadOnlyConnection'),
31+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\Read'),
32+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\AuraSql'),
33+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\EnvAuth'),
34+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\HttpMethod'),
35+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\PagerViewOption'),
36+
new AnnotationToAttribute('Ray\AuraSqlModule\Annotation\AuraSqlQueryConfig'),
37+
]
38+
);

rector.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
__DIR__ . '/src-files',
1212
__DIR__ . '/tests',
1313
])
14-
// uncomment to reach your current PHP version
1514
->withPhpSets()
16-
->withSets(
17-
[
18-
PHPUnitSetList::PHPUNIT_90,
19-
PHPUnitSetList::PHPUNIT_100,
20-
PHPUnitSetList::PHPUNIT_110,
21-
]
22-
)
23-
->withTypeCoverageLevel(0)
24-
->withDeadCodeLevel(0)
25-
->withCodeQualityLevel(0);
15+
->withTypeCoverageLevel(1)
16+
->withDeadCodeLevel(1)
17+
->withCodeQualityLevel(1);

src/Annotation/AuraSql.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
use Attribute;
88

9-
/**
10-
* @Annotation
11-
* @Target("CLASS")
12-
*/
139
#[Attribute(Attribute::TARGET_CLASS)]
1410
final class AuraSql
1511
{

src/Annotation/AuraSqlQueryConfig.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55
namespace Ray\AuraSqlModule\Annotation;
66

77
use Attribute;
8-
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
98
use Ray\Di\Di\Qualifier;
109

11-
/**
12-
* @Annotation
13-
* @Target("METHOD")
14-
* @Qualifier
15-
* @NamedArgumentConstructor
16-
*/
1710
#[Attribute(Attribute::TARGET_METHOD), Qualifier]
1811
final class AuraSqlQueryConfig
1912
{

src/Annotation/EnvAuth.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
use Attribute;
88
use Ray\Di\Di\Qualifier;
99

10-
/**
11-
* @Annotation
12-
* @Target("METHOD")
13-
* @Qualifier
14-
*/
1510
#[Attribute(Attribute::TARGET_METHOD), Qualifier]
1611
final class EnvAuth
1712
{

src/Annotation/HttpMethod.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
use Attribute;
88

9-
/**
10-
* @Annotation
11-
* @Target("METHOD")
12-
*/
139
#[Attribute(Attribute::TARGET_METHOD)]
1410
final class HttpMethod
1511
{

0 commit comments

Comments
 (0)