Skip to content

Commit

Permalink
[Php74] Add privatePropertyOnly config to TypedPropertyRector (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Sep 19, 2021
1 parent 3ff1751 commit b551636
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7638,6 +7638,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(TypedPropertyRector::class)
->call('configure', [[
TypedPropertyRector::CLASS_LIKE_TYPE_ONLY => false,
TypedPropertyRector::PRIVATE_PROPERTY_ONLY => false,
]]);
};
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixturePrivatePropertyOnly;

class Fixture
{
/**
* @var bool
*/
private $privateProperty;

/**
* @var bool
*/
protected $protectedProperty;
}

class ExtendsFixture extends Fixture
{
/**
* @var bool
*/
protected $protectedProperty;
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\Property\TypedPropertyRector\FixturePrivatePropertyOnly;

class Fixture
{
private bool $privateProperty;

/**
* @var bool
*/
protected $protectedProperty;
}

class ExtendsFixture extends Fixture
{
/**
* @var bool
*/
protected $protectedProperty;
}

?>
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 PrivatePropertyOnlyTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePrivatePropertyOnly');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/private_property_only.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

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

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);

$services = $containerConfigurator->services();
$services->set(TypedPropertyRector::class)
->call('configure', [[
TypedPropertyRector::PRIVATE_PROPERTY_ONLY => true,
]]);
};
19 changes: 18 additions & 1 deletion rules/Php74/Rector/Property/TypedPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,22 @@ final class TypedPropertyRector extends AbstractRector implements ConfigurableRe
*/
public const CLASS_LIKE_TYPE_ONLY = 'class_like_type_only';

/**
* @var string
*/
public const PRIVATE_PROPERTY_ONLY = 'PRIVATE_PROPERTY_ONLY';

/**
* Useful for refactoring of huge applications. Taking types first narrows scope
*/
private bool $classLikeTypeOnly = false;

/**
* If want to keep BC, it can be set to true
* @see https://3v4l.org/spl4P
*/
private bool $privatePropertyOnly = false;

public function __construct(
private PropertyTypeInferer $propertyTypeInferer,
private VendorLockResolver $vendorLockResolver,
Expand Down Expand Up @@ -87,6 +98,7 @@ final class SomeClass
,
[
self::CLASS_LIKE_TYPE_ONLY => false,
self::PRIVATE_PROPERTY_ONLY => false,
]
),
]
Expand Down Expand Up @@ -158,6 +170,7 @@ public function refactor(Node $node): ?Node
public function configure(array $configuration): void
{
$this->classLikeTypeOnly = $configuration[self::CLASS_LIKE_TYPE_ONLY] ?? false;
$this->privatePropertyOnly = $configuration[self::PRIVATE_PROPERTY_ONLY] ?? false;
}

public function provideMinPhpVersion(): int
Expand Down Expand Up @@ -259,6 +272,10 @@ private function shouldSkipProperty(Property $property): bool
}

// skip multiple properties
return count($property->props) > 1;
if (count($property->props) > 1) {
return true;
}

return $this->privatePropertyOnly && ! $property->isPrivate();
}
}

0 comments on commit b551636

Please sign in to comment.