Skip to content

Commit

Permalink
PhpExpressionUtil: now from() consider true, false and null as …
Browse files Browse the repository at this point in the history
…resolvable elements;
  • Loading branch information
rentalhost committed Jun 1, 2017
1 parent e65204b commit 325d71f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions resources-tests/api/PhpExpressionUtil.warpingLiterals.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class ResolvingInsideClass
const INDIRECT_LITERAL = self::DIRECT_LITERAL;
const WARPING_LITERAL = self::INDIRECT_LITERAL;

const RESOLVING_PRIMARY_CONTANTS = TRUE;
protected $resolvingFromProperty = self::RESOLVING_PRIMARY_CONTANTS;

protected $resolvingDirectlyFromProperty = null;

public function __construct()
{
$classIndirectLiteral = self::INDIRECT_LITERAL;
Expand Down
28 changes: 26 additions & 2 deletions src/api/PhpExpressionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl;

import java.util.ArrayList;
import java.util.Collection;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public enum PhpExpressionUtil {
;

private static final Collection<String> primaryConstants = new ArrayList<>();

static {
primaryConstants.add("null");
primaryConstants.add("true");
primaryConstants.add("false");
}

@Nullable
static PhpExpression recursionResolver(final RecursionResolver.Resolver<PhpReference, PhpExpression> resolver) {
final PsiElement elementResolved = resolver.getObject().resolve();
Expand All @@ -31,13 +42,26 @@ else if (elementResolved instanceof Constant) {
return (PhpExpression) referencedValue;
}

if (isPrimaryConstant(referencedValue)) {
return (PhpExpression) referencedValue;
}

return resolver.resolve((PhpReference) referencedValue);
}

private static boolean isPrimaryConstant(final PsiElement element) {
return primaryConstants.contains(element.getText().toLowerCase());
}

@Nullable
public static PhpExpression from(@NotNull final PhpExpression elementInitial) {
return RecursionResolver.resolve(elementInitial, resolver -> {
final Object element = resolver.getObject();
final PhpExpression element = (PhpExpression) resolver.getObject();

if ((element instanceof ConstantReference) &&
isPrimaryConstant(element)) {
return element;
}

if (element instanceof PhpReference) {
return RecursionResolver.resolve(element, PhpExpressionUtil::recursionResolver);
Expand All @@ -54,7 +78,7 @@ public static PhpExpression from(@NotNull final PhpExpression elementInitial) {
return (PhpExpression) resolver.resolve(elementValue);
}

return (PhpExpression) element;
return element;
});
}
}
14 changes: 11 additions & 3 deletions tests/idea/api/PhpExpressionUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.psi.elements.AssignmentExpression;
import com.jetbrains.php.lang.psi.elements.PhpExpression;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.elements.*;
import org.junit.Assert;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -48,6 +46,16 @@ public void testFrom() {
Assert.assertEquals("indirectClassValue", valueOf(classIndirectLiteral).getContents());
Assert.assertEquals("indirectClassValue", valueOf(classWarpingLiteral).getContents());

final ConstantReference classResolvingFromProperty =
(ConstantReference) PhpExpressionUtil.from(valueOf((PhpExpression) ((Field) getElementByName(fileSample, "resolvingFromProperty")).getDefaultValue()));

Assert.assertEquals("TRUE", valueOf(classResolvingFromProperty).getText());

final ConstantReference classResolvingDirectlyFromProperty =
(ConstantReference) PhpExpressionUtil.from(valueOf((PhpExpression) ((Field) getElementByName(fileSample, "resolvingDirectlyFromProperty")).getDefaultValue()));

Assert.assertEquals("null", valueOf(classResolvingDirectlyFromProperty).getText());

// Avoiding complex loopings.
final PhpExpression shouldAvoidCyclicLoopingsWithConstants = (PhpExpression) getElementByName(fileSample, "shouldAvoidCyclicLoopingsWithConstants");
final PhpExpression shouldAvoidCyclicLoopingsWithVariablesA = (PhpExpression) getElementByName(fileSample, "shouldAvoidCyclicLoopingsWithVariablesA");
Expand Down

0 comments on commit 325d71f

Please sign in to comment.