Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Downgrade] [PHP 8.0] Add DowngradeThrowExpressionRector #4766

Closed
wants to merge 3 commits into from
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
2 changes: 2 additions & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Downgrade\Rector\LNumber\ChangePhpVersionInPlatformCheckRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradeThrowExpressionRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnMixedTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnStaticTypeDeclarationRector;
Expand All @@ -24,4 +25,5 @@
ChangePhpVersionInPlatformCheckRector::TARGET_PHP_VERSION => 80000,
]]);
$services->set(DowngradePropertyPromotionToConstructorPropertyAssignRector::class);
$services->set(DowngradeThrowExpressionRector::class);
};
28 changes: 4 additions & 24 deletions config/set/nette-30-param-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,10 @@
),
new AddParamTypeDeclaration('Nette\ComponentModel\IComponent', 'setParent', 1, new StringType()),
new AddParamTypeDeclaration('Nette\ComponentModel\IContainer', 'getComponents', 0, new BooleanType()),
new AddParamTypeDeclaration(
'Nette\Forms\Container',
'addSelect',
0,
new StringType()
),
new AddParamTypeDeclaration(
'Nette\Forms\Container',
'addSelect',
3,
new IntegerType()
),
new AddParamTypeDeclaration(
'Nette\Forms\Container',
'addMultiSelect',
0,
new StringType()
),
new AddParamTypeDeclaration(
'Nette\Forms\Container',
'addMultiSelect',
3,
new IntegerType()
),
new AddParamTypeDeclaration('Nette\Forms\Container', 'addSelect', 0, new StringType()),
new AddParamTypeDeclaration('Nette\Forms\Container', 'addSelect', 3, new IntegerType()),
new AddParamTypeDeclaration('Nette\Forms\Container', 'addMultiSelect', 0, new StringType()),
new AddParamTypeDeclaration('Nette\Forms\Container', 'addMultiSelect', 3, new IntegerType()),
new AddParamTypeDeclaration(
'Nette\Forms\Rendering\DefaultFormRenderer',
'render',
Expand Down
6 changes: 1 addition & 5 deletions config/set/nette-30-return-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@
'addMultiSelect',
new ObjectType('Nette\Forms\Controls\MultiSelectBox')
),
new AddReturnTypeDeclaration(
'Nette\Forms\IFormRenderer',
'render',
new StringType()
),
new AddReturnTypeDeclaration('Nette\Forms\IFormRenderer', 'render', new StringType()),
new AddReturnTypeDeclaration(
'Nette\Forms\Controls\TextBase',
'getControl',
Expand Down
2 changes: 1 addition & 1 deletion config/set/nette-30.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
# @see https://github.com/nette/di/commit/a0d361192f8ac35f1d9f82aab7eb351e4be395ea
'Nette\DI\ServiceDefinition' => 'Nette\DI\Definitions\ServiceDefinition',
'Nette\DI\Statement' => 'Nette\DI\Definitions\Statement',
'WebChemistry\Forms\Controls\Multiplier' => 'Contributte\FormMultiplier\Multiplier'
'WebChemistry\Forms\Controls\Multiplier' => 'Contributte\FormMultiplier\Multiplier',
],
]]);
$services->set(ArgumentDefaultValueReplacerRector::class)->call('configure', [[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp80\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://wiki.php.net/rfc/throw_expression
*
* @see \Rector\DowngradePhp80\Tests\Rector\Class_\DowngradeThrowExpressionRector\DowngradeThrowExpressionRectorTest
*/
final class DowngradeThrowExpressionRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change constructor property promotion to property asssign', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct($nullableValue)
{
return $nullableValue ?? throw new InvalidArgumentException()
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct($nullableValue)
{
if ($nullableValue !== null) {
return $nullableValue;
}

throw new InvalidArgumentException()
}
}
CODE_SAMPLE

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
// change the node

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp80\Tests\Rector\Class_\DowngradeThrowExpressionRector;

use Iterator;
use Rector\DowngradePhp80\Rector\Class_\DowngradeThrowExpressionRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeThrowExpressionRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return DowngradeThrowExpressionRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\DowngradePhp80\Tests\Rector\Class_\DowngradeThrowExpressionRector\Fixture;

class SomeClass
{
public function __construct($nullableValue)
{
return $nullableValue ?? throw new InvalidArgumentException()
}
}

?>
-----
<?php

namespace Rector\DowngradePhp80\Tests\Rector\Class_\DowngradeThrowExpressionRector\Fixture;

class SomeClass
{
public function __construct($nullableValue)
{
if ($nullableValue !== null) {
return $nullableValue;
}

throw new InvalidArgumentException()
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function ternaryToNullCoalescing()

$x = (isset($a) ? $a : isset($b)) ? $b : "";

$x = isset($a) ? $a : isset($b) ? $b : isset($c) ? $c : "";
$x = ((isset($a) ? $a : isset($b)) ? $b : isset($c)) ? $c : "";
}

?>
Expand All @@ -39,7 +39,7 @@ function ternaryToNullCoalescing()

$x = ($a ?? isset($b)) ? $b : "";

$x = $a ?? isset($b) ? $b : isset($c) ? $c : "";
$x = (($a ?? isset($b)) ? $b : isset($c)) ? $c : "";
}

?>