PHP Codequalität

Tools im Überblick

Alexander Bogomolov / @abogomolov

Aktuelle Situation

  • Code unstrukturiert und unübersichtlich
  • Code nicht testbar
  • Code oft zu kompliziert
  • hohe Einarbeitungszeit in fremde Projekte
  • hohe Fehleranfälligkeit

Motivation

  • Übersichtlichkeit
  • Wartbarkeit
  • Testbarkeit

Standards

PHP Mess Detector

Analysiert den Code auf mögliche Probleme:

  • mögliche Bugs
  • suboptimaler Code
  • zu komplizierter Code
  • unbenutzte Parameter, Methoden & Eigenschaften

Rules

  • Clean Code Rules
    • ElseExpression
    • StaticAccess
    • BooleanArgumentFlag
  • Code Size Rules
    • CyclomaticComplexity
    • NPathComplexity
    • ExcessiveMethodLength, ExcessiveClassLength, ExcessiveParameterList, ExcessivePublicCount
    • TooManyFields
    • TooManyMethods
    • ExcessiveClassComplexity

Rules

  • Controversial Rules
    • Superglobals
    • CamelCase*
  • Design Rules
    • ExitExpression
    • EvalExpression
    • GotoStatement
    • NumberOfChildren
    • DepthOfInheritance
    • CouplingBetweenObjects

Rules

  • Naming Rules
    • ShortVariable & LongVariable
    • ShortMethodName
    • ConstructorWithNameAsEnclosingClass
    • ConstantNamingConventions
    • BooleanGetMethodName
  • Unused Code Rules

Installation

Schnell und einfach mit Composer

composer global require phpmd/phpmd

Verwendung

phpmd {source} {format} {rule} > {output}
~/.composer/vendor/bin/phpmd module/ xml codesize > codesize.xml

Demo

PhpStorm

NetBeans

PHP Copy/Paste Detector (PHPCPD)

Findet doppelten Code

Installation

Schnell und einfach mit Composer

composer global require sebastian/phpcpd

Verwendung

phpcpd {source}
~/.composer/vendor/bin/phpcpd module/

Output

phpcpd 2.0.2 by Sebastian Bergmann.

Found 3 exact clones with 138 duplicated lines in 5 files:

- module/ShowcaseReelAdmin/src/ShowcaseReelAdmin/Controller/ReelController.php:274-332
  module/ShowcaseReelAdmin/src/ShowcaseReelAdmin/Controller/ReelController.php:409-467

- module/ShowcaseReelAdmin/src/ShowcaseReelAdmin/Model/Table/ProjectTable.php:118-159
  module/ShowcaseReelAdmin/src/ShowcaseReelAdmin/Model/Table/ReelTable.php:124-165

- module/ShowcaseReelAdmin/src/ShowcaseReelAdmin/Form/ReelForm.php:79-118
  module/ShowcaseReelAdmin/src/ShowcaseReelAdmin/Form/ProjectForm.php:69-108

0.78% duplicated lines out of 17777 total lines of code.

Time: 543 ms, Memory: 16.00Mb

PHP_CodeSniffer

Sucht und findet Abweichungen von den Coding-Standards

Installation

Schnell und einfach mit Composer

composer global require squizlabs/php_codesniffer

Verwendung

phpcs --standard={standard} {source}
~/.composer/vendor/bin/phpcs --standard=PSR2 -a module/

Output

