Skip to content

Commit

Permalink
[Php74] Add inlinePublic configurable for TypedPropertyRector (#1745)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 2, 2022
1 parent 8320d29 commit 29c634f
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 5 deletions.
49 changes: 47 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 520 Rules Overview
# 521 Rules Overview

<br>

Expand Down Expand Up @@ -38,7 +38,7 @@

- [DowngradePhp74](#downgradephp74) (12)

- [DowngradePhp80](#downgradephp80) (27)
- [DowngradePhp80](#downgradephp80) (28)

- [DowngradePhp81](#downgradephp81) (8)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

?>
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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);
};
35 changes: 32 additions & 3 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\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\PropertyAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\AstResolver;
Expand All @@ -32,7 +33,7 @@
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;

/**
Expand All @@ -43,8 +44,23 @@
* @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,
Expand All @@ -58,12 +74,17 @@ public function __construct(
) {
}

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
{
Expand All @@ -80,6 +101,10 @@ final class SomeClass
private int $count;
}
CODE_SAMPLE
,
[
self::INLINE_PUBLIC => false,
]
),
]
);
Expand Down Expand Up @@ -249,6 +274,10 @@ private function shouldSkipProperty(Property $property, Scope $scope): bool
return true;
}

if ($this->inlinePublic) {
return $this->propertyAnalyzer->hasForbiddenType($property);
}

if ($property->isPrivate()) {
return $this->propertyAnalyzer->hasForbiddenType($property);
}
Expand Down

0 comments on commit 29c634f

Please sign in to comment.