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,16 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector\Fixture;

const ENVIRONMENT = 'local';

final class SkipConstantExpression
{
public static bool $isEnv = (ENVIRONMENT !== 'local' && ENVIRONMENT !== 'dev' && ENVIRONMENT !== 'stage');

public const IS_ENV = (ENVIRONMENT !== 'local' && ENVIRONMENT !== 'dev' && ENVIRONMENT !== 'stage');

public function demo(bool $value = (ENVIRONMENT !== 'local' && ENVIRONMENT !== 'dev' && ENVIRONMENT !== 'stage')): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\Fixture;

const ENVIRONMENT = 'local';

final class SkipConstantExpression
{
public static bool $isEnv = (ENVIRONMENT === 'local' || ENVIRONMENT === 'dev' || ENVIRONMENT === 'stage');

public const IS_ENV = (ENVIRONMENT === 'local' || ENVIRONMENT === 'dev' || ENVIRONMENT === 'stage');

public function demo(bool $value = (ENVIRONMENT === 'local' || ENVIRONMENT === 'dev' || ENVIRONMENT === 'stage')): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Name;
use Rector\CodeQuality\NodeFactory\InArrayFromRepeatedCompareFactory;
use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -68,6 +69,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?BooleanNot
{
// in_array() func call is not allowed in a constant expression
if ($this->isInConstantExpression($node)) {
return null;
}

if (! $this->isNotEqualOrNotIdentical($node->right)) {
return null;
}
Expand Down Expand Up @@ -96,6 +102,19 @@ public function refactor(Node $node): ?BooleanNot
return new BooleanNot($inArrayFuncCall);
}

private function isInConstantExpression(BooleanAnd $booleanAnd): bool
{
if ($booleanAnd->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) {
return true;
}

if ($booleanAnd->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) {
return true;
}

return $booleanAnd->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true;
}

private function isNotEqualOrNotIdentical(Expr $expr): bool
{
if ($expr instanceof NotIdentical) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Name;
use Rector\CodeQuality\NodeFactory\InArrayFromRepeatedCompareFactory;
use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -67,6 +68,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?FuncCall
{
// in_array() func call is not allowed in a constant expression
if ($this->isInConstantExpression($node)) {
return null;
}

if (! $this->isEqualOrIdentical($node->right)) {
return null;
}
Expand Down Expand Up @@ -102,6 +108,19 @@ public function refactor(Node $node): ?FuncCall
return new FuncCall(new Name('in_array'), $args);
}

private function isInConstantExpression(BooleanOr $booleanOr): bool
{
if ($booleanOr->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) {
return true;
}

if ($booleanOr->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) {
return true;
}

return $booleanOr->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true;
}

private function isEqualOrIdentical(Expr $expr): bool
{
if ($expr instanceof Identical) {
Expand Down
Loading