Skip to content

Commit

Permalink
Global: code-coverage;
Browse files Browse the repository at this point in the history
  • Loading branch information
rentalhost committed Jun 22, 2017
1 parent a97fe0b commit 03fb4df
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

@yield ('will be ignored')

@if ($expr)<fold text=' {...} '>
@yield ('will be ignored (code-coverage)')
</fold>@endif

@if ($expr)<fold text=' {...} '>
If folding.
</fold>@endif
Expand Down
3 changes: 3 additions & 0 deletions resources-tests/laravelInsight/fluent/FluentUtil.samples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$reference;
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class Fluent

class ChildFluent extends Fluent
{
public $publicIsOkay;
}

// Acceptable: Fluent is intantiated via ChildFluent.
$fluentInstantiatedIndirectly = new ChildFluent();
$fluentInstantiatedIndirectly-><weak_warning descr="Property was not annotated as @property $shouldAccept">shouldAccept</weak_warning>;
$fluentInstantiatedIndirectly->publicIsOkay;

// Acceptable: static property.
ChildFluent::<weak_warning descr="Property was not annotated as @property $staticShouldBeAccepted">$staticShouldBeAccepted</weak_warning>;
Expand All @@ -47,4 +49,7 @@ class ChildFluent extends Fluent
$fluentInstantiatedViaFacadeL54 = new \Facades\Illuminate\Support\Fluent();
$fluentInstantiatedViaFacade->shouldNotAccept;
\Facades\Illuminate\Support\Fluent::$shouldNotAccept;
// Code-coverage:
$fluentInstantiatedIndirectly->{'fieldNameIsEmpty'};
}
2 changes: 2 additions & 0 deletions resources-tests/utils/PhpClassUtil.resolve.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ public function getSelf(): ?self
$indirectReference->getSelf()->$chainedReference;

(new UnresolvedReference)->$unresolvedReference;

