dist: xenial
os: linux

language: php

git:
    depth: 10

cache:
    directories:
        - $HOME/.composer
        - $HOME/bin

env:
    global:
        - DEFAULT_COMPOSER_FLAGS="--optimize-autoloader --no-interaction --no-progress"
        - COMPOSER_FLAGS=""

before_install:
    # turn off XDebug
    - phpenv config-rm xdebug.ini || return 0

    # Composer: boost installation
    - composer global show hirak/prestissimo -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo

jobs:
    include:
        -
            stage: Static Code Analysis
            php: 7.4
            env: COMPOSER_FLAGS="--prefer-stable"
            install:
                - travis_retry ./dev-tools/install.sh

                - travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
                - composer info -D | sort
            before_script:
                - if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then COMMIT_RANGE=$TRAVIS_COMMIT_RANGE; else COMMIT_RANGE="HEAD~..HEAD"; fi;
                - export CHANGED_PHP_FILES=`git diff --name-only --diff-filter=ACMRTUXB $COMMIT_RANGE | grep -E "\.php$"`
            script:
                # @TODO remove at 3.0
                - |
                  git archive -o /dev/null HEAD -v 2>&1 | grep tests | grep \.php \
                    | grep -v tests/Test/AbstractFixerTestCase.php \
                    | grep -v tests/Test/AbstractIntegrationCaseFactory.php \
                    | grep -v tests/Test/AbstractIntegrationTestCase.php \
                    | grep -v tests/Test/Assert/AssertTokensTrait.php \
                    | grep -v tests/Test/IntegrationCase.php \
                    | grep -v tests/Test/IntegrationCaseFactory.php \
                    | grep -v tests/Test/IntegrationCaseFactoryInterface.php \
                    | grep -v tests/Test/InternalIntegrationCaseFactory.php \
                    | grep -v tests/Test/IsIdenticalConstraint.php \
                    | grep -v tests/TestCase.php \
                    && (echo "UNKNOWN FILES DETECTED" && travis_terminate 1) || echo "NO UNKNOWN FILES"
                - ./check_trailing_spaces.sh || travis_terminate 1
                - dev-tools/vendor/bin/phpstan analyse
                - if [ -n "$CHANGED_PHP_FILES" ]; then ./dev-tools/vendor/bin/phpmd `echo "$CHANGED_PHP_FILES" | xargs | sed 's/ /,/g'` text phpmd.xml || travis_terminate 1; fi
                - ./dev-tools/vendor/bin/composer-require-checker check composer.json --config-file $(realpath .composer-require-checker.json) || travis_terminate 1
                - composer normalize --dry-run --working-dir=./dev-tools ../composer.json
                - ./dev-tools/check-shell-scripts.sh

        - &STANDARD_TEST_JOB
            stage: Fast Test
            php: 7.0
            install:
                # Composer: enforce given Symfony components version
                - if [ "$SYMFONY_VERSION" != "" ]; then composer global show symfony/flex -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS symfony/flex; fi
                - if [ "$SYMFONY_VERSION" != "" ]; then composer config extra.symfony.require $SYMFONY_VERSION || true; fi

                - travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
                - composer info -D | sort
            script:
                - vendor/bin/phpunit || travis_terminate 1
                - PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer --diff --dry-run -v fix

        -
            <<: *STANDARD_TEST_JOB
            stage: Test
            php: 5.6
            env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"

        -
            <<: *STANDARD_TEST_JOB
            stage: Test
            php: 7.1
            name: 7.1 | Symfony ~4.1.0
            env: SYMFONY_DEPRECATIONS_HELPER=disabled SYMFONY_VERSION="~4.1.0"

        -
            <<: *STANDARD_TEST_JOB
            stage: Test
            php: 7.2
            name: 7.2 | Symfony ^5.0
            env: SYMFONY_DEPRECATIONS_HELPER=disabled PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER=1 SYMFONY_VERSION="^5.0"

        -
            <<: *STANDARD_TEST_JOB
            stage: Test
            php: 7.3
            name: 7.3 | With migration rules
            before_script:
                - php php-cs-fixer fix --rules @PHP73Migration,@PHP71Migration:risky,blank_line_after_opening_tag -q || travis_terminate 1

        -
            <<: *STANDARD_TEST_JOB
            stage: Test
            php: 7.4
            name: 7.4 | Collect coverage
            before_install:
                # for building a tag release we don't need to collect code coverage
                - if [ $TRAVIS_TAG ]; then travis_terminate 0; fi

                ## regular `before_install`
                # turn off XDebug
                - phpenv config-rm xdebug.ini || return 0

                # Composer: boost installation
                - composer global show hirak/prestissimo -q || travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo

                # Require PHPUnit 8
                - composer require --dev --no-update phpunit/phpunit:^8

                # Install PCOV
                - pecl install pcov
            before_script:
                # Make code compatible with PHPUnit 8
                - PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer fix --rules=void_return -q tests || return 0
            script:
                - vendor/bin/phpunit --testsuite coverage --exclude-group covers-nothing --coverage-clover build/logs/clover.xml || travis_terminate 1
                - php vendor/bin/php-coveralls -v

        -
            <<: *STANDARD_TEST_JOB
            stage: Test
            php: nightly
            env: COMPOSER_FLAGS="--ignore-platform-reqs" PHP_CS_FIXER_IGNORE_ENV=1 SYMFONY_DEPRECATIONS_HELPER=weak

        -
            stage: Deployment
            php: 7.4
            install: ./dev-tools/build.sh
            script:
                - PHP_CS_FIXER_TEST_ALLOW_SKIPPING_SMOKE_TESTS=0 vendor/bin/phpunit tests/Smoke/

            before_deploy:
                # ensure that deployment is happening only if tag matches version of PHP CS Fixer
                - test $(php dev-tools/info-extractor.php | jq .version.vnumber) == "\"$TRAVIS_TAG\""
            deploy:
                provider: releases
                token:
                    secure: K9NKi7X1OPz898fxtVc1RfWrSI+4hTFFYOik932wTz1jC4dQJ64Khh1LV9frA1+JiDS3+R6TvmQtpzbkX3y4L75UrSnP1ADH5wfMYIVmydG3ZjTMo8SWQWHmRMh3ORAKTMMpjl4Q7EkRkLp6RncKe+FAFPP5mgv55mtIMaE4qUk=
                file: php-cs-fixer.phar
                cleanup : false
                on:
                    repo: FriendsOfPHP/PHP-CS-Fixer
                    tags: true
            after_deploy:
                - ./dev-tools/trigger-website.sh ${TRAVIS_TOKEN} ${TRAVIS_TAG}

    allow_failures:
        - php: nightly
