<?xml version="1.0" encoding="utf-8"?>
<project name="PHPStan" default="check">
	<property name="path.composer-require-checker"
			  value="tmp/composer-require-checker-2.0.0.phar"/>
	<property name="url.composer-require-checker"
			  value="https://github.com/maglnet/ComposerRequireChecker/releases/download/2.0.0/composer-require-checker.phar"/>
	<target name="check" depends="
		composer-validate,
		composer-install,
		lint,
		cs,
		composer-normalize-check,
		composer-require-checker,
		test-configuration-validate,
		tests,
		phpstan
	"/>

	<target name="composer-validate">
		<exec
				executable="composer"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="validate"/>
			<arg value="--ansi"/>
		</exec>
	</target>

	<target name="composer-install">
		<exec
				executable="composer"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="install"/>
			<arg value="--ansi"/>
		</exec>
	</target>

	<target name="composer-normalize-check">
		<exec
				executable="composer"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="normalize"/>
			<arg value="--ansi"/>
			<arg value="--dry-run"/>
		</exec>
	</target>

	<target name="composer-normalize-fix">
		<exec
				executable="composer"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="normalize"/>
			<arg value="--ansi"/>
		</exec>
	</target>

	<target name="composer-require-checker">
		<if>
			<and>
				<not>
					<available file="${path.composer-require-checker}"/>
				</not>
				<not><os family="windows"/></not>
			</and>
			<then>
				<exec
					executable="wget"
					logoutput="true"
					passthru="true"
					checkreturn="true"
				>
					<arg value="-O"/>
					<arg path="${path.composer-require-checker}"/>
					<arg value="${url.composer-require-checker}"/>
				</exec>
			</then>
		</if>
		<if>
			<available file="${path.composer-require-checker}"/>
			<then>
				<exec
						executable="php"
						logoutput="true"
						passthru="true"
						checkreturn="true"
				>
					<arg path="${path.composer-require-checker}"/>
					<arg value="check"/>
					<arg value="--config-file"/>
					<arg path="${project.basedir}/build/composer-require-checker.json"/>
					<arg path="composer.json"/>
				</exec>
			</then>
		</if>
	</target>

	<target name="lint">
		<exec
				executable="vendor/bin/parallel-lint"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="--colors"/>
			<arg value="--exclude"/>
			<arg path="tests/PHPStan/Analyser/data"/>
			<arg value="--exclude"/>
			<arg path="tests/PHPStan/Rules/Methods/data"/>
			<arg value="--exclude"/>
			<arg path="tests/PHPStan/Rules/Functions/data"/>
			<arg value="--exclude"/>
			<arg path="tests/PHPStan/Rules/Operators/data/invalid-inc-dec.php"/>
			<arg value="--exclude"/>
			<arg path="tests/PHPStan/Rules/Arrays/data/offset-access-without-dim-for-reading.php"/>
			<arg path="src" />
			<arg path="tests" />
		</exec>
	</target>

	<target name="cs">
		<php expression="PHP_VERSION_ID &gt;= 70400 ?'true':'false'" returnProperty="isPHP74" level="verbose" />
		<if>
			<equals arg1="${isPHP74}" arg2="false" />
			<then>
				<exec
					executable="vendor/bin/phpcs"
					logoutput="true"
					passthru="true"
					checkreturn="true"
				>
					<arg value="--colors"/>
					<arg value="--extensions=php"/>
					<arg value="--encoding=utf-8"/>
					<arg value="--tab-width=4"/>
					<arg value="--cache=tmp/cache/phpcs"/>
					<arg value="--ignore=tests/*/data,tests/*/traits,tests/notAutoloaded,src/Reflection/SignatureMap/functionMap.php"/>
					<arg value="-sp"/>
					<arg path="src"/>
					<arg path="tests"/>
				</exec>
			</then>
		</if>
	</target>

	<target name="cs-fix">
		<exec
				executable="vendor/bin/phpcbf"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="--colors"/>
			<arg value="--extensions=php"/>
			<arg value="--encoding=utf-8"/>
			<arg value="--tab-width=4"/>
			<arg value="-sp"/>
			<arg path="src"/>
			<arg path="tests"/>
		</exec>
	</target>

	<target name="test-configuration-validate" depends="composer-install">
		<xmllint schema="vendor/phpunit/phpunit/phpunit.xsd" file="tests/phpunit.xml"/>
	</target>

	<target name="tests">
		<if>
			<os family="windows"/>
			<then>
				<property name="phpunit.executable" value="vendor/bin/phpunit"/>
			</then>
			<else>
				<property name="phpunit.executable" value="vendor/bin/paratest"/>
			</else>
		</if>
		<exec
				executable="${phpunit.executable}"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="-c"/>
			<arg value="tests/phpunit.xml"/>
			<arg path="tests"/>
		</exec>
	</target>

	<target name="phpstan">
		<property name="phpstan.config" value="build/phpstan-generated.neon"/>
		<php expression="PHP_VERSION_ID &gt;= 70300 ?'true':'false'" returnProperty="isPHP73" level="verbose" />
		<touch file="${phpstan.config}"/>
		<append
			destFile="${phpstan.config}"
			text="includes: [ phpstan.neon"
			append="false"
		></append>
		<if>
			<os family="windows" />
			<then>
				<append
						destFile="${phpstan.config}"
						text=", ignore-windows-errors.neon"
				></append>
			</then>
		</if>
		<if>
			<equals arg1="${isPHP73}" arg2="false" />
			<then>
				<append
						destFile="${phpstan.config}"
						text=", ignore-lt-php7.3-errors.neon"
				></append>
			</then>
		</if>
		<append
				destFile="${phpstan.config}"
				text=" ]"
		></append>
		<exec
				executable="php"
				logoutput="true"
				passthru="true"
				checkreturn="true"
		>
			<arg value="-d"/>
			<arg value="memory_limit=512M"/>
			<arg path="bin/phpstan"/>
			<arg value="analyse"/>
			<arg value="-c"/>
			<arg path="${phpstan.config}"/>
			<arg value="-l"/>
			<arg value="7"/>
			<arg path="build/PHPStan"/>
			<arg path="src"/>
			<arg path="tests"/>
		</exec>
	</target>

</project>
