mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
[Php74] Add privatePropertyOnly config to TypedPropertyRector (#898)
This commit is contained in:
parent
3ff17513c4
commit
b5516368d4
|
@ -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,
|
||||
]]);
|
||||
};
|
||||
```
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
]]);
|
||||
};
|
|
@ -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,
|
||||
|
@ -87,6 +98,7 @@ CODE_SAMPLE
|
|||
,
|
||||
[
|
||||
self::CLASS_LIKE_TYPE_ONLY => false,
|
||||
self::PRIVATE_PROPERTY_ONLY => false,
|
||||
]
|
||||
),
|
||||
]
|
||||
|
@ -158,6 +170,7 @@ CODE_SAMPLE
|
|||
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
|
||||
|
@ -259,6 +272,10 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
// skip multiple properties
|
||||
return count($property->props) > 1;
|
||||
if (count($property->props) > 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->privatePropertyOnly && ! $property->isPrivate();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user