Skip to content
Open
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
33 changes: 19 additions & 14 deletions php-checks/src/main/java/org/sonar/php/checks/DeadStoreCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,29 @@ private void verifyBlock(CfgBlock block, LiveVariables blockLiveVariables, Set<S
for (Tree element : ListUtils.reverse(block.elements())) {
Map<Symbol, VariableUsage> usagesInElement = blockLiveVariables.getVariableUsages(element);
for (Map.Entry<Symbol, VariableUsage> symbolWithUsage : usagesInElement.entrySet()) {
Symbol symbol = symbolWithUsage.getKey();
if (outOfScope(readSymbols, symbol)) {
// These cases are verified by other checks
continue;
}
VariableUsage usage = symbolWithUsage.getValue();
if (usage.isWrite() && !usage.isRead()) {
if (!willBeRead.contains(symbol) && !shouldSkip(element, symbol)) {
context().newIssue(this, element, String.format(MESSAGE_TEMPLATE, symbol.name()));
}
willBeRead.remove(symbol);
} else if (usage.isRead()) {
willBeRead.add(symbol);
}
processSymbolUsage(symbolWithUsage, readSymbols, willBeRead, element);
}
}
}

private void processSymbolUsage(Map.Entry<Symbol, VariableUsage> symbolWithUsage, Set<Symbol> readSymbols,
Set<Symbol> willBeRead, Tree element) {
Symbol symbol = symbolWithUsage.getKey();
if (outOfScope(readSymbols, symbol)) {
// These cases are verified by other checks
return;
}
VariableUsage usage = symbolWithUsage.getValue();
if (usage.isWrite() && !usage.isRead()) {
if (!willBeRead.contains(symbol) && !shouldSkip(element, symbol)) {
context().newIssue(this, element, String.format(MESSAGE_TEMPLATE, symbol.name()));
}
willBeRead.remove(symbol);
} else if (usage.isRead()) {
willBeRead.add(symbol);
}
}

private static boolean outOfScope(Set<Symbol> readSymbols, Symbol symbol) {
return !readSymbols.contains(symbol) || symbol.is(Symbol.Kind.PARAMETER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
public class NoAssertionInTestCheck extends PhpUnitCheck {
private static final String MESSAGE = "Add at least one assertion to this test case.";

private static final Pattern ASSERTION_METHODS_PATTERN = Pattern.compile("(assert|verify|fail|pass|should|will|check|expect|validate|.*test).*");
private static final Pattern ASSERTION_METHODS_PATTERN = Pattern.compile("(assert|verify|fail|pass|should|will|check|expect|validate).*|.*test.*");
private static final List<String> TEST_CONTROL_FUNCTIONS = Arrays.asList(
"addtoassertioncount",
"marktestskipped",
Expand Down Expand Up @@ -127,8 +127,7 @@ private boolean isLocalMethodWithAssertion(FunctionCallTree tree) {
return false;
}

if (!assertionInMethod.containsKey(methodDeclaration)) {
assertionInMethod.put(methodDeclaration, false);
if (assertionInMethod.putIfAbsent(methodDeclaration, false) == null) {
AssertionsFindVisitor v = new AssertionsFindVisitor(symbolTable);
methodDeclaration.accept(v);
assertionInMethod.put(methodDeclaration, v.hasFoundAssertion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public abstract class PhpUnitCheck extends PHPVisitorCheck {
private boolean isPhpUnitTestMethod = false;

private static Map<String, Assertion> assertions() {
return Stream.of(
arrayContainsAndEqualityAssertions(),
fileAndDirectoryAssertions(),
typeStateAndIdentityAssertions(),
stringXmlJsonAndConstraintAssertions())
.flatMap(s -> s)
.collect(Collectors.toMap(e -> e.name().toLowerCase(Locale.ENGLISH), e -> e));
}

private static Stream<Assertion> arrayContainsAndEqualityAssertions() {
return Stream.of(
new Assertion("assertArrayHasKey"),
new Assertion("assertArraySubset"),
Expand Down Expand Up @@ -77,7 +87,11 @@ private static Map<String, Assertion> assertions() {
new Assertion("assertLessThan", true),
new Assertion("assertAttributeLessThan", true),
new Assertion("assertLessThanOrEqual", true),
new Assertion("assertAttributeLessThanOrEqual"),
new Assertion("assertAttributeLessThanOrEqual"));
}

private static Stream<Assertion> fileAndDirectoryAssertions() {
return Stream.of(
new Assertion("assertFileEquals", true),
new Assertion("assertFileEqualsCanonicalizing", true),
new Assertion("assertFileEqualsIgnoringCase", true),
Expand Down Expand Up @@ -105,7 +119,11 @@ private static Map<String, Assertion> assertions() {
new Assertion("assertFileIsReadable"),
new Assertion("assertFileNotIsReadable"),
new Assertion("assertFileIsWritable"),
new Assertion("assertFileNotIsWritable"),
new Assertion("assertFileNotIsWritable"));
}

private static Stream<Assertion> typeStateAndIdentityAssertions() {
return Stream.of(
new Assertion("assertTrue"),
new Assertion("assertNotTrue"),
new Assertion("assertFalse"),
Expand Down Expand Up @@ -158,7 +176,11 @@ private static Map<String, Assertion> assertions() {
new Assertion("assertRegExp"),
new Assertion("assertNotRegExp"),
new Assertion("assertSameSize", true),
new Assertion("assertNotSameSize", true),
new Assertion("assertNotSameSize", true));
}

private static Stream<Assertion> stringXmlJsonAndConstraintAssertions() {
return Stream.of(
new Assertion("assertStringMatchesFormat"),
new Assertion("assertStringNotMatchesFormat"),
new Assertion("assertStringMatchesFormatFile"),
Expand Down Expand Up @@ -227,7 +249,7 @@ private static Map<String, Assertion> assertions() {
new Assertion("matches"),
new Assertion("stringStartsWith"),
new Assertion("stringContains"),
new Assertion("stringEndsWith")).collect(Collectors.toMap(e -> e.name().toLowerCase(Locale.ENGLISH), e -> e));
new Assertion("stringEndsWith"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected PhpUnitReportImporter(AnalysisWarningsWrapper analysisWarningsWrapper)
@Override
public final void execute(SensorContext context) {
super.execute(context);
if (getReportFiles(context).isEmpty()) {
if (getReportFiles(context).isEmpty() && logger().isInfoEnabled()) {
logger().info("No {} reports provided (see '{}' property)", reportName(), reportPathKey());
}
}
Expand Down