Skip to content
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
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

$attributed = new #[Tries(5)] class(4)
{
public function __construct(
private readonly int $attempts,
) {}

public function attempts(): int
{
return $this->attempts;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ private function matchComparedAndDesiredValues(BooleanAnd $booleanAnd): ?array
private function isStrictComparison(BooleanAnd $booleanAnd): bool
{
$notIdenticals = $this->betterNodeFinder->findInstanceOf($booleanAnd, NotIdentical::class);
$notEquals = $this->betterNodeFinder->findInstanceOf($booleanAnd, NotEqual::class);

if ($notIdenticals !== []) {
// mix not identical and not equals, keep as is
// @see https://3v4l.org/2SoHZ
return $notEquals === [];
if ($notIdenticals === []) {
return false;
}

return false;
$notEquals = $this->betterNodeFinder->findInstanceOf($booleanAnd, NotEqual::class);

// mix not identical and not equals, keep as is
// @see https://3v4l.org/2SoHZ
return $notEquals === [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public function refactor(Node $node): ?FuncCall
}

$identicals = $this->betterNodeFinder->findInstanceOf($node, Identical::class);
$equals = $this->betterNodeFinder->findInstanceOf($node, Equal::class);

if ($identicals !== []) {
$equals = $this->betterNodeFinder->findInstanceOf($node, Equal::class);
if ($equals !== []) {
// mix identical and equals, keep as is
// @see https://3v4l.org/24cFl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
return null;
}

$exceptionArgumentPosition = $this->resolveExceptionArgumentPosition($new->class);
if ($exceptionArgumentPosition === null) {
if ($new->isFirstClassCallable()) {
return null;
}

if ($new->isFirstClassCallable()) {
$exceptionArgumentPosition = $this->resolveExceptionArgumentPosition($new->class);
if ($exceptionArgumentPosition === null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ private function isAllowedVariable(Variable $variable): bool

private function isAllowedExpr(Expr $expr, Scope $scope): bool
{
if (! $expr instanceof Variable && ! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) {
return false;
}

if (! $scope->getType($expr)->isArray()->yes()) {
return false;
}
Expand All @@ -114,10 +118,6 @@ private function isAllowedExpr(Expr $expr, Scope $scope): bool
return $this->isAllowedVariable($expr);
}

if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) {
return false;
}

if (! $expr->name instanceof Identifier) {
return false;
}
Expand Down
13 changes: 9 additions & 4 deletions rules/CodeQuality/Rector/FuncCall/InlineIsAInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,25 @@ public function refactor(Node $node): ?Node
}

$args = $node->getArgs();
$firstArgValue = $args[0]->value;

$secondArgValue = $args[1]->value;
if (! $secondArgValue instanceof Variable && ! $secondArgValue instanceof ClassConstFetch) {
return null;
}

$firstArgValue = $args[0]->value;
if (! $this->isFirstObjectType($firstArgValue)) {
return null;
}

/**
* instanceof with Variable is ok, while on FuncCal with instanceof cause fatal error, see https://3v4l.org/IHb30
*/
if ($args[1]->value instanceof Variable) {
return new Instanceof_($firstArgValue, $args[1]->value);
if ($secondArgValue instanceof Variable) {
return new Instanceof_($firstArgValue, $secondArgValue);
}

$className = $this->resolveClassName($args[1]->value);
$className = $this->resolveClassName($secondArgValue);
if ($className === null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public function refactor(Node $node): ?Node
continue;
}

// possibly by docblock, dynamic array access
if ($issetExpr->var instanceof ArrayDimFetch) {
continue;
}

// has property PHP 7.4 type?
if ($this->shouldSkipForPropertyTypeDeclaration($issetExpr)) {
continue;
Expand All @@ -126,11 +131,6 @@ public function refactor(Node $node): ?Node
continue;
}

// possibly by docblock
if ($issetExpr->var instanceof ArrayDimFetch) {
continue;
}

if (! $classReflection->hasInstanceProperty($propertyFetchName) || $classReflection->isBuiltin()) {
$newNodes[] = $this->replaceToPropertyExistsWithNullCheck(
$issetExpr->var,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public function refactor(Node $node): ?Node
return null;
}

$emptyExprType = $this->getType($negagedExpr->expr);
if (! $emptyExprType->isArray()->yes()) {
$dimFetchVar = $node->if->var;
if (! $this->nodeComparator->areNodesEqual($negagedExpr->expr, $dimFetchVar)) {
return null;
}

$dimFetchVar = $node->if->var;
if (! $this->nodeComparator->areNodesEqual($negagedExpr->expr, $dimFetchVar)) {
$emptyExprType = $this->getType($negagedExpr->expr);
if (! $emptyExprType->isArray()->yes()) {
return null;
}

Expand Down
5 changes: 5 additions & 0 deletions rules/Php83/Rector/Class_/ReadOnlyAnonymousClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function refactor(Node $node): ?Node
return null;
}

// attribute between "new" and "class" cannot be format-preserving printed with an added readonly modifier
if ($node->attrGroups !== []) {
return null;
}

return $this->readonlyClassManipulator->process($node);
}

Expand Down
Loading