Refactoring des Example Projekts
Codesniffer im Projekt nutzen

Bei aller Freude über die erfolgreiche Installation wollen wir den eigentlichen Antrieb, die monotonen Arbeiten macht phpUnderControl für uns, nicht vergessen. Es wird Zeit mal wieder Quellcode zu bearbeiten.
2. Meilenstein
- der Codingstyle für Codesniffer wird auf “Zend” festgelegt
- Refactoring: Code Style
Wir legen den Zend Style für unser Projekt einfach fest und lernen ihn dann on the fly.
Hier stellt sich gleich mal die Zwischenfrage: Sollte man den Codesniffer in die Versionverwaltung integrieren und “schlechte” Commits verhindern? Es kommt mal wieder auf die Rahmenbedingungen an. Codesniffers Speicherbedarf steigt mit dem Projektvolumen, zeitkritische BugFixes, Einsatz externer Tools alles das läßt sich geschickt administieren, aber wollen wir nicht eigentlich coden? Den Spaß am Refactoring gegen den Frust beim Commit abzuwägen ist stark projektabhängig.
Festlegung für das Example Projekt: PhpUnderControl dient zur Kontrolle und keine Sniffer Integration in die Versionsverwaltung.
Welche Codingstyles kennt den CodeSniffer?
developer@lamp:/opt/cc$ phpcs -i The installed coding standards are PEAR, PHPCS, Squiz, Zend and MySource
Na wenn Zend schon drin ist brauchen wir nur noch den Schalter finden.
Usage: phpcs [-nwlsvi] [--report=<report>] [--report-file=<reportfile>] [--config-set key value] [--config-delete key] [--config-show] [--standard=<standard>] [--sniffs=<sniffs>] [--extensions=<extensions>] [--ignore=<patterns>] [--generator=<generator>] [--tab-width=<width>] <file> ... -n Do not print warnings -w Print both warnings and errors (on by default) -l Local directory only, no recursion -s Show sniff codes in all reports -v[v][v] Print verbose output -i Show a list of installed coding standards --help Print this help message --version Print version information <file> One or more files and/or directories to check <extensions> A comma separated list of file extensions to check (only valid if checking a directory) <patterns> A comma separated list of patterns that are used to ignore directories and files <sniffs> A comma separated list of sniff codes to limit the check to (all sniffs must be part of the specified standard) <standard> The name of the coding standard to use <width> The number of spaces each tab represents <generator> The name of a doc generator to use (forces doc generation instead of checking) <report> Print either the "full", "xml", "checkstyle", "csv", "emacs", "source" or "summary" report (the "full" report is printed by default) <reportfile> Write the report to the specified file path (report is also written to screen)
Na dann auf zu den Quellen und die Möglichkeiten durchgespielt:
cd /home/developer/example.phpuc/source/src phpcs --standard=zend .
FILE: /home/developer/example.phpuc/source/src/Math.php -------------------------------------------------------------------------------- FOUND 26 ERROR(S) AND 154 WARNING(S) AFFECTING 68 LINE(S) -------------------------------------------------------------------------------- ...
Refactoring beginnt
Da ist noch Refactoring nötig. Holen wir die Übersicht der Stylefehler auf die phpUnderControl Seiten. Den Aufruf von Codesniffer finden wir in der build.xml, leicht zu erkennen als <target name=”php-codesniffer”>
<target name="php-codesniffer">
<exec executable="phpcs" dir="${basedir}/source" output="${basedir}/build/logs/checkstyle.xml" error="/tmp/checkstyle.error.log">
<arg line="--report=checkstyle --standard=zend src"/>
</exec>
</target>
Den Parameter –standard=zend geändert und nach den commit ins CVS Repo sollte der nächste build getriggert werden.
cd /home/developer/example.phpuc cvs commit -m "codesniffer auf zend umgestellt"
Beim nächsten Build Check sind wir dabei und finden kurz darauf unsere 26 Error(s) im Codesniffer Abschnitt des phpUnderControl. Gz - die erste Änderung in unserem Continuous Integration Prozess.

