Skip to content

Instantly share code, notes, and snippets.

@pavlakis
Created October 25, 2018 23:16
Show Gist options
  • Save pavlakis/9c1ec4c70d5e4213ddc60be38bc3093b to your computer and use it in GitHub Desktop.
Save pavlakis/9c1ec4c70d5e4213ddc60be38bc3093b to your computer and use it in GitHub Desktop.
Running PHPStan on a legacy Yii project

Running PHPStan on a legacy Yii project

Yii Framework

The Yii Framework (version 1) is compatible with PHP 7.2 (as of 1.1.20) and can also use composer. However, as a legacy project many still run it on older PHP versions.

To run PHPStan against it, I am using a Docker image with PHP 7.2 and Composer.

Composer

Below is the minimum composer.json we need to set this up.

I am using a classmap to include all the files I want to test, while also excluding some framework files which Yii uses for optimisations and can cause conflicts.

composer.json

{
    "autoload-dev": {
        "classmap": [
            "protected/models/",
            "protected/commands/",
            "protected/components/",
            "vendor/yiisoft/yii/framework/yiilite.php"
        ],
        "exclude-from-classmap": [
	        "vendor/yiisoft/yii/framework/YiiBase.php",
	        "vendor/yiisoft/yii/framework/yii.php"
        ]
    },
    "require-dev": {
        "yiisoft/yii": "^1.1"
    },
    "config": {
        "bin-dir": "bin",
        "optimize-autoloader": true
    }
}

PHPStan

I am running PHPStan using a Docker container with a different version and as such I don't want to pollute my normal composer.json

Below is the shell script I am using to set it up. PHPStan is installed using composer global which only lives inside the Docker container.

run-static-analysis.sh

#!/usr/bin/env sh

composer global require phpstan/phpstan
composer install

~/.composer/vendor/bin/phpstan analyse protected/models/ --level 0 

To run it, I just need to call this from a Docker container:

docker-run-static-analysis.sh

#!/usr/bin/env sh

docker run --name php-7.2.11 --rm -v "$(pwd)":/var/www/html:rw pavlakis/php-cli-common:7.2.11 sh -c "./run-static-analysis.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment