From 8d8d9737832462d623910537e8afed251cfc6c0d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 7 Mar 2025 13:57:40 +0100 Subject: [PATCH] [docs] add attribute section --- ...ow-to-upgrade-annotations-to-attributes.md | 31 +++++++-------- resources/docs/attributes.md | 35 +++++++++++++++++ resources/docs/set-lists.md | 38 ------------------- .../DocumentationMenuFactory.php | 1 + 4 files changed, 50 insertions(+), 55 deletions(-) create mode 100644 resources/docs/attributes.md diff --git a/resources/blog/posts/2021/2021-10-11-how-to-upgrade-annotations-to-attributes.md b/resources/blog/posts/2021/2021-10-11-how-to-upgrade-annotations-to-attributes.md index f770a4bf9..bd72e06c1 100644 --- a/resources/blog/posts/2021/2021-10-11-how-to-upgrade-annotations-to-attributes.md +++ b/resources/blog/posts/2021/2021-10-11-how-to-upgrade-annotations-to-attributes.md @@ -8,11 +8,9 @@ perex: | Do you want to know the answer? Rector has a simple solution for you. -updated_at: '2022-04' +updated_at: '2025-03' updated_message: | - Since **Rector 0.12** a new `RectorConfig` is available with simpler and easier to use config methods. - -since_rector: 0.12 + Since **Rector 2.0** a new `withAttributesSets()` configuration method is available. --- One package that added support for attributes is Doctrine: @@ -30,23 +28,22 @@ Now, let's go to upgrade itself. It's effortless. ## Upgrade from Annotations to Attributes in 3 Steps -### 1. Configure `rector.php` to include the packages you use: +### 1. Configure `rector.php` to include all available attributes: + +```php +use Rector\Config\RectorConfig; + +return RectorConfig::configure() + ->withAttributesSets(); +``` + +Or limit to specific group - this option is better when you're addin Rector to a new project: ```php -use Rector\Doctrine\Set\DoctrineSetList; -use Rector\Symfony\Set\SymfonySetList; -use Rector\Symfony\Set\SensiolabsSetList; -use Rector\Nette\Set\NetteSetList; use Rector\Config\RectorConfig; -return function (RectorConfig $rectorConfig): void { - $rectorConfig->sets([ - DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES, - SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES, - NetteSetList::ANNOTATIONS_TO_ATTRIBUTES, - SensiolabsSetList::FRAMEWORK_EXTRA_61, - ]); -}; +return RectorConfig::configure() + ->withAttributesSets(symfony: true); ```
diff --git a/resources/docs/attributes.md b/resources/docs/attributes.md new file mode 100644 index 000000000..b85898ba4 --- /dev/null +++ b/resources/docs/attributes.md @@ -0,0 +1,35 @@ +Do you want to migrate your annotations to native PHP 8.0 attributes? + +```diff + use Doctrine\ORM\Mapping as ORM; + +-/** +- * @ORM\Entity +- */ ++#[ORM\Entity] + class SomeEntity + { + } +``` + +Following method will automatically pick up attribute classes present in your `/vendor`, and upgrade annotations to their attribute equivalent: + +```php +withAttributesSets(); +``` + +**If you're on a legacy project** and want to take it step by step, use named arguments to limit to specific groups: + +```php +withAttributesSets(symfony: true, doctrine: true); +``` diff --git a/resources/docs/set-lists.md b/resources/docs/set-lists.md index 26e0c392c..9f963374e 100644 --- a/resources/docs/set-lists.md +++ b/resources/docs/set-lists.md @@ -60,44 +60,6 @@ return RectorConfig::configure() That way you can use all the sets that are needed to upgrade your code to the desired PHP version in single line. -## PHP 8.0 Attributes - -Do you want to migrate your annotations to native PHP 8.0 attributes? - -```diff - use Doctrine\ORM\Mapping as ORM; - --/** -- * @ORM\Entity -- */ -+#[ORM\Entity] - class SomeEntity - { - } -``` - -Following method will automatically pick up attribute classes present in your `/vendor`, and upgrade annotations to their attribute equivalent: - -```php -withAttributesSets(); -``` - -**If you're on a legacy project** and want to take it step by step, use named arguments to limit to specific groups: - -```php -withAttributesSets(symfony: true, doctrine: true); -``` - ## Community or External Sets How can I use Rector with community sets or my custom one? diff --git a/src/Documentation/DocumentationMenuFactory.php b/src/Documentation/DocumentationMenuFactory.php index fc2aa4b5c..998fa557b 100644 --- a/src/Documentation/DocumentationMenuFactory.php +++ b/src/Documentation/DocumentationMenuFactory.php @@ -27,6 +27,7 @@ public function create(): array $this->documentationMenuItemFactory->createSection('define-paths', 'Define Paths'), $this->documentationMenuItemFactory->createSection('set-lists', 'Set Lists'), $this->documentationMenuItemFactory->createSection('levels', 'Levels', true), + $this->documentationMenuItemFactory->createSection('attributes', 'PHP 8.0 Attributes', true), $this->documentationMenuItemFactory->createSection('composer-based-sets', 'Composer-Based Sets', true), $this->documentationMenuItemFactory->createInternalLink(FindRuleController::class, 'Find Rules'), $this->documentationMenuItemFactory->createSection(