protected function getExistingVariable($name, $level = null, $messages = true)
    {
        $output = $messages ? $this->stdErr : new NullOutput();

        if ($level === self::LEVEL_ENVIRONMENT || ($this->hasSelectedEnvironment() && $level === null)) {
            $variable = $this->getSelectedEnvironment()->getVariable($name);
            if ($variable !== false) {
                if ($level === null && $this->getSelectedProject()->getVariable($name)) {
                    $output->writeln('Variable found at both project and environment levels: <error>' . $name . '</error>');
                    $output->writeln("To select a variable, use the --level option ('" . self::LEVEL_PROJECT . "' or '" . self::LEVEL_ENVIRONMENT . "').");

                    return false;
                }

                return $variable;
            }
        }
        if ($level !== self::LEVEL_ENVIRONMENT) {
            $variable = $this->getSelectedProject()->getVariable($name);
            if ($variable !== false) {
                return $variable;
            }
        }
        $output->writeln('Variable not found: <error>' . $name . '</error>');

        return false;
    }