org.sonar.plugins.php.PHPChecks Maven / Gradle / Ivy
/*
* SonarQube PHP Plugin
* Copyright (C) 2010-2021 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.plugins.php;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.batch.rule.Checks;
import org.sonar.api.rule.RuleKey;
import org.sonar.plugins.php.api.visitors.PHPCheck;
import org.sonar.plugins.php.api.visitors.PHPCustomRuleRepository;
/**
* Wrapper around Checks Object to ease the manipulation of the different PHP rule repositories.
*/
public class PHPChecks {
private final CheckFactory checkFactory;
private Set> checksByRepository = new HashSet<>();
private PHPChecks(CheckFactory checkFactory) {
this.checkFactory = checkFactory;
}
public static PHPChecks createPHPCheck(CheckFactory checkFactory) {
return new PHPChecks(checkFactory);
}
public PHPChecks addChecks(String repositoryKey, Iterable> checkClass) {
checksByRepository.add(checkFactory
.create(repositoryKey)
.addAnnotatedChecks(checkClass));
return this;
}
public PHPChecks addCustomChecks(@Nullable PHPCustomRuleRepository[] customRuleRepositories) {
if (customRuleRepositories != null) {
for (PHPCustomRuleRepository ruleRepository : customRuleRepositories) {
addChecks(ruleRepository.repositoryKey(), new ArrayList<>(ruleRepository.checkClasses()));
}
}
return this;
}
public List all() {
List allVisitors = new ArrayList<>();
for (Checks checks : checksByRepository) {
allVisitors.addAll(checks.all());
}
return allVisitors;
}
@Nullable
public RuleKey ruleKeyFor(PHPCheck check) {
RuleKey ruleKey;
for (Checks checks : checksByRepository) {
ruleKey = checks.ruleKey(check);
if (ruleKey != null) {
return ruleKey;
}
}
return null;
}
}