Die bemängelten Stellen werden wir mal gleich ausbessern. Ich benutze dazu am besten schnell 2 Konsolen.
watch -n5 phpcs --standard=zend .
Nummer 1 zeigt mir alle 5 Sekunden den aktuellen Stand. In der 2. bessere ich den Code aus. Etwas Find&Replace über die Variablennamen mit Ziffern, die langen Zeilen umgebrochen, die Klammerung an den Standard angepasst und plötzlich bleiben die Meldungen in Konsole 1 aus - geschafft.
Publishing
Das nächste CVS Commit sollte unseren Erfolg visualisieren.
cvs commit -m “CS Style erfüllt”
In der Datei habe ich:
.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Manuel Pichler nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Example
* @author Manuel Pichler
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id: Math.php,v 1.4 2009-05-13 21:51:26 developer Exp $
* @link http://www.phpundercontrol.org/
*/
/**
* Simple math class.
*
* @package Example
* @author Manuel Pichler
* @author Jens Klose
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: 0.4.7
* @link http://www.phpundercontrol.org/
*/
class PhpUnderControl_Example_Math
{
/**
* Adds the two given values.
*
* @param integer $firstVar Value one.
* @param integer $secondVar Value two.
*
* @return integer.
*/
public function add($firstVar, $secondVar)
{
return ($firstVar + $secondVar);
}
/**
* Subtract param two from param one
*
* @param integer $firstVar Value one.
* @param integer $secondVar Value two.
*
* @return integer.
*/
public function sub($firstVar, $secondVar)
{
return ($firstVar - $secondVar);
}
/**
* Not tested method that should be visible with low coverage.
*/
public function div($firstVar, $secondVar)
{
$thirthVar = $firstVar / ($secondVar + $firstVar);
if ($thirthVar > 14) {
$forthVar = 0;
for ($i = 0; $i < $thirthVar; $i++) {
$forthVar += ($secondVar * $i);
}
}
$fifthVar = ($forthVar < $thirthVar
? ($thirthVar - $forthVar)
: ($forthVar - $thirthVar));
$sixthVar = ($firstVar * $secondVar
* $thirthVar * $forthVar * $fifthVar);
$d = array(
$firstVar,
$secondVar,
$thirthVar,
$forthVar,
$fifthVar,
$sixthVar);
$seventhVar = 1;
for ($i = 0; $i < $sixthVar; $i++) {
shuffle($d);
$seventhVar = $seventhVar + $i * end($d);
}
$eighthVar = $seventhVar;
foreach ( $d as $x ) {
$eighthVar *= $x;
}
$thirthVar = $firstVar / ($secondVar + $firstVar);
if ($thirthVar > 14) {
$forthVar = 0;
for ($i = 0; $i < $thirthVar; $i++) {
$forthVar += ($secondVar * $i);
}
}
$fifthVar = ($forthVar < $thirthVar
? ($thirthVar - $forthVar)
: ($forthVar - $thirthVar));
$sixthVar = ($firstVar * $secondVar * $thirthVar
* $forthVar * $fifthVar);
$d = array(
$firstVar,
$secondVar,
$thirthVar,
$forthVar,
$fifthVar,
$sixthVar
);
$seventhVar = 1;
for ($i = 0; $i < $sixthVar; $i++) {
shuffle($d);
$seventhVar = $seventhVar + $i * end($d);
}
$eighthVar = $seventhVar;
foreach ( $d as $x ) {
$eighthVar *= $x;
}
return $eighthVar;
}
/**
* Simple copy for cpd detection.
*/
public function complex($firstVar, $secondVar)
{
$thirthVar = $firstVar / ($secondVar + $firstVar);
if ($thirthVar > 14) {
$forthVar = 0;
for ($i = 0; $i < $thirthVar; $i++) {
$forthVar += ($secondVar * $i);
}
}
$fifthVar = ($forthVar < $thirthVar
? ($thirthVar - $forthVar)
: ($forthVar - $thirthVar));
$sixthVar = ($firstVar
* $secondVar
* $thirthVar
* $forthVar
* $fifthVar);
$d = array(
$firstVar,
$secondVar,
$thirthVar,
$forthVar,
$fifthVar,
$sixthVar
);
$seventhVar = 1;
for ($i = 0; $i < $sixthVar; $i++) {
shuffle($d);
$seventhVar = $seventhVar + $i * end($d);
}
$eighthVar = $seventhVar;
foreach ( $d as $x ) {
$eighthVar *= $x;
}
$thirthVar = $firstVar / ($secondVar + $firstVar);
if ($thirthVar > 14) {
$forthVar = 0;
for ($i = 0; $i < $thirthVar; $i++) {
$forthVar += ($secondVar * $i);
}
}
$fifthVar = ($forthVar < $thirthVar
? ($thirthVar - $forthVar)
: ($forthVar - $thirthVar));
$sixthVar = ($firstVar
* $secondVar
* $thirthVar
* $forthVar
* $fifthVar);
$d = array(
$firstVar,
$secondVar,
$thirthVar,
$forthVar,
$fifthVar,
$sixthVar
);
$seventhVar = 1;
for ($i = 0; $i < $sixthVar; $i++) {
shuffle($d);
$seventhVar = $seventhVar + $i * end($d);
}
$eighthVar = $seventhVar;
foreach ( $d as $x ) {
$eighthVar *= $x;
}
return $eighthVar;
}
}
Seltsam, die Codesniffer Ausgaben zeigen weiterhin Fehler an, obwohl die Message des Commits im Listing am Ende erscheint.
Aber haben wir den schon die Projektdateien unseres phpUnderControls aktualisieren lassen? Nein der Bootstrapper hat lediglich die build.xml erneuert. Da war doch was mit XML PingPong - genau in welche SteuerXML soll den nun der Projektupdate?
Das Projekt kennt seine Dateien besser und welche Versionsverwaltungen auch immer die Entwickler in Zukunft einsetzen wollen, welche Teilprojekte überhaupt aktualisiert werden sollen, alles das können die Entwickler am besten gleich in IHRER projektinternen build.xml verwalten. Ein neues Target muss her:
<target name="build" depends="cvs-update,php-documentor,php-codesniffer,phpunit"/>
<target name="cvs-update">
<exec executable="cvs" dir="${basedir}/source" logerror="on">
<arg line="update -P -d"/>
</exec>
</target>
Dieses Target auch noch ins build integriert als ersten Teil der depends Kette und ein neues commit des Projektes gewagt.
cvs commit -m "cvs update ins build"
Warten…
Erfolg! Zumindest die Codesniffer Meldungen haben wir beseitigt. Das komplette Build steht zwar noch auf rot, aber jetzt kann uns nichts mehr stoppen. Der nächste Meilenstein wartet.
Nützliche Links:
ANT Manual
CruiseControl Config
CodeSniffer
Deutsche ausführliche Anleitung von Nils Langner (PHP hates me)
Kategorie: Projekte, Aktualisiert am 24. Mai 2009 von Jens Klose | Anmelden
Cool - Gruesse aus Bochum
Sieht spannend aus.
Grüße aus dem Süden