All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.php.checks.utils.argumentmatching.ArgumentMatcherValueContainment Maven / Gradle / Ivy

The newest version!
/*
 * 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.utils.argumentmatching;

import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.sonar.php.checks.utils.CheckUtils;
import org.sonar.plugins.php.api.tree.Tree;
import org.sonar.plugins.php.api.tree.expression.ExpressionTree;
import org.sonar.plugins.php.api.tree.expression.LiteralTree;

public class ArgumentMatcherValueContainment extends ArgumentMatcher {

  private final Set values;

  > ArgumentMatcherValueContainment(ArgumentMatcherValueContainmentBuilder builder) {
    super(builder);
    this.values = builder.values;
  }

  @Override
  public boolean matches(ExpressionTree argumentValue) {
    boolean matchesValues = false;
    Optional value = nameOf(argumentValue);

    if (value.isPresent()) {
      String quoteLessLowercaseValue = CheckUtils.trimQuotes(value.get()).toLowerCase(Locale.ENGLISH);
      matchesValues = this.values.contains(quoteLessLowercaseValue);
    }
    return matchesValues;
  }

  public Optional nameOf(Tree tree) {
    String name;
    if (tree instanceof LiteralTree literal) {
      name = literal.value();
    } else {
      name = CheckUtils.nameOf(tree);
    }
    return Optional.ofNullable(name);
  }

  Set getValues() {
    return values;
  }

  public static > ArgumentMatcherValueContainmentBuilder builder() {
    return new ArgumentMatcherValueContainmentBuilder<>();
  }

  public static class ArgumentMatcherValueContainmentBuilder> extends ArgumentMatcherBuilder {
    private Set values;

    public T values(String value) {
      return values(Set.of(value));
    }

    public T values(Set values) {
      this.values = values.stream()
        .map(value -> value.toLowerCase(Locale.ENGLISH))
        .collect(Collectors.toSet());
      return (T) this;
    }

    @Override
    public ArgumentMatcherValueContainment build() {
      return new ArgumentMatcherValueContainment(this);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy