<?xml version="1.0" encoding="UTF-8"?>

<project name="OwsMetadata" default="build" xmlns:if="ant:if" xmlns:unless="ant:unless">
    <property name="exclusionlist" value="${basedir}/vendor,${basedir}/metadata" />
    <property name="exclusionlistwithslashes" value="${basedir}/vendor/,${basedir}/metadata/"/>

    <property name="sourcedir" value="${basedir}/src"/>
    <property name="testsdir" value="${basedir}/tests"/>
    <property name="configdir" value="${basedir}/config"/>
    <property name="execdir" value="${basedir}/vendor/bin"/>

    <property name="testFilter" value=""/>
    <property name="skipLint" value=""/>
    <property name="skipCoverage" value=""/>
    <property name="updateSnapshots" value="false"/>

    <target name="build">
        <antcall target="lint-parallel" unless:true="${skipLint}"/>
        <antcall target="php-cs-fixer-lint" unless:true="${skipLint}"/>
        <antcall target="phpstan" unless:true="${skipLint}"/>
        <antcall target="phpcs" unless:true="${skipLint}"/>
        <antcall target="phpunit"/>
    </target>

    <target name="lint-parallel">
        <exec executable="${execdir}/parallel-lint" failonerror="true">
            <arg value="${basedir}/"/>
            <arg value="-e"/>
            <arg value="php"/>
            <arg value="--short"/>
            <arg value="--exclude"/>
            <arg value="${basedir}/vendor"/>
            <arg value="--exclude"/>
            <arg value="${basedir}/public"/>
        </exec>
    </target>

    <target name="phpunit" description="Run unit tests with PHPUnit">
        <exec executable="${execdir}/phpunit" dir="${testsdir}" failonerror="true">
            <arg value="--coverage-html" unless:true="${skipCoverage}"/>
            <arg value="${basedir}/build/coverage" unless:true="${skipCoverage}"/>
            <arg value="--coverage-clover" unless:true="${skipCoverage}"/>
            <arg value="${basedir}/build/logs/phpunit.coverage.xml" unless:true="${skipCoverage}"/>
            <arg value="--log-junit" unless:true="${skipCoverage}"/>
            <arg value="${basedir}/build/logs/phpunit.xml" unless:true="${skipCoverage}"/>
            <arg value="--exclude-group"/>
            <arg value="integration"/>
            <arg value="--filter" unless:blank="${testFilter}"/>
            <arg value="${testFilter}" unless:blank="${testFilter}"/>
            <arg value="-d --snapshot-update" if:true="${updateSnapshots}"/>
        </exec>
    </target>

    <target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer">
        <exec executable="${execdir}/phpcs" failonerror="true">
            <arg value="--extensions=php,inc"/>
            <arg value="-s"/>
            <arg value="-p"/>
            <arg value="--severity=1"/>
            <arg value="--report=full"/>
            <arg value="--standard=PSR2"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${configdir}"/>
            <arg path="${sourcedir}"/>
            <arg path="${testsdir}"/>
        </exec>
    </target>

    <target name="phpstan" description="Static code analysis using PHPStan">
        <exec executable="${execdir}/phpstan" failonerror="true">
            <arg value="analyze"/>
            <arg value="--no-progress"/>
            <arg value="--no-interaction"/>
            <arg value="--memory-limit"/>
            <arg value="512M"/>
            <arg value="-c"/>
            <arg value="${basedir}/build/phpstan.neon.dist"/>
        </exec>
    </target>

    <target name="phpcbf" description="Format code using PHP_CodeSniffer">
        <exec executable="${execdir}/phpcbf" failonerror="true">
            <arg value="-p"/>
            <arg value="--severity=1"/>
            <arg value="--extensions=php,inc"/>
            <arg value="--standard=PSR2"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${configdir}"/>
            <arg path="${sourcedir}"/>
            <arg path="${testsdir}"/>
        </exec>
    </target>

    <target name="php-cs-fixer-lint" description="Check code format using php-cs-fixer">
        <exec executable="${execdir}/php-cs-fixer" failonerror="true">
            <arg value="check"/>
            <arg value="--diff"/>
            <arg value="--config=${basedir}/build/.php-cs-fixer.dist.php"/>
            <arg value="--allow-unsupported-php-version=no"/>
            <arg value="--show-progress=dots"/>
        </exec>
    </target>

    <target name="php-cs-fixer" description="Format code using php-cs-fixer">
        <exec executable="${execdir}/php-cs-fixer" failonerror="true">
            <arg value="fix"/>
            <arg value="--config=${basedir}/build/.php-cs-fixer.dist.php"/>
            <arg value="--allow-unsupported-php-version=no"/>
            <arg value="--show-progress=dots"/>
        </exec>
    </target>
</project>
