diff --git a/.github/workflows/code_samples.yaml b/.github/workflows/code_samples.yaml index e63e3545f4..ab275d3395 100644 --- a/.github/workflows/code_samples.yaml +++ b/.github/workflows/code_samples.yaml @@ -8,6 +8,7 @@ jobs: name: Validate code samples runs-on: "ubuntu-22.04" strategy: + fail-fast: false matrix: php: - "8.4" # Upper supported version @@ -47,6 +48,12 @@ jobs: - name: Run PHPStan analysis run: composer phpstan + + - if: matrix.php == '8.4' # ibexa/rector supports PHP 8.3 and higher + name: Run Rector check + run: | + composer require --dev ibexa/rector:~4.6.x-dev + composer check-rector code-samples-inclusion-check: name: Check code samples inclusion diff --git a/README.md b/README.md index 18687db596..77b0d848b1 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,23 @@ composer update composer phpstan ``` +## Downgrading code samples with Rector + +Code samples must be compatible with PHP 7.4. +Rector is used to check and downgrade PHP 8.x syntax and functions to PHP 7.4-compatible equivalents. + +To check which files need downgrading (dry-run, no changes applied): +```bash +composer require --dev ibexa/rector:~4.6.x-dev +composer check-rector +``` + +To refactor the code samples, run: + +``` bash +vendor/bin/rector +``` + ## Where to View https://doc.ibexa.co diff --git a/code_samples/api/commerce/src/Command/PaymentCommand.php b/code_samples/api/commerce/src/Command/PaymentCommand.php index f18b0b6115..792441ba91 100644 --- a/code_samples/api/commerce/src/Command/PaymentCommand.php +++ b/code_samples/api/commerce/src/Command/PaymentCommand.php @@ -37,7 +37,7 @@ public function __construct( UserService $userService, PaymentServiceInterface $paymentService, OrderServiceInterface $orderService, - PaymentMethodServiceInterface $paymentMethodService, + PaymentMethodServiceInterface $paymentMethodService ) { $this->paymentService = $paymentService; $this->permissionResolver = $permissionResolver; diff --git a/code_samples/back_office/search/src/EventSubscriber/MySuggestionEventSubscriber.php b/code_samples/back_office/search/src/EventSubscriber/MySuggestionEventSubscriber.php index ce59700b51..dca46b7ae0 100644 --- a/code_samples/back_office/search/src/EventSubscriber/MySuggestionEventSubscriber.php +++ b/code_samples/back_office/search/src/EventSubscriber/MySuggestionEventSubscriber.php @@ -18,7 +18,7 @@ class MySuggestionEventSubscriber implements EventSubscriberInterface, LoggerAwa private ProductServiceInterface $productService; public function __construct( - ProductServiceInterface $productService, + ProductServiceInterface $productService ) { $this->productService = $productService; } diff --git a/code_samples/data_migration/src/Migrations/Step/ReplaceNameStepExecutor.php b/code_samples/data_migration/src/Migrations/Step/ReplaceNameStepExecutor.php index 6ea22d920e..dc507d8f51 100644 --- a/code_samples/data_migration/src/Migrations/Step/ReplaceNameStepExecutor.php +++ b/code_samples/data_migration/src/Migrations/Step/ReplaceNameStepExecutor.php @@ -38,7 +38,7 @@ protected function doHandle(StepInterface $step) continue; } - if (str_contains($field->value, 'Company Name')) { + if (strpos($field->value, 'Company Name') !== false) { $newValue = str_replace('Company Name', $step->getReplacement(), $field->value); $struct->setField($field->fieldDefIdentifier, new Value($newValue)); } diff --git a/composer.json b/composer.json index 2467d6a85f..3cae9b8374 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,9 @@ "url": "https://updates.ibexa.co" } ], + "require": { + "php": "^7.4 || ^8.0" + }, "require-dev": { "ibexa/automated-translation": "4.6.x-dev", "ibexa/code-style": "^1.0", @@ -82,7 +85,14 @@ "scripts": { "fix-cs": "php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots", "check-cs": "@fix-cs --dry-run", - "phpstan": "phpstan analyse" + "phpstan": "phpstan analyse", + "check-rector": "rector process --dry-run --ansi" + }, + "suggest": { + "ibexa/rector": "Add Rector to check for code refactoring opportunities" + }, + "scripts-descriptions": { + "check-rector": "Check for code refactoring opportunities" }, "config": { "allow-plugins": false diff --git a/rector.php b/rector.php new file mode 100644 index 0000000000..f65f3d9c82 --- /dev/null +++ b/rector.php @@ -0,0 +1,27 @@ +withPaths([ + __DIR__ . '/code_samples', + ]) + ->withSkip([ + DowngradeIteratorCountToArrayRector::class, + ]) + ->withSets([ + IbexaSetList::IBEXA_46->value, + ]) + ->withDowngradeSets(php74: true) + ->withRules([ + DowngradeSymfonyCommandAttributeRector::class, + ]);