FILE: ...w/code-review/rocks-rocketshare/module/ShowcaseClient/Module.php
----------------------------------------------------------------------
FOUND 30 ERRORS AFFECTING 26 LINES
----------------------------------------------------------------------
5 | ERROR | [x] Whitespace found at end of line
6 | ERROR | [x] Whitespace found at end of line
13 | ERROR | [x] Whitespace found at end of line
14 | ERROR | [x] Whitespace found at end of line
16 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
17 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
18 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 2
22 | ERROR | [x] Whitespace found at end of line
24 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
25 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
26 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4
28 | ERROR | [x] Line indented incorrectly; expected 8 spaces, found 4
28 | ERROR | [x] Expected 1 space after IF keyword; 0 found
28 | ERROR | [x] Expected 1 space after closing parenthesis; found "\n "
29 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4
30 | ERROR | [x] Line indented incorrectly; expected at least 12 spaces, found 7
30 | ERROR | [x] Whitespace found at end of line
34 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 2
38 | ERROR | [x] Whitespace found at end of line
41 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
42 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
43 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4
43 | ERROR | [x] Whitespace found at end of line
46 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 2
51 | ERROR | [x] Whitespace found at end of line
53 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
54 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 2
55 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4
56 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 6
62 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 2
----------------------------------------------------------------------
PHPCBF CAN FIX THE 30 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Standards

 ~/.composer/vendor/bin/phpcs -i
The installed coding standards are Squiz, PSR1, MySource, PHPCS, PEAR, PSR2 and Zend

Standards erweitern?

Klar!

Für fast jedes Open-Source Projekt gibt es fertige Sniffs.

Sniff installieren

  1. Sniff herunterladen:
    composer global require wp-coding-standards/wpcs
  2. Sniff registrieren (Symlink):
    ln -s ~/.composer/vendor/wp-coding-standards/wpcs/WordPress/ ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/
  3. Überprüfen:
    ~/.composer/vendor/bin/phpcs -i
    The installed coding standards are DrupalPractice, Squiz, PSR1, MySource, Drupal, Symfony2, WordPress, PHPCS, PEAR, PSR2, Zend and Symfony2

PhpStorm

NetBeans

PHPLOC

Misst und analysiert die Struktur des PHP Projekts

Installation

Schnell und einfach mit Composer

composer global require phploc/phploc

Verwendung

phploc {source}
~/.composer/vendor/bin/phploc module/

Output

phploc 2.1.1 by Sebastian Bergmann.

Directories                                         46
Files                                              121

Size
  Lines of Code (LOC)                            17777
  Comment Lines of Code (CLOC)                    4756 (26.75%)
  Non-Comment Lines of Code (NCLOC)              13021 (73.25%)
  Logical Lines of Code (LLOC)                    3755 (21.12%)
    Classes                                       3265 (86.95%)
      Average Class Length                          30
        Minimum Class Length                         0
        Maximum Class Length                       273
      Average Method Length                          3
        Minimum Method Length                        0
        Maximum Method Length                       61
    Functions                                       25 (0.67%)
      Average Function Length                        0
    Not in classes or functions                    465 (12.38%)
Cyclomatic Complexity
  Average Complexity per LLOC                     0.16
  Average Complexity per Class                    6.72
    Minimum Class Complexity                      1.00
    Maximum Class Complexity                     71.00
  Average Complexity per Method                   1.80
    Minimum Method Complexity                     1.00
    Maximum Method Complexity                    31.00

Dependencies
  Global Accesses                                    2
    Global Constants                                 0 (0.00%)
    Global Variables                                 0 (0.00%)
    Super-Global Variables                           2 (100.00%)
  Attribute Accesses                               593
    Non-Static                                     592 (99.83%)
    Static                                           1 (0.17%)
  Method Calls                                    3310
    Non-Static                                    3282 (99.15%)
    Static                                          28 (0.85%)

Structure
  Namespaces                                        37
  Interfaces                                         1
  Traits                                             0
  Classes                                          107
    Abstract Classes                                 8 (7.48%)
    Concrete Classes                                99 (92.52%)
  Methods                                          779
    Scope
      Non-Static Methods                           777 (99.74%)
      Static Methods                                 2 (0.26%)
    Visibility
      Public Methods                               706 (90.63%)
      Non-Public Methods                            73 (9.37%)
  Functions                                         61
    Named Functions                                  0 (0.00%)
    Anonymous Functions                             61 (100.00%)
  Constants                                         38
    Global Constants                                 0 (0.00%)
    Class Constants                                 38 (100.00%)

Integration

  • IDE (PhpStorm / NetBeans)
  • Pre-Commit Hook
  • Jenkins

Fragen?

Vielen Dank!