Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
- 'e2e/parallel-custom-config'
- 'e2e/parallel-reflection-resolver'
- 'e2e/no-parallel-reflection-resolver'
- 'e2e/typed-property-disallow-bc-break'

name: End to end test - ${{ matrix.directory }}

Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/e2e_allow_bc_break.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow runs system tests: Use the Rector application from the source
# checkout to process "fixture" projects in e2e/ directory
# to see if those can be processed successfully
name: End to End tests

on:
pull_request:
branches:
- main
push:
branches:
- main

env:
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361
COMPOSER_ROOT_VERSION: "dev-main"

jobs:
end_to_end:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version: ['8.1']
directory:
- 'e2e/typed-property-allow-bc-break'

name: End to end test Allow BC Break - ${{ matrix.directory }}

steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
coverage: none

# run in root rector-src
- run: composer install --ansi

# run in e2e subdir
-
run: composer install --ansi
working-directory: ${{ matrix.directory }}

# run e2e test
- run: php ../e2eTestRunner.php --allow-bc-break
working-directory: ${{ matrix.directory }}
1 change: 1 addition & 0 deletions build/target-repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ By [buying a book](https://leanpub.com/rector-the-power-of-automated-refactoring
- [Static Reflection and Autoload](/docs/static_reflection_and_autoload.md)
- [How to Configure Rule](/docs/how_to_configure_rules.md)
- [Beyond PHP - Entering the realm of FileProcessors](/docs/beyond_php_file_processors.md)
- [How to apply Major Change with BC-Break allowed](/docs/how_to_apply_major_change_with_bc_break_allowed.md)

### For Rule Developers and Contributors

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# How to Apply Major Change with BC-Break Allowed

Rector always try to make change to your code with preserve BC break as possible, for example, with `TypedPropertyRector`, you can always only change:

1. private property
2. protected property on final class without extends

For example:

```diff
class SomeClass
{
/** @var bool */
public $a;

/** @var bool */
protected $b;

- /** @var bool */
- private $c;
+ private bool $c;
```

What if you want to change all of them for `Major` change use case, eg, upgrade from version 1 to version 2 of your application? There is `Option::ALLOW_BC_BREAK` option for that, it can be run via `--allow-bc-break` in command:

```bash
vendor/bin/rector --allow-bc-break
```

so the change will be:

```diff
class SomeClass
{
- /** @var bool */
- private $a;
+ private bool $a;

- /** @var bool */
- private $b;
+ private bool $b;

- /** @var bool */
- private $c;
+ private bool $c;
```

### Note:

This Option is currently applied in the following rules:

- `TypedPropertyRector`
4 changes: 4 additions & 0 deletions e2e/e2eTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
$e2eCommand .= ' --config ' . $argv[2];
}

if (isset($argv[1]) && $argv[1] === '--allow-bc-break') {
$e2eCommand .= ' --allow-bc-break';
}

exec($e2eCommand, $output, $exitCode);
$output = trim(implode("\n", $output));

Expand Down
1 change: 1 addition & 0 deletions e2e/typed-property-allow-bc-break/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
5 changes: 5 additions & 0 deletions e2e/typed-property-allow-bc-break/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.1"
}
}
35 changes: 35 additions & 0 deletions e2e/typed-property-allow-bc-break/expected-output.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1 file with changes
===================

1) src/SomeTypedProperty.php:3

---------- begin diff ----------
@@ @@

class SomeTypedProperty
{
- /**
- * @var string
- */
- private $foo;
+ private string $foo;

- /**
- * @var string
- */
- protected $bar;
+ protected string $bar;

- /**
- * @var string
- */
- public $baz;
+ public string $baz;
}
----------- end diff -----------

Applied rules:
* TypedPropertyRector (https://wiki.php.net/rfc/typed_properties_v2#proposal)


[OK] 1 file would have changed (dry-run) by Rector
19 changes: 19 additions & 0 deletions e2e/typed-property-allow-bc-break/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::PATHS, [
__DIR__ . '/src/',
]);

$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class);
};

21 changes: 21 additions & 0 deletions e2e/typed-property-allow-bc-break/src/SomeTypedProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

class SomeTypedProperty
{
/**
* @var string
*/
private $foo;

/**
* @var string
*/
protected $bar;

/**
* @var string
*/
public $baz;
}
1 change: 1 addition & 0 deletions e2e/typed-property-disallow-bc-break/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
5 changes: 5 additions & 0 deletions e2e/typed-property-disallow-bc-break/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^8.1"
}
}
25 changes: 25 additions & 0 deletions e2e/typed-property-disallow-bc-break/expected-output.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
1 file with changes
===================

1) src/SomeTypedProperty.php:3

---------- begin diff ----------
@@ @@

class SomeTypedProperty
{
- /**
- * @var string
- */
- private $foo;
+ private string $foo;

/**
* @var string
----------- end diff -----------

Applied rules:
* TypedPropertyRector (https://wiki.php.net/rfc/typed_properties_v2#proposal)


[OK] 1 file would have changed (dry-run) by Rector
19 changes: 19 additions & 0 deletions e2e/typed-property-disallow-bc-break/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::PATHS, [
__DIR__ . '/src/',
]);

$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class);
};

21 changes: 21 additions & 0 deletions e2e/typed-property-disallow-bc-break/src/SomeTypedProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

class SomeTypedProperty
{
/**
* @var string
*/
private $foo;

/**
* @var string
*/
protected $bar;

/**
* @var string
*/
public $baz;
}
5 changes: 5 additions & 0 deletions rules/Php74/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Configuration\Option;
use Rector\Core\NodeAnalyzer\PropertyAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\AstResolver;
Expand Down Expand Up @@ -249,6 +250,10 @@ private function shouldSkipProperty(Property $property, Scope $scope): bool
return true;
}

if ($this->parameterProvider->provideBoolParameter(Option::ALLOW_BC_BREAK)) {
return $this->propertyAnalyzer->hasForbiddenType($property);
}

if ($property->isPrivate()) {
return $this->propertyAnalyzer->hasForbiddenType($property);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function createFromInput(InputInterface $input): Configuration

/** @var string|null $memoryLimit */
$memoryLimit = $input->getOption(Option::MEMORY_LIMIT);
$allowBcBreak = (bool) $input->getOption(Option::ALLOW_BC_BREAK);

if ($allowBcBreak) {
$this->parameterProvider->changeParameter(Option::ALLOW_BC_BREAK, true);
}

return new Configuration(
$isDryRun,
Expand Down
5 changes: 5 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,9 @@ final class Option
* @var string
*/
public const MEMORY_LIMIT = 'memory-limit';

/**
* @var string
*/
public const ALLOW_BC_BREAK = 'allow-bc-break';
}
7 changes: 7 additions & 0 deletions src/Console/Command/AbstractProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,12 @@ protected function configure(): void

$this->addOption(Option::PARALLEL_PORT, null, InputOption::VALUE_REQUIRED);
$this->addOption(Option::PARALLEL_IDENTIFIER, null, InputOption::VALUE_REQUIRED);

$this->addOption(
Option::ALLOW_BC_BREAK,
null,
InputOption::VALUE_NONE,
'Verify whether rector rules should apply BC break changes',
);
}
}