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

Using the template requires a range of tools packages present. They can be
installed as follows:

	Install packages:
	composer global require sebastian/phpcpd:~2.0 phploc/phploc:~2.1 phpmd/phpmd:~2.2 phing/phing:~2.1 squizlabs/php_codesniffer:~2.3

	Add following to the your ".bashrc" file:
	export PATH="$PATH:$HOME/.composer/vendor/bin"
-->
<project name="inc_common" default="build">
	<import file="targets/db.xml"/>
	<import file="targets/deploy.xml"/>
	<import file="targets/folders.xml"/>

	<php function="ini_set">
		<param value="memory_limit"/>
		<param value="-1"/>
	</php>

	<!-- AutoLoader is needed to make PhpLoc (installed globally via Composer) work. -->
	<if>
		<available file="${env.HOME}/.composer/vendor/autoload.php" type="file"/>
		<then>
			<autoloader autoloaderpath="${env.HOME}/.composer/vendor/autoload.php"/>
		</then>
	</if>

	<target name="build" description="Builds a project" depends="init,verify,analyze"/>

	<target name="build-parallel" description="Builds a project (long targets executed in parallel)" depends="init,lint,tools-parallel,phpunit"/>

	<target name="tools-parallel" description="Run tools in parallel">
		<parallel threadCount="2">
			<sequential>
				<phingcall target="pdepend"/>
				<phingcall target="phpmd-ci"/>
			</sequential>

			<phingcall target="phpcpd"/>
			<phingcall target="phpcs-ci"/>
			<phingcall target="phploc"/>
			<phingcall target="phpdoc"/>
		</parallel>
	</target>

	<!-- ### Verify code consistency

	Make sure that our code is clean and functional. -->
	<target name="verify" description="Verify code consistency" depends="init,lint"/>
	<!--<target name="verify" description="Verify code consistency" depends="init,lint,phpunit"/>-->

	<!-- ### Analyze code

	Perform static analysis of the code to generate statistics, identify potential
	problems and opportunities for refactorings and make sure that the code
	complies with coding standards. -->
	<target name="analyze" description="Analyze code" depends="init,phploc,phpmd-ci,phpcpd,phpcs-ci"/>
	<!--<target name="analyze" description="Analyze code" depends="init,phploc,pdepend,phpmd-ci,phpcpd,phpcs-ci"/>-->

	<!-- ### Generate documentation

	Generate HTML documentation and code browser for the project. -->
	<target name="docs" description="Generate documentation" depends="init,phpdoc" />

	<!-- ### PHP linting

	Check files for syntax errors. -->
	<target name="lint" description="Perform syntax check of sourcecode files" depends="init">
		<phplint haltonfailure="true" deprecatedAsError="false" cachefile="${build.cache.dir}/phplint.txt">
			<fileset refid="php.files"/>
		</phplint>
	</target>

	<!-- ### Generate code statistics

	Measures the size of the project using
	[phploc](http://sebastianbergmann.github.com/phploc/) and generates
	statistics. -->
	<target name="phploc" description="Measure project size using PHPLOC" depends="init">
		<phploc reportType="csv" reportName="phploc" reportDirectory="${build.logs.dir}">
			<fileset refid="php.files"/>
		</phploc>
	</target>

	<target name="pdepend" description="Calculate software metrics using PHP_Depend" depends="init">
		<exec executable="pdepend" passthru="true">
			<arg value="--jdepend-xml=${build.logs.dir}/jdepend.xml"/>
			<arg value="--jdepend-chart=${build.pdepend.dir}/dependencies.svg"/>
			<arg value="--overview-pyramid=${build.pdepend.dir}/overview-pyramid.svg"/>
			<arg value="--ignore=${pdepend.exclude}"/>
			<arg path="${scan.dir}"/>
		</exec>
	</target>

	<!-- ### Detect code mess

	Uses [PHPMD](http://phpmd.org/) to detect code mess and look for potential
	problems. -->

	<target name="phpmd-ci" description="Perform project mess detection using PHPMD (creates a log file for the CI server)" depends="init">
		<fail message="Please create own 'phpmd-ci' task using 'parallel' and 'phpmd-ci-inc' tasks." />
	</target>

	<target name="phpmd-ci-inc">
		<exec executable="phpmd" passthru="true">
			<arg path="${param.include}"/>
			<arg value="xml"/>
			<arg value="${build.config.dir}/inc/phpmd.xml"/>
			<arg value="--reportfile"/>
			<arg value="${build.logs.dir}/${param.report.file}"/>
			<arg value="--suffixes"/>
			<arg value="php"/>
			<arg value="--exclude"/>
			<arg value="${param.exclude}"/>
		</exec>
	</target>

	<target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer (print human readable output)" depends="init">
		<exec executable="phpcs" passthru="true">
			<arg value="--standard=${vendor.dir}/aik099/coding-standard/CodingStandard"/>
			<arg value="-wps"/>
			<arg value="--encoding=UTF-8"/>
			<arg value="--extensions=php"/>
			<arg value="-d"/>
			<arg value="memory_limit=-1"/>
			<arg path="${scan.dir}"/>
		</exec>
	</target>

	<target name="phpcbf" description="Fixes coding standard violations using PHP_CodeSniffer" depends="init">
		<exec executable="phpcbf" passthru="true">
			<arg value="--standard=${vendor.dir}/aik099/coding-standard/CodingStandard"/>
			<arg value="-wps"/>
			<arg value="--no-patch"/>
			<arg value="--encoding=UTF-8"/>
			<arg value="--extensions=php"/>
			<arg value="-d"/>
			<arg value="memory_limit=-1"/>
			<arg path="${scan.dir}"/>
		</exec>
	</target>

	<target name="phpcs-ci" description="Find coding standard violations using PHP_CodeSniffer (creates a log file for the CI server)" depends="init">
		<exec executable="phpcs" passthru="true">
			<arg value="--report=checkstyle"/>
			<arg value="--report-file=${build.logs.dir}/checkstyle.xml"/>
			<arg value="--standard=${vendor.dir}/aik099/coding-standard/CodingStandard"/>
			<arg value="-wp"/>
			<arg value="--encoding=UTF-8"/>
			<arg value="--extensions=php"/>
			<arg value="-d"/>
			<arg value="memory_limit=-1"/>
			<arg path="${scan.dir}"/>
		</exec>
	</target>

	<!-- ### Detect potential copy/pasting

	Uses [phpcpd](https://github.com/sebastianbergmann/phpcpd) to detect duplicate
	code. This indicates potential refactorings.-->
	<target name="phpcpd" description="Find duplicate code using PHPCPD" depends="init">
		<exec command="which phpcpd" outputProperty="phpcpd_binary"/>
		<exec executable="php" passthru="true">
			<arg value="-d memory_limit=-1"/>
			<arg value="${phpcpd_binary}"/>
			<arg value="--log-pmd=${build.logs.dir}/pmd-cpd.xml"/>
			<arg value="--progress"/>
			<arg line="--names-exclude=*_config.php ${phpcpd.exclude}"/>
			<arg path="${scan.dir}"/>
		</exec>
	</target>

	<!-- ### Generate documentation -->
	<!-- #### Generate API Documentation

	Uses [phpDocumentor](http://www.phpdoc.org/) to generate documentation. -->
	<target name="phpdoc" description="Generate API documentation using phpdoc" depends="init">
		<phpdoc2 title="API Documentation" destdir="${build.api.dir}" template="responsive" quiet="true">
			<fileset refid="php.files"/>
		</phpdoc2>
	</target>

	<target name="phpunit" description="Run tests with PHPUnit" depends="init">
		<exec executable="phpunit" logoutput="true" checkreturn="true">
			<arg value="--configuration"/>
			<arg path="${build.config.dir}/phpunit.xml"/>
			<arg value="--testsuite"/>
			<arg value="custom"/>
		</exec>
	</target>

	<target name="init" depends="create-aliases,folders-create,deploy-ci" description="Prepare for build">
	</target>

	<target name="create-aliases" description="Creating directory aliases" hidden="true">
		<property name="pdo.dsn.without.db" value="mysql:host=${db.host}"/>
		<property name="pdo.dsn" value="${pdo.dsn.without.db};dbname=${db.name}"/>

		<property name="build.config.dir" value="."/>
		<property name="build.dir" value="${base.dir}/build"/>
		<!--<property name="build.api.dir" value="${build.dir}/api"/>-->
		<!--<property name="build.coverage.dir" value="${build.dir}/coverage"/>-->
		<property name="build.logs.dir" value="${build.dir}/logs"/>
		<property name="build.cache.dir" value="${build.dir}/cache"/>
		<!--<property name="build.pdepend.dir" value="${build.dir}/pdepend"/>-->
		<property name="vendor.dir" value="${base.dir}/vendor"/>
	</target>
</project>