/* no type */->$ccNotTypedElement;
1 change: 1 addition & 0 deletions resources-tests/utils/PsiElementUtil.warpingLiterals.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ public function declaredOnDocComment()
const SHOULD_STOP_HERE = NOT_HERE;
const NOT_HERE = false;
$stopOnFirstConstantReference = SHOULD_STOP_HERE;
$stopOnFirstConstantReferenceIndirect = $stopOnFirstConstantReference;
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ public void visitPhpFieldReference(@NotNull final FieldReference fieldReference)
if (FluentUtil.isUsingIndirectly(fieldReference)) {
final List<PhpClass> fieldClasses = PhpClassUtil.resolve(fieldReference);
final PhpClass fieldClass = fieldClasses.get(0);
final ASTNode fieldNameNode = fieldReference.getNameNode();
final String fieldName = fieldReference.getName();

if (fieldNameNode == null) {
assert fieldName != null;

if (fieldName.isEmpty()) {
return;
}

final String fieldName = fieldNameNode.getText();
final Field fieldDeclaration = PhpClassUtil.findPropertyDeclaration(fieldClass, fieldName);

if ((fieldDeclaration != null) &&
Expand All @@ -55,7 +56,9 @@ public void visitPhpFieldReference(@NotNull final FieldReference fieldReference)
}

if (PhpDocCommentUtil.findPropertyRecursively(fieldClass, fieldName) == null) {
problemsHolder.registerProblem((PsiElement) fieldNameNode,
assert fieldReference.getNameNode() != null;

problemsHolder.registerProblem((PsiElement) fieldReference.getNameNode(),
String.format(messagePropertyUndefined, fieldName),
ProblemHighlightType.WEAK_WARNING,
new PropertyQuickFix(fieldClass, fieldName, "mixed"));
Expand Down
15 changes: 7 additions & 8 deletions src/utils/PhpClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpClassMember;
import com.jetbrains.php.lang.psi.elements.PhpExpression;
import com.jetbrains.php.lang.psi.elements.PhpReference;
import com.jetbrains.php.lang.psi.elements.PhpTypedElement;
import com.jetbrains.php.lang.psi.elements.PhpUse;
Expand Down Expand Up @@ -210,22 +211,20 @@ public static Method findMethodDeclaration(

@NotNull
public static List<PhpClass> resolve(@NotNull final PsiElement element) {
final List<PhpClass> classes = new ArrayList<>();
PsiElement elementProcessed = PsiElementUtil.resolve(element);
PsiElement elementProcessed = PsiElementUtil.resolve(element);

if (elementProcessed instanceof PhpClassMember) {
elementProcessed = ((PhpClassMember) elementProcessed).getContainingClass();
}
else if (elementProcessed instanceof MemberReference) {
elementProcessed = ((MemberReference) elementProcessed).getClassReference();
final PhpExpression elementClassReference = ((MemberReference) elementProcessed).getClassReference();
assert elementClassReference != null;

if (elementProcessed == null) {
return classes;
}

elementProcessed = PsiElementUtil.resolve(elementProcessed, MemberReference.class::isInstance);
elementProcessed = PsiElementUtil.resolve(elementClassReference, MemberReference.class::isInstance);
}

final List<PhpClass> classes = new ArrayList<>();

if (!(elementProcessed instanceof PhpTypedElement)) {
return classes;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/idea/laravelInsight/fluent/FluentUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.rentalhost.idea.laravelInsight.fluent;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.junit.Assert;

import net.rentalhost.suite.FixtureSuite;

public class FluentUtilTest extends FixtureSuite {
public void testIsUsingDirectly() {
Assert.assertFalse(FluentUtil.isUsingDirectly(null));

final PsiFile fileSample = getResourceFile("laravelInsight/fluent/FluentUtil.samples.php");
final PsiElement emptyReference = getElementByName(fileSample, "reference");

Assert.assertFalse(FluentUtil.isUsingDirectly(emptyReference));
}

public void testIsUsingIndirectly() {
Assert.assertFalse(FluentUtil.isUsingIndirectly(null));

final PsiFile fileSample = getResourceFile("laravelInsight/fluent/FluentUtil.samples.php");
final PsiElement emptyReference = getElementByName(fileSample, "reference");

Assert.assertFalse(FluentUtil.isUsingIndirectly(emptyReference));
}
}
4 changes: 4 additions & 0 deletions tests/idea/utils/PhpClassUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,9 @@ public void testResolve() {
final PsiElement unresolvedReference = getElementByName(fileSample, "unresolvedReference").getParent();

Assert.assertEquals(0, PhpClassUtil.resolve(unresolvedReference).size());

final PsiElement ccNotTypedElement = getElementByName(fileSample, "ccNotTypedElement").getParent();

Assert.assertEquals(0, PhpClassUtil.resolve(ccNotTypedElement).size());
}
}
6 changes: 6 additions & 0 deletions tests/idea/utils/PsiElementUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public void testResolve() {
Assert.assertTrue(stopOnFirstConstantReference instanceof ConstantReference);
Assert.assertEquals("$stopOnFirstConstantReference = SHOULD_STOP_HERE", stopOnFirstConstantReference.getParent().getText());

final PsiElement stopOnFirstConstantReferenceIndirect =
PsiElementUtil.resolve(getStringLiteral(fileSample, "stopOnFirstConstantReferenceIndirect"), ConstantReference.class::isInstance);

Assert.assertTrue(stopOnFirstConstantReferenceIndirect instanceof ConstantReference);
Assert.assertEquals("$stopOnFirstConstantReference = SHOULD_STOP_HERE", stopOnFirstConstantReferenceIndirect.getParent().getText());

// Make sure that resolves to a @property will not broke.
final AssignmentExpression ccInstancePropertyRef = (AssignmentExpression) getElementByName(fileSample, "ccInstancePropertyRef").getParent();
final PsiElement ccInstanceProperty = PsiElementUtil.resolve(valueOf((PhpExpression) ccInstancePropertyRef.getValue()));
Expand Down

0 comments on commit 03fb4df

Please sign in to comment.