Skip to content

Commit 2944625

Browse files
mnocondabrt
andauthored
[4.6] Added Rector downgradesets (#3107)
* Added downgrade sets for Rector, targetting PHP 7.4 and Symfony 5 * Downgrades code samples * Added README * Apply suggestion from @mnocon * Apply suggestions from code review Co-authored-by: Tomasz Dąbrowski <64841871+dabrt@users.noreply.github.com> --------- Co-authored-by: Tomasz Dąbrowski <64841871+dabrt@users.noreply.github.com>
1 parent 3a78125 commit 2944625

7 files changed

Lines changed: 65 additions & 4 deletions

File tree

.github/workflows/code_samples.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
name: Validate code samples
1313
runs-on: "ubuntu-22.04"
1414
strategy:
15+
fail-fast: false
1516
matrix:
1617
php:
1718
- "8.4" # Upper supported version
@@ -56,6 +57,12 @@ jobs:
5657

5758
- name: Run PHPStan analysis
5859
run: composer phpstan
60+
61+
- if: matrix.php == '8.4' # ibexa/rector supports PHP 8.3 and higher
62+
name: Run Rector check
63+
run: |
64+
composer require --dev ibexa/rector:~4.6.x-dev
65+
composer check-rector
5966
6067
code-samples-inclusion-check:
6168
name: Check code samples inclusion

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ composer update
8383
composer phpstan
8484
```
8585

86+
## Downgrading code samples with Rector
87+
88+
Code samples must be compatible with PHP 7.4.
89+
Rector is used to check and downgrade PHP 8.x syntax and functions to PHP 7.4-compatible equivalents.
90+
91+
To check which files need downgrading (dry-run, no changes applied), run:
92+
```bash
93+
composer require --dev ibexa/rector:~4.6.x-dev
94+
composer check-rector
95+
```
96+
97+
To refactor the code samples, run:
98+
99+
``` bash
100+
vendor/bin/rector
101+
```
102+
86103
## Where to View
87104

88105
https://doc.ibexa.co

code_samples/api/commerce/src/Command/PaymentCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737
UserService $userService,
3838
PaymentServiceInterface $paymentService,
3939
OrderServiceInterface $orderService,
40-
PaymentMethodServiceInterface $paymentMethodService,
40+
PaymentMethodServiceInterface $paymentMethodService
4141
) {
4242
$this->paymentService = $paymentService;
4343
$this->permissionResolver = $permissionResolver;

code_samples/back_office/search/src/EventSubscriber/MySuggestionEventSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MySuggestionEventSubscriber implements EventSubscriberInterface, LoggerAwa
1818
private ProductServiceInterface $productService;
1919

2020
public function __construct(
21-
ProductServiceInterface $productService,
21+
ProductServiceInterface $productService
2222
) {
2323
$this->productService = $productService;
2424
}

code_samples/data_migration/src/Migrations/Step/ReplaceNameStepExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function doHandle(StepInterface $step)
3838
continue;
3939
}
4040

41-
if (str_contains($field->value, 'Company Name')) {
41+
if (strpos($field->value, 'Company Name') !== false) {
4242
$newValue = str_replace('Company Name', $step->getReplacement(), $field->value);
4343
$struct->setField($field->fieldDefIdentifier, new Value($newValue));
4444
}

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"url": "https://updates.ibexa.co"
1212
}
1313
],
14+
"require": {
15+
"php": "^7.4 || ^8.0"
16+
},
1417
"require-dev": {
1518
"ibexa/automated-translation": "4.6.x-dev",
1619
"ibexa/code-style": "^1.0",
@@ -83,7 +86,14 @@
8386
"scripts": {
8487
"fix-cs": "php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots",
8588
"check-cs": "@fix-cs --dry-run",
86-
"phpstan": "phpstan analyse"
89+
"phpstan": "phpstan analyse",
90+
"check-rector": "rector process --dry-run --ansi"
91+
},
92+
"suggest": {
93+
"ibexa/rector": "Add Rector to check for code refactoring opportunities"
94+
},
95+
"scripts-descriptions": {
96+
"check-rector": "Check for code refactoring opportunities"
8797
},
8898
"config": {
8999
"allow-plugins": false

rector.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
use Rector\Config\RectorConfig;
10+
use Ibexa\Contracts\Rector\Sets\IbexaSetList;
11+
use Rector\Symfony\DowngradeSymfony70\Rector\Class_\DowngradeSymfonyCommandAttributeRector;
12+
use Rector\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector;
13+
14+
return RectorConfig::configure()
15+
->withPaths([
16+
__DIR__ . '/code_samples',
17+
])
18+
->withSkip([
19+
DowngradeIteratorCountToArrayRector::class,
20+
])
21+
->withSets([
22+
IbexaSetList::IBEXA_46->value,
23+
])
24+
->withDowngradeSets(php74: true)
25+
->withRules([
26+
DowngradeSymfonyCommandAttributeRector::class,
27+
]);

0 commit comments

Comments
 (0)