[Php74] Add inlinePublic configurable for TypedPropertyRector (#1745)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-02-03 04:06:51 +07:00 committed by GitHub
parent 8320d29003
commit 29c634ff65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 169 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# 520 Rules Overview
# 521 Rules Overview
<br>
@ -38,7 +38,7 @@
- [DowngradePhp74](#downgradephp74) (12)
- [DowngradePhp80](#downgradephp80) (27)
- [DowngradePhp80](#downgradephp80) (28)
- [DowngradePhp81](#downgradephp81) (8)
@ -5746,6 +5746,33 @@ Downgrade `str_starts_with()` to `strncmp()` version
<br>
### DowngradeStringReturnTypeOnToStringRector
Add "string" return on current `__toString()` method when parent method has string return on `__toString()` method
- class: [`Rector\DowngradePhp80\Rector\ClassMethod\DowngradeStringReturnTypeOnToStringRector`](../rules/DowngradePhp80/Rector/ClassMethod/DowngradeStringReturnTypeOnToStringRector.php)
```diff
abstract class ParentClass
{
public function __toString(): string
{
return 'value';
}
}
class ChildClass extends ParentClass
{
- public function __toString()
+ public function __toString(): string
{
return 'value';
}
}
```
<br>
### DowngradeThrowExprRector
Downgrade throw expression
@ -8040,8 +8067,26 @@ Add null default to properties with PHP 7.4 property nullable type
Changes property `@var` annotations from annotation to type.
:wrench: **configure it!**
- class: [`Rector\Php74\Rector\Property\TypedPropertyRector`](../rules/Php74/Rector/Property/TypedPropertyRector.php)
```php
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class)
->configure([
TypedPropertyRector::INLINE_PUBLIC => false,
]);
};
```
```diff
final class SomeClass
{

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureInlinePublic;
final class SimpleArray
{
/**
* @var array
*/
private $foo;
/**
* @var array
*/
protected $bar;
/**
* @var array
*/
public $baz;
}
?>
-----
<?php
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixtureInlinePublic;
final class SimpleArray
{
private array $foo;
protected array $bar;
public array $baz;
}
?>

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class InlinePublicTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureInlinePublic');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/inline_public_configured_rule.php';
}
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class)
->configure([
TypedPropertyRector::INLINE_PUBLIC => true,
]);
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::UNION_TYPES - 1);
};

View File

@ -18,6 +18,7 @@ use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\PropertyAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\AstResolver;
@ -32,7 +33,7 @@ use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer;
use Rector\VendorLocker\VendorLockResolver;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -43,8 +44,23 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
* @see \Rector\Tests\Php74\Rector\Property\TypedPropertyRector\DoctrineTypedPropertyRectorTest
* @see \Rector\Tests\Php74\Rector\Property\TypedPropertyRector\ImportedTest
*/
final class TypedPropertyRector extends AbstractRector implements MinPhpVersionInterface
final class TypedPropertyRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface, MinPhpVersionInterface
{
/**
* @var string
*/
public const INLINE_PUBLIC = 'inline_public';
/**
* Default to false, which only apply changes:
*
* private modifier property
* - protected modifier property on final class without extends
*
* Set to true will allow change other modifiers as well as far as not forbidden, eg: callable type, null type, etc.
*/
private bool $inlinePublic = false;
public function __construct(
private readonly VarDocPropertyTypeInferer $varDocPropertyTypeInferer,
private readonly VendorLockResolver $vendorLockResolver,
@ -58,12 +74,17 @@ final class TypedPropertyRector extends AbstractRector implements MinPhpVersionI
) {
}
public function configure(array $configuration): void
{
$this->inlinePublic = $configuration[self::INLINE_PUBLIC] ?? (bool) current($configuration);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes property `@var` annotations from annotation to type.',
[
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
@ -80,6 +101,10 @@ final class SomeClass
private int $count;
}
CODE_SAMPLE
,
[
self::INLINE_PUBLIC => false,
]
),
]
);
@ -249,6 +274,10 @@ CODE_SAMPLE
return true;
}
if ($this->inlinePublic) {
return $this->propertyAnalyzer->hasForbiddenType($property);
}
if ($property->isPrivate()) {
return $this->propertyAnalyzer->hasForbiddenType($property);
}