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

[Privatization] Skip privatization in abstract only on open-source type #5514

Merged
merged 2 commits into from
Feb 11, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -57,6 +58,10 @@ public function collect(Node $node): void
}

$propertyType = $this->resolvePropertyCallerType($node);
if ($propertyType instanceof MixedType) {
return;
}

// make sure name is valid
if (StaticInstanceOf::isOneOf($node->name, [StaticCall::class, MethodCall::class])) {
return;
Expand Down Expand Up @@ -84,10 +89,10 @@ public function findPropertyFetchesByTypeAndName(string $className, string $prop
private function resolvePropertyCallerType(Node $node): Type
{
if ($node instanceof PropertyFetch) {
return $this->nodeTypeResolver->getStaticType($node->var);
return $this->nodeTypeResolver->resolve($node->var);
}

return $this->nodeTypeResolver->getStaticType($node->class);
return $this->nodeTypeResolver->resolve($node->class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ public function __construct(PropertyVisibilityVendorLockResolver $propertyVisibi

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Privatize local-only property to private property',
[
new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition('Privatize local-only property to private property', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public $value;
Expand All @@ -61,7 +59,7 @@ public function run()
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<<<'CODE_SAMPLE'
class SomeClass
{
private $value;
Expand All @@ -72,9 +70,8 @@ public function run()
}
}
CODE_SAMPLE
),

]);
),
]);
}

/**
Expand Down Expand Up @@ -106,7 +103,7 @@ public function refactor(Node $node): ?Node
$propertyClassName = $node->getAttribute(AttributeKey::CLASS_NAME);

// has external usage
if ([$propertyClassName] !== $usedPropertyFetchClassNames) {
if ($usedPropertyFetchClassNames !== [] && [$propertyClassName] !== $usedPropertyFetchClassNames) {
return null;
}

Expand Down Expand Up @@ -153,7 +150,13 @@ private function shouldSkipClass(ClassLike $classLike): bool
return true;
}

return $this->isObjectTypes($classLike, ['PHPUnit\Framework\TestCase', 'PHP_CodeSniffer\Sniffs\Sniff']);
if ($this->isObjectTypes($classLike, ['PHPUnit\Framework\TestCase', 'PHP_CodeSniffer\Sniffs\Sniff'])) {
return true;
}
if (! $classLike->isAbstract()) {
return false;
}
return $this->isOpenSourceProjectType();
}

private function shouldSkipProperty(Property $property): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Privatization\Tests\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector\Fixture;

abstract class IncludeAbstract
{
public $property;
}

?>
-----
<?php

namespace Rector\Privatization\Tests\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector\Fixture;

abstract class IncludeAbstract
{
private $property;
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Privatization\Tests\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector\Fixture;

class SkipUsedInTrait
final class SkipUsedInTrait
{
public $go;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Privatization\Tests\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector\FixtureOpenSource;

abstract class SkipProtectedAbstract
{
protected $property;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Privatization\Tests\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

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

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

protected function provideConfigFileInfo(): ?SmartFileInfo
{
return new SmartFileInfo(__DIR__ . '/config/fixture_open_source.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\ProjectType;
use Rector\Privatization\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE);

$services = $containerConfigurator->services();
$services->set(PrivatizeLocalPropertyToPrivatePropertyRector::class);
};