Featured image of post Code Style and Quality Control in PHP Projects

Code Style and Quality Control in PHP Projects

How to Maintain Consistent Code Style in Team Development

  • Maintaining consistent code style is crucial in team development

  • Configure PSR12 style in PHPStorm
    • File -> Settings -> Editor-> Code Style -> Set from -> select PSR12

Using PHP_CodeSniffer for Code Style Checks

  • Create phpcs.xml in project root for team-wide usage

  • Configuration notes:

    • Official documentation is limited - often requires debugging error messages
    • Example: To disable end xxx() comments:
      1. Search source code for //end
      2. Locate PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting::ClosingDeclarationCommentSniff
      3. Remove <rule ref="Squiz.Commenting.ClosingDeclarationComment"/> from config

Static Analysis with phpstan

  • Create phpstan.neon configuration in project root

Workflow Implementation

  1. Team members work on feature branches
  2. Merge to dev branch triggers git hook
  3. Stage server performs:
    • PHPUnit tests
    • PHPCS style checks (with phpcbf auto-fix)
    • PHPStan static analysis
  4. Successful checks trigger merge to master; failures trigger notifications

Configuration Notes

  • For containerized testing:
    • Share host machine’s SSH credentials:
    volumes:
        # Share Git credentials
        - /root/.ssh:/home/laradock/.ssh
    
  • Ensure proper permissions for SSH directory access