org.sonar.php.checks.ArgumentWithDefaultValueNotLastCheck Maven / Gradle / Ivy
/*
* SonarQube PHP Plugin
* Copyright (C) 2010-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.php.checks;
import java.util.ArrayList;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.php.utils.collections.ListUtils;
import org.sonar.plugins.php.api.tree.SeparatedList;
import org.sonar.plugins.php.api.tree.declaration.ParameterListTree;
import org.sonar.plugins.php.api.tree.declaration.ParameterTree;
import org.sonar.plugins.php.api.visitors.PHPVisitorCheck;
@Rule(key = ArgumentWithDefaultValueNotLastCheck.KEY)
public class ArgumentWithDefaultValueNotLastCheck extends PHPVisitorCheck {
public static final String KEY = "S1788";
private static final String MESSAGE = "Move arguments %s after arguments without default value";
@Override
public void visitParameterList(ParameterListTree parameterList) {
List parametersToMove = getParametersToMove(parameterList);
if (!parametersToMove.isEmpty() && !isVariableLengthParameterList(parameterList)) {
context().newIssue(this, parameterList, String.format(MESSAGE, getNameListString(parametersToMove)));
}
super.visitParameterList(parameterList);
}
private static boolean isVariableLengthParameterList(ParameterListTree parameterList) {
SeparatedList parameters = parameterList.parameters();
if (!parameters.isEmpty()) {
ParameterTree lastParameter = parameters.get(parameters.size() - 1);
return lastParameter.ellipsisToken() != null;
}
return false;
}
/**
* Return list of parameter nodes that are not declared at the end.
*
* Example: $p2 will be returned.
* function f($p1, $p2 = 1, $p3, $p4 = 4) {...}
*/
private static List getParametersToMove(ParameterListTree parameterList) {
List parametersToMove = new ArrayList<>();
boolean metParamWithoutDefault = false;
for (ParameterTree param : ListUtils.reverse(parameterList.parameters())) {
boolean hasDefault = param.initValue() != null;
if (!hasDefault && !metParamWithoutDefault) {
metParamWithoutDefault = true;
} else if (hasDefault && metParamWithoutDefault) {
parametersToMove.add(param);
}
}
return ListUtils.reverse(parametersToMove);
}
private static String getNameListString(List params) {
List parameterNames = new ArrayList<>();
for (ParameterTree parameter : params) {
parameterNames.add("\"" + parameter.variableIdentifier().variableExpression().text() + "\"");
}
return String.join(", ", parameterNames);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy