PHP 8: Modern PHP Development · Testing and Tooling

Code Style, Automated Refactoring and CI

Automate formatting with PHP-CS-Fixer, upgrades with Rector, and checks in CI.

Use PHP-CS-Fixer or PHP_CodeSniffer to enforce PER Coding Style automatically, Rector to apply automated refactorings and version upgrades (e.g., to PHP 8.3 features), and run tests + static analysis + style checks in CI on every pull request. Cache Composer dependencies, test against the PHP versions you support, and fail fast on cheap checks.

# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ['8.3', '8.4']
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
      - run: composer install --no-progress --prefer-dist
      - run: vendor/bin/php-cs-fixer check --diff
      - run: vendor/bin/phpstan analyse --no-progress
      - run: vendor/bin/phpunit

Common pitfalls

  • Mixing formatting changes with logic changes in one PR.
  • Running Rector without reviewing its diff.
  • Only testing on the newest PHP version while production runs an older one.

Key points

  • Automate style; never debate it in reviews
  • Rector automates upgrades
  • CI: style, static analysis, tests on every PR

Further reading: Rector documentation

Practice questions

What is Rector used for?

Answer

Automated refactoring: upgrading code to newer PHP versions (e.g., constructor promotion, match), framework upgrades, and enforcing code-quality rules across a codebase.

Why test against multiple PHP versions in CI?

Answer

To ensure compatibility with every version you support (e.g., production on 8.3 while preparing 8.4) and catch deprecations early.

Open in the app to mark lessons complete, take notes and track your progress.