How to Delegate Code Reviews to CI

Are you doing code reviews? No? Yes?

In both cases, you won't have too. Just add a couple of YAML lines to your CI.

I'm very grateful that Rector is getting traction lately. More and more PHP developers save dozens of hours by running simple CLI commands on their codebases.


It's a lot. But you want more laziness, right?


Rector can do much more without you even running it.

What do you Review in Code?

If you do code-review, what do you mostly do?

  1. Look for patterns, explain them, report them everywhere or just somewhere and hope the other person will fix them all.

  2. Discuss the design and why and what should be done about it.

Well, the 2nd cannot be automated, unless you're able to put it in clear step-by-step, e.g SOLID laws in procedural code. So you still have to do that, sorry.

Dead Fat Code

But 1st step can be easily automated. How? Well, let's take a dead code for example. In the last project I've helped to improve with Rector, there were 2 years of dead-code piled up. A dead that you have to:

So many human-hours wasted. In the end, we removed over 12 % of "dead fat code". Wouldn't it be better if that fat would never be got it?

Add Rector in CI in 3 Steps

  1. Install Rector
composer require rector/rector --dev
  1. Create rector-ci.yaml config just for code-reviews
# rector-ci.yaml
parameters:
    sets:
        - "dead-code"
  1. And add to your CI bash
vendor/bin/rector process src --config rector-ci.yaml --dry-run

How to add Rector CI to your Favorite CI?

I've prepared a demo with PHP code and a testing pipeline for all widely used CI services.


There you'll find all the configuration you need to let your CI do code-reviews.

1. GitHub Actions


Repository CI Feedback

# .github/workflows/php.yml
name: Rector Code Review

on: [push]

jobs:

    build:
        runs-on: ubuntu-latest

        steps:
        - uses: actions/[email protected]

        - name: Validate composer.json and composer.lock
          run: composer validate

        - name: Install dependencies
          run: composer install --prefer-dist --no-progress --no-suggest

        - name: Code Review
          run: ./vendor/bin/rector process --config rector-ci.yaml --dry-run

2. GitHub + Travis CI


Repository CI Feedback

# .travis.yml
os: linux
language: php

php:
    - '7.2'

install:
    # install composer dependencies for all stages
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress

jobs:
    include:
        -
            stage: test
            name: "Tests"
            script:
                - vendor/bin/phpunit

        -
            stage: test
            name: "Code Review"
            script:
                - vendor/bin/rector process --config rector-ci.yaml --dry-run

3. Gitlab CI


Repository CI Feedback

# .gitlab-ci.yml
# inspired from https://docs.gitlab.com/ee/ci/examples/php.html

# see https://github.com/RobBrazier/docker-php-composer
image: robbrazier/php:7.2

before_script:
    # install composer dependencies for all stages
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress

stages:
    - test

tests:
    stage: test
    script:
        - vendor/bin/phpunit

code-review:
    stage: test
    script:
        - vendor/bin/rector process --config rector-ci.yaml --dry-run

4. Bitbucket


Repository CI Feedback

# bitbucket-pipelines.yml
# see https://github.com/RobBrazier/docker-php-composer
image: robbrazier/php:7.2

pipelines:
    default:
        - step:
              name: "Build"
              script:
                  - composer install --prefer-dist --no-ansi --no-interaction --no-progress

              artifacts:
                  - build/**
                  - vendor/**
                  - composer.json # beacause of scripts
                  - composer

        # @see https://confluence.atlassian.com/bitbucket/parallel-steps-946606807.html
        - parallel:
              - step:
                    name: "Tests"
                    caches:
                        - composer
                    script:
                        - vendor/bin/phpunit

              # Run Rector CI
              - step:
                    name: "Code Review"
                    caches:
                        - composer
                    script:
                        - vendor/bin/rector process src --config rector-ci.yaml

What sets to Start With?

Here are my favorite sets I apply first:

# rector.yaml
parameters:
    sets:
        - 'coding-style'
        - 'code-quality'
        - 'dead-code'
        - 'nette-utils-code-quality'

But you don't have to stop there. Pick any of 103 sets that Rector provides. E.g.:

# rector.yaml
parameters:
    sets:
        - 'php70'
        - 'php71'
        - 'php72'
        - 'php73'
        - 'php74'



That's it!

Oh... one more thing. You don't have to resolve all the Rector reported flaws manually, just remove the --dry-run option and run it locally before pushing:

vendor/bin/rector process src --config rector-ci.yaml


Enjoy your coffee!


Found a typo? Fix it to join team of 73 people that improve content here

Rector Is Rector saving you time and money?
Support it at Patreon so it grows and saves you even more!