Sauberer Code mit PHP_CodeSniffer

Gerade in großen Softwareprojekten ist die Qualitätssicherung von großer Bedeutung. Dabei spielen auch zunächst unwichtig erscheinende Dinge, wie Coding Standards eine wichtige Rolle, um die Lesbarkeit und Wiederverwendbarkeit des Codes zu verbessern.

Da es sich bei Coding Standards um klare Definitionen handelt, ist es möglich diese durch Software prüfen zu lassen.

Ein bekanntes Tool dafür ist der PHP_CodeSniffer (http://pear.php.net/package/PHP_CodeSniffer) aus dem Pear-Paket.

Mit ihm können beliebige Dateien auf einen festgelegten Aufbau kontrolliert werden. Wer bereits den PHPDocumentator benutzt, kann sich vorstellen, wie das Ganze funktioniert.
Ein Parser läuft über den Code und meldet entsprechende Abweichungen von den angegebenen Regeln.

Als Bestandteil der PEAR-Bibliothek, kann der PHP_CodeSniffer über die Kommandozeile, beispielsweise per "pear install PHP_CodeSniffer" installiert werden.

Die Ausführungen der Prüfung funktioniert dann ebenfalls über die Kommandozeile (oder automatisiert per Cronjob) durch den Befehl "phpcs -..."

Fehler werden dann beispielsweise in der folgenden Form gemeldet:

$ phpcs /path/to/code/myfile.php

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERRORS AND 1 WARNINGS AFFECTING 5 LINES
--------------------------------------------------------------------------------
2 | ERROR | Missing file doc comment
20 | ERROR | PHP keywords must be lowercase; expected "false" but found
| | "FALSE"
47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
47 | WARNING | Equals sign not aligned with surrounding assignments
51 | ERROR | Missing function doc comment
88 | ERROR | Line not indented correctly; expected 9 spaces but found 6

Die Anleitung zur Verwendung findet ihr hier:
http://pear.php.net/package/PHP_CodeSniffer/docs

Übrigens, die Zend Coding Standards, auf welche ich auch als Freund der ZendFramework setze, sind bereits dabei und sollten sich im Verzeichnis:
"/usr/share/php/PHP/CodeSniffer/Standards/Zend/ZendCodingStandard.php"

befinden.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <smarty>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Textual smileys will be replaced with graphical ones.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.