Skip to content

Commit

Permalink
[Core] Remove ParentAttributeSourceLocator (#3257)
Browse files Browse the repository at this point in the history
* [Core] Remove ParentAttributeSourceLocator

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 31, 2022
1 parent 6bd6b3c commit eadd540
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Caching\FileSystem\DependencyResolver;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\StaticReflection\SourceLocator\ParentAttributeSourceLocator;
use Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\Core\Util\StringUtils;
Expand Down Expand Up @@ -81,7 +80,6 @@ public function __construct(
private readonly ScopeFactory $scopeFactory,
private readonly PrivatesAccessor $privatesAccessor,
private readonly RenamedClassesSourceLocator $renamedClassesSourceLocator,
private readonly ParentAttributeSourceLocator $parentAttributeSourceLocator,
private readonly NodeNameResolver $nodeNameResolver
) {
$this->decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator($this->nodeScopeResolver);
Expand Down Expand Up @@ -454,11 +452,7 @@ private function decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator(
);

// 2. get Rector locator
$aggregateSourceLocator = new AggregateSourceLocator([
$sourceLocator,
$this->renamedClassesSourceLocator,
$this->parentAttributeSourceLocator,
]);
$aggregateSourceLocator = new AggregateSourceLocator([$sourceLocator, $this->renamedClassesSourceLocator]);
$this->privatesAccessor->setPrivatePropertyOfClass(
$reflector,
'sourceLocator',
Expand Down
40 changes: 21 additions & 19 deletions rules/Php80/Rector/Identical/StrEndsWithRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function run()
$isNotMatch = substr($haystack, -strlen($needle)) !== $needle;
}
}
CODE_SAMPLE,
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
Expand All @@ -82,7 +83,8 @@ public function run()
$isNotMatch = substr($haystack, -9) !== 'hardcoded';
}
}
CODE_SAMPLE,
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
Expand Down Expand Up @@ -131,15 +133,15 @@ private function refactorSubstr(BinaryOp $binaryOp): FuncCall | BooleanNot | nul
return null;
}

if (!$this->argsAnalyzer->isArgsInstanceInArgsPositions($substrFuncCall->args, [0, 1])) {
if (! $this->argsAnalyzer->isArgsInstanceInArgsPositions($substrFuncCall->args, [0, 1])) {
return null;
}

/** @var Arg $secondArg */
$secondArg = $substrFuncCall->args[1];
if (
!$this->isUnaryMinusStrlenFuncCallArgValue($secondArg->value, $comparedNeedleExpr) &&
!$this->isHardCodedLNumberAndString($secondArg->value, $comparedNeedleExpr)
! $this->isUnaryMinusStrlenFuncCallArgValue($secondArg->value, $comparedNeedleExpr) &&
! $this->isHardCodedLNumberAndString($secondArg->value, $comparedNeedleExpr)
) {
return null;
}
Expand All @@ -155,17 +157,17 @@ private function refactorSubstr(BinaryOp $binaryOp): FuncCall | BooleanNot | nul
private function refactorSubstrCompare(BinaryOp $binaryOp): FuncCall | BooleanNot | null
{
$funcCallAndExpr = $this->binaryOpAnalyzer->matchFuncCallAndOtherExpr($binaryOp, 'substr_compare');
if (!$funcCallAndExpr instanceof FuncCallAndExpr) {
if (! $funcCallAndExpr instanceof FuncCallAndExpr) {
return null;
}

$expr = $funcCallAndExpr->getExpr();
if (!$this->valueResolver->isValue($expr, 0)) {
if (! $this->valueResolver->isValue($expr, 0)) {
return null;
}

$substrCompareFuncCall = $funcCallAndExpr->getFuncCall();
if (!$this->argsAnalyzer->isArgsInstanceInArgsPositions($substrCompareFuncCall->args, [0, 1, 2])) {
if (! $this->argsAnalyzer->isArgsInstanceInArgsPositions($substrCompareFuncCall->args, [0, 1, 2])) {
return null;
}

Expand All @@ -182,8 +184,8 @@ private function refactorSubstrCompare(BinaryOp $binaryOp): FuncCall | BooleanNo
$thirdArgValue = $thirdArg->value;

if (
!$this->isUnaryMinusStrlenFuncCallArgValue($thirdArgValue, $needle) &&
!$this->isHardCodedLNumberAndString($thirdArgValue, $needle)
! $this->isUnaryMinusStrlenFuncCallArgValue($thirdArgValue, $needle) &&
! $this->isHardCodedLNumberAndString($thirdArgValue, $needle)
) {
return null;
}
Expand All @@ -195,25 +197,25 @@ private function refactorSubstrCompare(BinaryOp $binaryOp): FuncCall | BooleanNo

private function isUnaryMinusStrlenFuncCallArgValue(Expr $substrOffset, Expr $needle): bool
{
if (!$substrOffset instanceof UnaryMinus) {
if (! $substrOffset instanceof UnaryMinus) {
return false;
}

if (!$substrOffset->expr instanceof FuncCall) {
if (! $substrOffset->expr instanceof FuncCall) {
return false;
}

$funcCall = $substrOffset->expr;

if (!$this->nodeNameResolver->isName($funcCall, 'strlen')) {
if (! $this->nodeNameResolver->isName($funcCall, 'strlen')) {
return false;
}

if (!isset($funcCall->args[0])) {
if (! isset($funcCall->args[0])) {
return false;
}

if (!$funcCall->args[0] instanceof Arg) {
if (! $funcCall->args[0] instanceof Arg) {
return false;
}

Expand All @@ -222,17 +224,17 @@ private function isUnaryMinusStrlenFuncCallArgValue(Expr $substrOffset, Expr $ne

private function isHardCodedLNumberAndString(Expr $substrOffset, Expr $needle): bool
{
if (!$substrOffset instanceof UnaryMinus) {
if (! $substrOffset instanceof UnaryMinus) {
return false;
}

if (!$substrOffset->expr instanceof LNumber) {
if (! $substrOffset->expr instanceof LNumber) {
return false;
}

$lNumber = $substrOffset->expr;

if (!$needle instanceof String_) {
if (! $needle instanceof String_) {
return false;
}

Expand All @@ -243,7 +245,7 @@ private function buildReturnNode(Expr $haystack, Expr $needle, bool $isPositive)
{
$funcCall = $this->nodeFactory->createFuncCall('str_ends_with', [$haystack, $needle]);

if (!$isPositive) {
if (! $isPositive) {
return new BooleanNot($funcCall);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Spatie\Enum\Enum;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -23,6 +24,9 @@
*/
final class SpatieEnumMethodCallToEnumConstRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var class-string<Enum>
*/
private const SPATIE_FQN = 'Spatie\Enum\Enum';

/**
Expand Down Expand Up @@ -133,11 +137,19 @@ private function refactorMethodCall(MethodCall $methodCall, string $methodName):
return null;
}

if ($methodName === 'getName' || $methodName === 'label') {
if ($methodName === 'getName') {
return $this->refactorGetterToMethodCall($methodCall, 'name');
}

if ($methodName === 'label') {
return $this->refactorGetterToMethodCall($methodCall, 'name');
}

if ($methodName === 'getValue' || $methodName === 'value') {
if ($methodName === 'getValue') {
return $this->refactorGetterToMethodCall($methodCall, 'value');
}

if ($methodName === 'value') {
return $this->refactorGetterToMethodCall($methodCall, 'value');
}

Expand Down

This file was deleted.

0 comments on commit eadd540

Please sign in to comment.