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

org.sonar.php.tree.impl.statement.IfStatementTreeImpl 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.tree.impl.statement;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
import org.sonar.php.tree.impl.PHPTree;
import org.sonar.php.tree.impl.lexical.InternalSyntaxToken;
import org.sonar.php.utils.collections.IteratorUtils;
import org.sonar.plugins.php.api.tree.Tree;
import org.sonar.plugins.php.api.tree.expression.ParenthesisedExpressionTree;
import org.sonar.plugins.php.api.tree.lexical.SyntaxToken;
import org.sonar.plugins.php.api.tree.statement.ElseClauseTree;
import org.sonar.plugins.php.api.tree.statement.ElseifClauseTree;
import org.sonar.plugins.php.api.tree.statement.IfStatementTree;
import org.sonar.plugins.php.api.tree.statement.StatementTree;
import org.sonar.plugins.php.api.visitors.VisitorCheck;

public class IfStatementTreeImpl extends PHPTree implements IfStatementTree {

  private final Kind kind;

  private final InternalSyntaxToken ifToken;
  private final ParenthesisedExpressionTree condition;
  private final InternalSyntaxToken colonToken;
  private final List statements;
  private final List elseifClauses;
  private final ElseClauseTree elseClause;
  private final InternalSyntaxToken endifToken;
  private final InternalSyntaxToken eosToken;

  public IfStatementTreeImpl(
    InternalSyntaxToken ifToken, ParenthesisedExpressionTree condition, StatementTree statement,
    List elseifClauses, ElseClauseTree elseClause) {
    kind = Kind.IF_STATEMENT;

    this.ifToken = ifToken;
    this.condition = condition;
    this.statements = Collections.singletonList(statement);
    this.elseifClauses = elseifClauses;
    this.elseClause = elseClause;

    this.colonToken = null;
    this.endifToken = null;
    this.eosToken = null;
  }

  public IfStatementTreeImpl(
    InternalSyntaxToken ifToken, ParenthesisedExpressionTree condition, InternalSyntaxToken colonToken,
    List statements, List elseifClauses, ElseClauseTree elseClause,
    InternalSyntaxToken endifToken, InternalSyntaxToken eosToken) {
    kind = Kind.ALTERNATIVE_IF_STATEMENT;

    this.ifToken = ifToken;
    this.condition = condition;
    this.statements = statements;
    this.elseifClauses = elseifClauses;
    this.elseClause = elseClause;

    this.colonToken = colonToken;
    this.endifToken = endifToken;
    this.eosToken = eosToken;
  }

  @Override
  public SyntaxToken ifToken() {
    return ifToken;
  }

  @Override
  public ParenthesisedExpressionTree condition() {
    return condition;
  }

  @Nullable
  @Override
  public SyntaxToken colonToken() {
    return colonToken;
  }

  @Override
  public List statements() {
    return statements;
  }

  @Override
  public List elseifClauses() {
    return elseifClauses;
  }

  @Nullable
  @Override
  public ElseClauseTree elseClause() {
    return elseClause;
  }

  @Nullable
  @Override
  public SyntaxToken endifToken() {
    return endifToken;
  }

  @Nullable
  @Override
  public SyntaxToken eosToken() {
    return eosToken;
  }

  @Override
  public Kind getKind() {
    return kind;
  }

  @Override
  public Iterator childrenIterator() {
    return IteratorUtils.concat(
      IteratorUtils.iteratorOf(ifToken, condition, colonToken),
      statements.iterator(),
      elseifClauses.iterator(),
      IteratorUtils.iteratorOf(elseClause, endifToken, eosToken));
  }

  @Override
  public void accept(VisitorCheck visitor) {
    visitor.visitIfStatement(this);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy