RootsLabs

More than a tool ! GitHub Google+ LinkedIn RSS

Automatiser vos tâches sur un projet PHP avec Phing

Progi1984 - Commentaires (2)

Comme nous l’avons vu précédemment, il est possible d’assurer la qualité de votre projet PHP via Travis-CI. Mais comment faire en local sans Internet ? Phing est la solution pour lancer des tâches en local.

Phing

Installation de Phing

On ajoute le channel PEAR de Phing :

[user@computer ~]# pear channel-discover pear.phing.info
Adding Channel "pear.phing.info" succeeded
Discovery of channel "pear.phing.info" succeeded

On ajoute différents channels pour PEAR :

[user@computer ~]# pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded

[user@computer ~]# pear channel-discover pear.pdepend.org
Adding Channel "pear.pdepend.org" succeeded
Discovery of channel "pear.pdepend.org" succeeded

[user@computer ~]# pear channel-discover pear.phpmd.org
Adding Channel "pear.phpmd.org" succeeded
Discovery of channel "pear.phpmd.org" succeeded

[user@computer ~]# pear channel-discover pear.phpdoc.org
Adding Channel "pear.phpdoc.org" succeeded
Discovery of channel "pear.phpdoc.org" succeeded

[user@computer ~]# pear channel-discover components.ez.no
Adding Channel "components.ez.no" succeeded
Discovery of channel "components.ez.no" succeeded

[user@computer ~]# pear channel-discover pear.netpirates.net
Adding Channel "pear.netpirates.net" succeeded
Discovery of channel "pear.netpirates.net" succeeded

[user@computer ~]# pear channel-discover pear.symfony.com
Adding Channel "pear.symfony.com" succeeded
Discovery of channel "pear.symfony.com" succeeded

A ce moment-là, on installe Phing via PEAR :

[user@computer ~]# pear install phing/phing
WARNING: "pear/PEAR_PackageFileManager" is deprecated in favor of "pear/PEAR_PackageFileManager2"
Did not download optional dependencies: phing/phingdocs, pear/VersionControl_SVN, pear/VersionControl_Git, phpunit/PHPUnit, phpunit/PHP_CodeCoverage, pecl/Xdebug, pear/PEAR_PackageFileManager, pear/Services_Amazon_S3, pear/HTTP_Request2, pdepend/PHP_Depend, phpmd/PHP_PMD, phpunit/phpcpd, phpunit/phploc, phpdoc/phpDocumentor, pear/PHP_CodeSniffer, pear/Net_Growl, use --alldeps to download automatically
phing/phing can optionally use package "phing/phingdocs" (version >= 2.6.1)
phing/phing can optionally use package "pear/VersionControl_SVN" (version >= 0.4.0)
phing/phing can optionally use package "pear/VersionControl_Git" (version >= 0.4.3)
phing/phing can optionally use package "phpunit/PHPUnit" (version >= 3.6.0)
phing/phing can optionally use package "phpunit/PHP_CodeCoverage" (version >= 1.1.0)
phing/phing can optionally use package "pecl/Xdebug" (version >= 2.0.5)
phing/phing can optionally use package "pear/PEAR_PackageFileManager" (version >= 1.5.2)
phing/phing can optionally use package "pear/Services_Amazon_S3" (version >= 0.3.1)
phing/phing can optionally use package "pear/HTTP_Request2" (version >= 0.5.2)
phing/phing can optionally use package "pdepend/PHP_Depend" (version >= 0.10.0)
phing/phing can optionally use package "phpmd/PHP_PMD" (version >= 1.1.0)
phing/phing can optionally use package "phpunit/phpcpd" (version >= 1.3.3)
phing/phing can optionally use package "phpunit/phploc" (version >= 1.6.4)
phing/phing can optionally use package "phpdoc/phpDocumentor" (version >= 2.0.0b7)
phing/phing can optionally use package "pear/PHP_CodeSniffer" (version >= 1.3.0)
phing/phing can optionally use package "pear/Net_Growl" (version >= 2.6.0)
downloading phing-2.6.1.tgz ...
Starting to download phing-2.6.1.tgz (481,484 bytes)
.................................................................................................done: 481,484 bytes
install ok: channel://pear.phing.info/phing-2.6.1

On installe nos outils de QA (PHP_CodeBrowser, PHP_CodeSniffer, PHP_CPD, PHPDoc, PHPLoc, PHP_MD) :

[user@computer ~]# pear install PHP_CodeSniffer
Did not download optional dependencies: phpunit/PHP_Timer, use --alldeps to download automatically
pear/PHP_CodeSniffer can optionally use package "phpunit/PHP_Timer"
downloading PHP_CodeSniffer-1.4.7.tgz ...
Starting to download PHP_CodeSniffer-1.4.7.tgz (406,387 bytes)
..................................done: 406,387 bytes
install ok: channel://pear.php.net/PHP_CodeSniffer-1.4.7

[user@computer ~]# pear install --alldeps phpmd/PHP_PMD
downloading PHP_PMD-1.5.0.tgz ...
Starting to download PHP_PMD-1.5.0.tgz (49,534 bytes)
.............done: 49,534 bytes
install ok: channel://pear.phpmd.org/PHP_PMD-1.5.0

[user@computer ~]# pear install phpunit/phpcpd
downloading phpcpd-1.4.3.tgz ...
Starting to download phpcpd-1.4.3.tgz (10,297 bytes)
.....done: 10,297 bytes
downloading FinderFacade-1.1.0.tgz ...
Starting to download FinderFacade-1.1.0.tgz (4,309 bytes)
...done: 4,309 bytes
downloading fDOMDocument-1.4.2.tgz ...
Starting to download fDOMDocument-1.4.2.tgz (15,121 bytes)
...done: 15,121 bytes
downloading Finder-2.3.6.tgz ...
Starting to download Finder-2.3.6.tgz (36,360 bytes)
...done: 36,360 bytes
install ok: channel://pear.netpirates.net/fDOMDocument-1.4.2
install ok: channel://pear.symfony.com/Finder-2.3.6
install ok: channel://pear.phpunit.de/FinderFacade-1.1.0
install ok: channel://pear.phpunit.de/phpcpd-1.4.3

[user@computer ~]# pear install pear.phpunit.de/phploc
downloading phploc-2.0.2.tgz ...
Starting to download phploc-2.0.2.tgz (12,541 bytes)
.....done: 12,541 bytes
downloading Console-2.3.6.tgz ...
Starting to download Console-2.3.6.tgz (84,471 bytes)
...done: 84,471 bytes
install ok: channel://pear.symfony.com/Console-2.3.6
install ok: channel://pear.phpunit.de/phploc-2.0.2

[user@computer ~]# pear install phpunit/PHP_CodeBrowser
WARNING: "pear/PhpDocumentor" is deprecated in favor of "phpdoc/phpdocumentor"
Did not download optional dependencies: pear/Text_Highlighter, phpunit/PHPUnit, pear/PhpDocumentor, use --alldeps to download automatically
WARNING: "pear/DB" is deprecated in favor of "pear/MDB2"
Did not download optional dependencies: pear/DB, pear/MDB2, pear/Mail, use --alldeps to download automatically
phpunit/PHP_CodeBrowser can optionally use package "pear/Text_Highlighter" (version >= 0.7.1)
phpunit/PHP_CodeBrowser can optionally use package "phpunit/PHPUnit" (version >= 3.4.0)
phpunit/PHP_CodeBrowser can optionally use package "pear/PhpDocumentor" (version >= 1.4.3)
pear/Log can optionally use package "pear/DB" (version >= 1.3)
pear/Log can optionally use package "pear/MDB2" (version >= 2.0.0RC1)
pear/Log can optionally use package "pear/Mail"
pear/Log can optionally use PHP extension "sqlite"
downloading PHP_CodeBrowser-1.0.2.tgz ...
Starting to download PHP_CodeBrowser-1.0.2.tgz (171,746 bytes)
.....................................done: 171,746 bytes
downloading Console_CommandLine-1.2.0.tgz ...
Starting to download Console_CommandLine-1.2.0.tgz (39,740 bytes)
...done: 39,740 bytes
downloading File_Iterator-1.3.4.tgz ...
Starting to download File_Iterator-1.3.4.tgz (5,199 bytes)
...done: 5,199 bytes
downloading Log-1.12.7.tgz ...
Starting to download Log-1.12.7.tgz (46,898 bytes)
...done: 46,898 bytes
install ok: channel://pear.php.net/Console_CommandLine-1.2.0
install ok: channel://pear.phpunit.de/File_Iterator-1.3.4
install ok: channel://pear.php.net/Log-1.12.7
install ok: channel://pear.phpunit.de/PHP_CodeBrowser-1.0.2

[user@computer ~]# pear install phpdoc/phpDocumentor
downloading phpDocumentor-2.1.0.tgz ...
Starting to download phpDocumentor-2.1.0.tgz (2,512,395 bytes)
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................done: 2,512,395 bytes
install ok: channel://pear.phpdoc.org/phpDocumentor-2.1.0

Mise en place

Pour gérer les tâches avec Phing, il faut créer un fichier build.xml (dans mon cas, à la racine du projet).

Ce fichier contiendra une variable builddir qui sera le dossier où seront crées les fichiers de log, docs, etc…
On crée ensuite la cible principale qa On peut appeler une cible de cette manière :

phing <cible>

