Dieses Blog durchsuchen

Samstag, 10. September 2016

build: automate your php code reviews with ant tasks

Each php project needs a local build process, to check the code quality.
In PHP there are some standard tools to check.
- Codestyle
- Dependencies
- Complexity
- Syntaxerrors

and many more issues.

Its very handy to use tools like ant, to wrap up this QA tools. So you can automate the code review process and run this tool against your codebase.

Once you have ant installed, you can add a build.xml definition to your project. This definition orchestrates the single phptools to inspect your code and create docs.  This tasks are called "Ant-Tasks"

After you have setup your build.xml you can simly start ant to run all tests.

Later you can add this ant tasks to your jenkins buildsystem to check every releaseversion for issues in quality.

Lets start.

Prerequisits

We need some things installed on the local machine.
  • ant
  • composer

Create a testproject

I have created a small testproject with just 1 class and 1 testclass in it.
But that's okay to demonstrate the case. If you take a look in the composer.json, you will see, that all buildtools will be installed during build. So you dont need to install any tool, except ant


Just clone the project:
git clone https://github.com/pboethig/testPHPProject.git


Create your build definition

We want to create a single task for each testing tool in our defintion.
Additionalyl we have to prepare the buildfolder-structure and setup the project with this build definition.

Her is mine. It lives directly in the project root.

As you can see, there is a task named "<target>" for each tool which gets run on the codebase in the src folder.

The single targets gets run by the 2 targets TEST and REPORT. Later your will start the REPORT TASK via ant in the console. This will automaticly start the TEST target, so that single targets can be executed.

My build.xml from the projectroot
<project name="Testproject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
<mkdir dir="${basedir}/build/api"/>
<mkdir dir="${basedir}/build/api/html"/>
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/pdepend"/>
<mkdir dir="${basedir}/build/phpdox"/>
<antcall target="REPORT" />
</target>
<target name="TEST" depends="Lint, PHPUnit"></target>
<target name="REPORT" depends="TEST, phploc,phpcs,pdepend,phpmd,phpcpd,phpdox"></target>
<target name="Lint" description="Lint Check (Syntax error check)">
<apply executable="php" failonerror="true">
<arg value="-l" />
<fileset dir="${basedir}/src">
<include name="**/*.php"/>
</fileset>
</apply>
</target>
<target name="phploc" description="messure projectsize with PHPLOC">
<exec osfamily="unix" executable="${basedir}/vendor/bin/phploc">
<arg value="--count-tests" />
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
<arg path="${basedir}/src" />
</exec>
<exec osfamily="windows" executable="${basedir}/vendor/bin/phploc.bat">
<arg value="--count-tests" />
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="phpcs" description="checks missing codestyles">
<exec osfamily="windows" executable="${basedir}/vendor/bin/phpcs.bat">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--standard=${basedir}/build/phpcs/phpcs_ruleset.xml" />
<arg path="${basedir}/src" />
</exec>
<exec osfamily="unix" executable="${basedir}/vendor/bin/phpcs">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--standard=${basedir}/build/phpcs/phpcs_ruleset.xml" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="pdepend" description="checks dependencies">
<exec osfamily="windows" executable="${basedir}/vendor/bin/pdepend.bat">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramide.svg" />
<arg path="${basedir}/src" />
</exec>
<exec osfamily="unix" executable="${basedir}/vendor/bin/pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramide.svg" />
<arg path="${basedir}/src" />
</exec>
</target>
<target name="phpmd" description="checks codemess">
<exec osfamily="windows" executable="${basedir}/vendor/bin/phpmd.bat">
<arg path="${basedir}/src" />
<arg value="xml" />
<arg value="${basedir}/build/phpmd/phpmd_ruleset.xml" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramide.svg" />
</exec>
<exec osfamily="unix" executable="${basedir}/vendor/bin/phpmd">
<arg path="${basedir}/src" />
<arg value="xml" />
<arg value="${basedir}/build/phpmd/phpmd_ruleset.xml" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramide.svg" />
</exec>
</target>
<target name="phpcpd" description="find douplicate code">
<exec osfamily="windows" executable="${basedir}/vendor/bin/phpcpd.bat">
<arg value="--log-pmd" />
<arg value="${basedir}/build/logs/pmd-cpd.xml"/>
<arg path="${basedir}/src"/>
</exec>
<exec osfamily="unix" executable="${basedir}/vendor/bin/phpcpd">
<arg value="--log-pmd" />
<arg value="${basedir}/build/logs/pmd-cpd.xml"/>
<arg path="${basedir}/src"/>
</exec>
</target>
<target name="phpdox" description="Generate API Documentation">
<exec osfamily="windows" executable="${basedir}/vendor/bin/phpdox.bat">
</exec>
<exec osfamily="unix" executable="${basedir}/vendor/bin/phpdox">
</exec>
</target>
<target name="PHPUnit" description="Run PHP Unit">
<exec osfamily="unix" executable="${basedir}/vendor/bin/phpunit" failonerror="true">
<arg value="--configuration"/>
<arg value="${basedir}/phpunit.xml"/>
</exec>
<exec osfamily="windows" executable="${basedir}/vendor/bin/phpunit.bat" failonerror="true">
<arg value="--configuration"/>
<arg value="${basedir}/phpunit.xml"/>
</exec>
</target>
<target name="package" description="package the release">
<zip
destfile="${basedir}/../release/release-${TAG_TO_BUILD}.zip"
basedir="." excludes="vendor/**, release/**"/>
</target>
<target name="dist">
</target>
<target name="clean"
description="clean up">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
view raw build.xml hosted with ❤ by GitHub


Start your local build

After you have installed the tools and configured your build tasks to run that tools on your codebase

Open a terminal in your projectroot and run:

ant init

If you want to start a release build you simply can run:

After that you can see the results on the console and in the "build/logs" folder.

ant init package -DTAG_TO_BUILD=1.0.1

This will create a release packagein folder "release"


You can use this project-skelleton in jenkins buildjobs too.


For better understanding of the testresults it good to read the documentation of the tools.


Keine Kommentare:

Kommentar veröffentlichen