use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.
the class BadExceptionsProcessingInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpTry(@NotNull Try tryStatement) {
final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(tryStatement);
final int expressionsCount = body == null ? 0 : ExpressionSemanticUtil.countExpressionsInGroup(body);
if (expressionsCount > 3) {
holder.registerProblem(tryStatement.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%c%", String.valueOf(expressionsCount))));
}
}
@Override
public void visitPhpCatch(@NotNull Catch catchStatement) {
final Variable variable = catchStatement.getException();
final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(catchStatement);
if (variable != null && body != null) {
final String variableName = variable.getName();
if (!StringUtils.isEmpty(variableName)) {
/* incomplete catch statement */
boolean isVariableUsed = false;
for (final Variable usedVariable : PsiTreeUtil.findChildrenOfType(body, Variable.class)) {
if (usedVariable.getName().equals(variableName)) {
isVariableUsed = true;
break;
}
}
if (!isVariableUsed) {
if (ExpressionSemanticUtil.countExpressionsInGroup(body) == 0) {
holder.registerProblem(variable, MessagesPresentationUtil.prefixWithEa(messageFailSilently));
} else {
holder.registerProblem(variable, MessagesPresentationUtil.prefixWithEa(messageChainedException));
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.
the class UnnecessaryElseFixer method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiElement expression = null == element ? null : element.getParent();
final PsiElement newline = PhpPsiElementFactory.createFromText(project, PsiWhiteSpace.class, "\n");
if (null == expression || null == newline || project.isDisposed()) {
return;
}
if (expression instanceof Else) {
final Else elseStatement = (Else) expression;
final If parentIfExpression = (If) expression.getParent();
final PsiElement parentIfHolder = parentIfExpression.getParent();
if (elseStatement.getFirstPsiChild() instanceof If) {
/* handle 'else if' */
final If nestedIfCopy = (If) elseStatement.getFirstPsiChild().copy();
elseStatement.delete();
parentIfHolder.addAfter(nestedIfCopy, parentIfExpression);
parentIfHolder.addAfter(newline, parentIfExpression);
return;
}
if (elseStatement.getFirstPsiChild() instanceof GroupStatement) {
/* handle 'else {} ' */
final GroupStatement elseBodyCopy = (GroupStatement) elseStatement.getFirstPsiChild().copy();
elseStatement.delete();
final PsiElement startBracket = elseBodyCopy.getFirstChild();
final PsiElement endBracket = elseBodyCopy.getLastChild();
PsiElement last = endBracket.getPrevSibling();
if (last instanceof PsiWhiteSpace) {
last = last.getPrevSibling();
}
while (null != last) {
if (last != endBracket && last != startBracket) {
parentIfHolder.addAfter(last, parentIfExpression);
}
last = last.getPrevSibling();
}
/* trailing spacing should not be influence by else body */
final PsiElement trailingSpaceCandidate = parentIfExpression.getNextSibling();
if (trailingSpaceCandidate instanceof PsiWhiteSpace) {
trailingSpaceCandidate.replace(newline);
} else {
parentIfHolder.addAfter(newline, parentIfExpression);
}
return;
}
}
if (expression instanceof ElseIf) {
/* handle 'elseif': messed up to not loose cursor after fixing... */
final ElseIf elseIfStatement = (ElseIf) expression;
final If parentIfExpression = (If) expression.getParent();
/* back up original if */
final If newIf = PhpPsiElementFactory.createPhpPsiFromText(project, If.class, "if (true) {\n}");
newIf.getCondition().replace(parentIfExpression.getCondition());
ExpressionSemanticUtil.getGroupStatement(newIf).replace(ExpressionSemanticUtil.getGroupStatement(parentIfExpression));
/* drop the elseif, backup resulted construct */
parentIfExpression.getCondition().replace(elseIfStatement.getCondition().copy());
ExpressionSemanticUtil.getGroupStatement(parentIfExpression).replace(ExpressionSemanticUtil.getGroupStatement(elseIfStatement).copy());
elseIfStatement.delete();
final PsiElement followUpIf = parentIfExpression.copy();
/* insert following up if, which was backed up */
final PsiElement parentIfHolder = parentIfExpression.getParent();
parentIfHolder.addAfter(followUpIf, parentIfExpression);
parentIfHolder.addAfter(newline, parentIfExpression);
/* actualize if-statement with clean one */
parentIfHolder.addBefore(newIf, parentIfExpression);
parentIfExpression.delete();
}
}
use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.
the class EncryptionInitializationVectorRandomnessInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final String functionName = reference.getName();
/* variable functions are not supported, as we are checking 2 different extensions functions */
if (functionName != null && (functionName.equals("openssl_encrypt") || functionName.equals("mcrypt_encrypt"))) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length != 5 || arguments[4] == null || arguments[4].getText().isEmpty()) {
return;
}
/* discover and inspect possible values */
final Set<PsiElement> values = PossibleValuesDiscoveryUtil.discover(arguments[4]);
if (!values.isEmpty()) {
/* check all possible values */
final List<String> reporting = new ArrayList<>();
for (final PsiElement source : values) {
if (OpenapiTypesUtil.isFunctionReference(source)) {
final String sourceName = ((FunctionReference) source).getName();
if (sourceName != null && secureFunctions.contains(sourceName)) {
continue;
}
}
reporting.add(source.getText());
}
if (!reporting.isEmpty() && !this.isAggregatedGeneration(values)) {
/* sort reporting list to produce testable results */
Collections.sort(reporting);
final String ivFunction = functionName.startsWith("openssl_") ? "openssl_random_pseudo_bytes" : "mcrypt_create_iv";
holder.registerProblem(arguments[4], MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, ivFunction, String.join(", ", reporting))), ProblemHighlightType.GENERIC_ERROR);
}
reporting.clear();
}
values.clear();
}
}
private boolean isAggregatedGeneration(@NotNull Set<PsiElement> candidates) {
if (candidates.size() == 1) {
final PsiElement candidate = candidates.iterator().next();
if (candidate instanceof FunctionReference) {
final PsiElement resolved = OpenapiResolveUtil.resolveReference((PsiReference) candidate);
if (resolved instanceof Function) {
final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(resolved);
if (body != null) {
return PsiTreeUtil.findChildrenOfType(body, FunctionReference.class).stream().anyMatch(c -> secureFunctions.contains(c.getName()));
}
}
}
}
return false;
}
};
}
use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.
the class ExpressionSemanticUtilTest method testGetLastStatement.
public void testGetLastStatement() {
Project project = myFixture.getProject();
GroupStatement statement;
String blockWithCommentsOnly = "{ /** dock-block*/ /* comment*/ /** doc-block*/ }";
statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWithCommentsOnly);
assertNotNull(statement);
assertNull(ExpressionSemanticUtil.getLastStatement(statement));
String blockWith2Statements = "{ /** dock-block*/ echo 1; return 2; /** doc-block*/ }";
statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWith2Statements);
assertNotNull(statement);
PsiElement lastStatement = ExpressionSemanticUtil.getLastStatement(statement);
assertNotNull(lastStatement);
assertInstanceOf(lastStatement, PhpReturn.class);
}
use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.
the class ExpressionSemanticUtilTest method testCountExpressionsInGroup.
public void testCountExpressionsInGroup() {
Project project = myFixture.getProject();
GroupStatement statement;
String blockWithCommentsOnly = "{ /** dock-block*/ /* comment*/ /** doc-block*/ }";
statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWithCommentsOnly);
assertNotNull(statement);
assertEquals(0, ExpressionSemanticUtil.countExpressionsInGroup(statement));
String blockWith2Statements = "{ /** dock-block*/ echo 1; return 2; /** doc-block*/ }";
statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWith2Statements);
assertNotNull(statement);
assertEquals(2, ExpressionSemanticUtil.countExpressionsInGroup(statement));
}
Aggregations