Kehraus
Splint und Co: Tools zur statischen Code-Analyse
PHP Code Sniffer
Auch für die populäre Programmiersprache PHP gibt es ein ähnliches Projekt, den PHP Code Sniffer[6]. Nach der Installation testen Sie mit phpcs eine einzelne PHP-Datei oder gleich ein komplettes Verzeichnis prüfen. Phpcs definiert dazu verschiedene Coding-Standards, die Sie über die Option --standard=Typ auswählen.
Im Beispiel aus Listing 4 beanstandet Phpcs einige eher kosmetische Kleinigkeiten wie beispielsweise falsche Einrückungen oder fehlende Leerzeichen – definitiv keine Fehler, aber unschön. Auch bei Phpcs haben Sie etliche Optionen zur Verfügung (phpcs --help), können eigene Regel-Sets definieren und einzelne Codeteile von der Prüfung ausklammern (Listing 5).
Listing 4
$ phpcs -i
The installed coding standards are PHPCS, Squiz, MySource, Zend and PEAR
$ cat test.php
<?php /* Testdatei für phpcs */
echo "abc";
if (rand(1,10)>5) {
echo "Zufallszahl >5";
}
?>
$ phpcs test.php
FILE: /home/dauti/Artikel/2012-Code-Analyse/test.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 3 LINE(S)
--------------------------------------------------------------------------------
1 | ERROR | You must use "/**" style comments for a file comment
3 | ERROR | No space found after comma in function call
4 | ERROR | Spaces must be used to indent lines; tabs are not allowed
4 | ERROR | Line indented incorrectly; expected at least 4 spaces, found 1
--------------------------------------------------------------------------------
$ phpcs --standard=PHPCS test.php
FILE: /home/dauti/Artikel/2012-Code-Analyse/test.php
--------------------------------------------------------------------------------
FOUND 10 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
1 | ERROR | You must use "/**" style comments for a file comment
1 | ERROR | Single line block comment not allowed; use inline ("// text")
| | comment instead
2 | ERROR | String "abc" does not require double quotes; use single quotes
| | instead
3 | ERROR | No space found after comma in function call
3 | ERROR | Expected 1 space before ">"; 0 found
3 | ERROR | Expected 1 space after ">"; 0 found
4 | ERROR | Spaces must be used to indent lines; tabs are not allowed
4 | ERROR | Line indented incorrectly; expected at least 4 spaces, found 1
4 | ERROR | Line indented incorrectly; expected at least 4 spaces, found 1
4 | ERROR | String "Zufallszahl >5" does not require double quotes; use single
| | quotes instead
--------------------------------------------------------------------------------
Listing 5
// @codingStandardsIgnoreStart ... dieser Code wird nicht geprüft ... // @codingStandardsIgnoreEnd
Sonstige vergleichbare Projekte
Lint-ähnliche Codeprüfer gibt es nicht nur für C, Perl und PHP, sondern auch für viele weitere Programmiersprachen. Suchen Sie doch einfach mal nach dem Begriff "lint" und Ihrer Lieblingsprogrammiersprache im Netz – Sie werden mit hoher Wahrscheinlichkeit schnell fündig. Doch solche Tools gibt es nicht nur für Programmiersprachen, sondern in vergleichbarer Form auch für das Textsatzsystem LaTeX (ChkTeX, [7]), Pakete der Typen RPM (RPM Lint, [8]) und DEB (Lintian, [9]) sowie Cascading Style Sheets (CSS Lint, [10]).