La cible principale qa appelle d’autres cibles :

  • qa-clean : Supprime le dossier de build
  • qa-prepare : Crée les dossiers de build et les sous dossiers
  • phpcs : Lance PHP CodeSniffer avec les deux checkstyles PSR1 et PSR2
  • phpcpd : Lance PHP copy/paste pour vérifier l’existence de parties dupliquées
  • phpmd : Lance PHP Mess Detector pour éliminer de potentiels erreurs
  • phploc : Lance PHPLOC pour faire des mesures du projet
  • pdepend : Lance PDepend pour faire d’autres mesures sur le code du projet
  • phpdoc : Lance PHPDoc pour générer une documentation propre du projet
  • phpcb : Lance PHP Code Browser pour réunir en une seule interface web les différents logs

Voici le fichier de référence :

<?xml version="1.0"?>
<project name="YATSPHP" default="qa" basedir=".">
    <!-- Property -->
    <property name="builddir" value="build" />

    <!-- Main targets -->
    <target name="qa" depends="qa-clean, qa-prepare, phpcs, phpcpd, phpmd, phploc, pdepend, phpdoc, phpcb" description="Do static analysis of the code"/>

    <!-- QA : Clean the directory -->
    <target name="qa-clean">
        <delete dir="${builddir}" />
    </target>
    <!-- QA : Prepare the directory for the build -->
    <target name="qa-prepare">
        <mkdir dir="${builddir}" />
        <mkdir dir="${builddir}/out" />
        <mkdir dir="${builddir}/logs" />
        <mkdir dir="${builddir}/docs" />
    </target>
    <!-- PHP CodeSniffer -->
    <target name="phpcs">
        <phpcodesniffer standard="PSR1" showSniffs="false" showWarnings="true">
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
            <formatter type="default" usefile="false"/>
            <formatter type="checkstyle" outfile="${builddir}/logs/checkstyle_PSR1.xml"/>
        </phpcodesniffer>
        <phpcodesniffer standard="PSR2" showSniffs="false" showWarnings="true">
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
            <formatter type="default" usefile="false"/>
            <formatter type="checkstyle" outfile="${builddir}/logs/checkstyle_PSR2.xml"/>
        </phpcodesniffer>
    </target>
    <!-- PHP copy/paste analysis -->
    <target name="phpcpd">
        <phpcpd file="src">
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
            <formatter type="pmd" outfile="${builddir}/logs/pmd.xml"/>
        </phpcpd>
    </target>
    <!-- PHP Mess Detector -->
    <target name="phpmd">
        <phpmd rulesets="codesize,design,naming,unusedcode">
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
            <formatter type="xml" outfile="${builddir}/logs/pmd.xml"/>
        </phpmd>
    </target>
    <!-- PHPLOC -->
    <target name="phploc">
        <phploc countTests="false" reportType="xml" reportDirectory="${builddir}/logs/" reportName="phploc">
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
        </phploc>
    </target>
    <!-- PDepend -->
    <target name="pdepend">
        <phpdepend>
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
            <logger type="summary-xml" outfile="${builddir}/logs/pdepend-summary.xml"/>
            <logger type="jdepend-xml" outfile="${builddir}/logs/pdepend-jdepend.xml"/>
            <analyzer type="coderank-mode" value="method"/>
        </phpdepend>
    </target>
    <!-- PHPDocumentor -->
    <target name="phpdoc">
        <phpdoc2 destdir="${builddir}/docs" template="responsive" title="YATSPHP">
            <fileset dir="src">
                <include name="**/*.php" />
                <include name="*.php" />
            </fileset>
        </phpdoc2>
    </target>
    <!-- PHP Code Browser -->
    <target name="phpcb">
        <exec logoutput="true" command="phpcb --log ${builddir}/logs --source src/ --output ${builddir}/out" />
    </target>
</project>

Conclusion

Et voilà, vous pouvez dorénavant lancer une analyse qualité de votre code directement depuis de votre machine via Phing. Vous pouvez ajouter d’autres cibles pour gérer de nombreuses choses : checkout d’un SVN ou GIT, copie de fichier, déploiement FTP, requêtage de base de données, requêtage HTTP, ZIP/TAR et décompression d’archives. Phing est puissant et vous pouvez même créer vos propres plugins Phing.

Lien : Doc Phing

Commentaires

1. RootsLabs » Phing : Fournir une archive PHAR pour votre librairie, le 9 décembre 2013 à 10:33

[…] nous l’avons vu dans cet article sur Phing, c’est un outil qui permet d’automatise de nombreuses tâches. Dans cet article, nous […]

2. RootsLabs » Optimiser ses fichiers statiques, le 6 juin 2018 à 14:04

[…] Vos fichiers statiques sont, avec votre code PHP, les fichiers à optimiser. Dans un prochain article, nous verrons comment utiliser Phing pour automatiser ces optimisations comme nous l’avons fait pour tester la qualité d’un projet. […]

Ajouter un commentaire

Commentaire :