├── .github └── FUNDING.yml ├── README.md ├── action.yml └── phpstan-action.bash /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | github: [g105b] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP Static Analysis in Github Actions 2 | 3 | PHP Static Analysis in Github Actions. 4 | ====================================== 5 | 6 | PHPStan finds bugs in your code without writing tests by using running static analysis on your project's code. 7 | 8 | Usage 9 | ----- 10 | 11 | Create your Github Workflow configuration in `.github/workflows/ci.yml` or similar. 12 | 13 | ```yaml 14 | name: CI 15 | 16 | on: [push] 17 | 18 | jobs: 19 | build-test: 20 | runs-on: ubuntu-latest 21 | 22 | steps: 23 | - uses: actions/checkout@v3 24 | - uses: php-actions/composer@v6 # or alternative dependency management 25 | - uses: php-actions/phpstan@v3 26 | with: 27 | path: src/ 28 | 29 | # ... then your own project steps ... 30 | ``` 31 | 32 | ### Version numbers 33 | 34 | This action is released with semantic version numbers, but also tagged so the latest major release's tag always points to the latest release within the matching major version. 35 | 36 | Please feel free to use `uses: php-actions/phpstan@v3` to always run the latest version of v3, or `uses: php-actions/phpstan@v3.0.0` to specify the exact release. 37 | 38 | Example 39 | ------- 40 | 41 | We've put together an extremely simple example application that uses `php-actions/phpstan`. Check it out here: https://github.com/php-actions/example-phpstan. 42 | 43 | Inputs 44 | ------ 45 | 46 | The following configuration options are available: 47 | 48 | + `version` The version of PHPStan to use e.g. `9`, `9.5.0`, `latest` or `composer` (default: `composer` will use the version in your `vendor/bin` directory) 49 | + `php_version` The version of PHP to use e.g. `7.4` (default: latest) 50 | + `php_extensions` Space-separated list of extensions using [php-build][php-build] e.g. `xdebug mbstring` (default: N/A) 51 | + `vendored_phpstan_path` The path to a phar file already present on the runner (default: N/A) 52 | + `command` The command to run e.g. `list` or `worker` (default: analyse) 53 | + `path` Path(s) with source code to run analysis on, space-separated (required) 54 | + `configuration` Configuration file location 55 | + `level` Level of rule options - the higher, the stricter 56 | + `paths_file` Path to a file with a list of paths to run analysis on 57 | + `autoload_file` Project's additional autoload file path 58 | + `error_format` Format in which to print the result of the analysis 59 | + `generate_baseline` Path to a file where the baseline should be saved 60 | + `memory_limit` Memory limit for analysis 61 | + `args` Extra arguments to pass to the phpstan binary 62 | 63 | By default, adding - uses: php-actions/phpstan@v2 into your workflow will run `phpstan analyse`, as `analyse` is the default command name. 64 | 65 | You can issue custom commands by passing a command input, like so: 66 | 67 | ```yaml 68 | jobs: 69 | phpstan: 70 | 71 | ... 72 | 73 | - name: PHPStan 74 | uses: php-actions/phpstan@v3 75 | with: 76 | command: your-command-here 77 | ``` 78 | 79 | The syntax for passing in a custom input is the following: 80 | 81 | ```yaml 82 | ... 83 | 84 | jobs: 85 | phpstan: 86 | 87 | ... 88 | 89 | - name: PHPStan Static Analysis 90 | uses: php-actions/phpstan@v3 91 | with: 92 | configuration: custom/path/to/phpstan.neon 93 | memory_limit: 256M 94 | ``` 95 | 96 | If you require other configurations of phpstan, please request them in the [Github issue tracker](https://github.com/php-actions/phpstan/issues) 97 | 98 | PHP and PHPStan versions 99 | ------------------------ 100 | 101 | It's possible to run any version of PHPStan under any version of PHP, with any PHP extensions you require. This is configured with the following inputs: 102 | 103 | + `version` - the version number of PHPStan to run e.g. `1.10.14` (default: `composer`) 104 | + `php_version` - the version number of PHP to use e.g. `8.1` (default: `latest`) 105 | + `php_extensions` - a space-separated list of extensions to install using [php-build][php-build] e.g. `xdebug mbstring` (default: N/A) 106 | 107 | If you require a specific version combination that is not compatible with Github Actions for some reason, please make a request in the [Github issue tracker][issues]. 108 | 109 | *** 110 | 111 | If you found this repository helpful, please consider [sponsoring the developer][sponsor]. 112 | 113 | [php-build]: https://github.com/php-actions/php-build 114 | [issues]: https://github.com/php-actions/phpstan/issues 115 | [sponsor]: https://github.com/sponsors/g105b 116 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: PHPStan (php-actions) 2 | description: PHP Static Analysis in Github Actions. 3 | 4 | inputs: 5 | version: 6 | description: What version of PHPStan to use 7 | default: composer 8 | required: false 9 | 10 | php_version: 11 | description: What version of PHP to use 12 | default: latest 13 | required: false 14 | 15 | php_extensions: 16 | description: Space separated list of extensions to configure with the PHP build 17 | required: false 18 | 19 | vendored_phpstan_path: 20 | description: Path to a vendored phpstan binary 21 | required: false 22 | 23 | command: 24 | description: The command to run (analyse [default], clear-result-cache, dump-deps, help, list, worker) 25 | required: true 26 | default: analyse 27 | 28 | path: 29 | description: Path(s) with source code to run analysis on 30 | required: false 31 | 32 | configuration: 33 | description: Configuration file location 34 | required: false 35 | 36 | level: 37 | description: Level of rule options - the higher, the stricter 38 | required: false 39 | 40 | paths_file: 41 | description: Path to a file with a list of paths to run analysis on 42 | required: false 43 | 44 | autoload_file: 45 | description: Project's additional autoload file path 46 | required: false 47 | 48 | error_format: 49 | description: Format in which to print the result of the analysis 50 | required: false 51 | 52 | generate_baseline: 53 | description: Path to a file where the baseline should be saved 54 | required: false 55 | 56 | memory_limit: 57 | description: Memory limit for analysis 58 | required: false 59 | 60 | args: 61 | description: Extra arguments to pass to the phpstan binary 62 | required: false 63 | 64 | runs: 65 | using: "composite" 66 | steps: 67 | - env: 68 | ACTION_TOKEN: ${{ github.token }} 69 | ACTION_VERSION: ${{ inputs.version }} 70 | ACTION_PHP_VERSION: ${{ inputs.php_version }} 71 | ACTION_PHPSTAN_PATH: ${{ inputs.vendored_phpstan_path }} 72 | ACTION_PHP_EXTENSIONS: ${{ inputs.php_extensions }} 73 | ACTION_COMMAND: ${{ inputs.command }} 74 | ACTION_PATH: ${{ inputs.path }} 75 | ACTION_CONFIGURATION: ${{ inputs.configuration }} 76 | ACTION_LEVEL: ${{ inputs.level }} 77 | ACTION_PATHS_FILE: ${{ inputs.paths_file }} 78 | ACTION_AUTOLOAD_FILE: ${{ inputs.autoload_file }} 79 | ACTION_ERROR_FORMAT: ${{ inputs.error_format }} 80 | ACTION_GENERATE_BASELINE: ${{ inputs.generate_baseline }} 81 | ACTION_MEMORY_LIMIT: ${{ inputs.memory_limit }} 82 | ACTION_ARGS: ${{ inputs.args }} 83 | id: phpstan_run 84 | run: | 85 | set -e 86 | bash <(curl -s https://raw.githubusercontent.com/php-actions/php-build/59e915e4b8eeab830231a5dce6ad0998adebac05/php-build.bash) phpstan 87 | ${{ github.action_path }}/phpstan-action.bash 88 | shell: bash 89 | 90 | branding: 91 | icon: 'check-square' 92 | color: 'purple' 93 | -------------------------------------------------------------------------------- /phpstan-action.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | github_action_path=$(dirname "$0") 4 | docker_tag=$(cat ./docker_tag) 5 | echo "Docker tag: $docker_tag" >> output.log 2>&1 6 | 7 | if [ "$ACTION_VERSION" = "composer" ] 8 | then 9 | VENDOR_BIN="vendor/bin/phpstan" 10 | if test -f "$VENDOR_BIN" 11 | then 12 | ACTION_PHPSTAN_PATH="$VENDOR_BIN" 13 | else 14 | echo "Trying to use version installed by Composer, but there is no file at $ACTION_PHPSTAN_PATH" 15 | exit 1 16 | fi 17 | fi 18 | 19 | if [ -z "$ACTION_PHPSTAN_PATH" ] 20 | then 21 | phar_url="https://www.getrelease.download/phpstan/phpstan/$ACTION_VERSION/phar" 22 | phar_path="${github_action_path}/phpstan.phar" 23 | curl --silent -H "User-agent: cURL (https://github.com/php-actions)" -L "$phar_url" > "$phar_path" 24 | else 25 | phar_path="${GITHUB_WORKSPACE}/$ACTION_PHPSTAN_PATH" 26 | fi 27 | 28 | if [ ! -x "$phar_path" ]; 29 | then 30 | chmod +x "$phar_path" 31 | fi 32 | 33 | command_string=("phpstan") 34 | 35 | if [ -n "$ACTION_COMMAND" ] 36 | then 37 | command_string+=("$ACTION_COMMAND") 38 | fi 39 | 40 | if [ -n "$ACTION_PATH" ] 41 | then 42 | IFS=" " 43 | read -r -a splitIFS <<< "$ACTION_PATH" 44 | for path in "${splitIFS[@]}" 45 | do 46 | command_string+=("$path") 47 | done 48 | fi 49 | 50 | if [ -n "$ACTION_CONFIGURATION" ] 51 | then 52 | command_string+=(--configuration="$ACTION_CONFIGURATION") 53 | fi 54 | 55 | if [ -n "$ACTION_LEVEL" ] 56 | then 57 | command_string+=(--level="$ACTION_LEVEL") 58 | fi 59 | 60 | if [ -n "$ACTION_PATHS_FILE" ] 61 | then 62 | command_string+=(--paths-file="$ACTION_PATHS_FILE") 63 | fi 64 | 65 | if [ -n "$ACTION_AUTOLOAD_FILE" ] 66 | then 67 | command_string+=(--autoload-file="$ACTION_AUTOLOAD_FILE") 68 | fi 69 | 70 | if [ -n "$ACTION_ERROR_FORMAT" ] 71 | then 72 | command_string+=(--error-format="$ACTION_ERROR_FORMAT") 73 | fi 74 | 75 | if [ -n "$ACTION_GENERATE_BASELINE" ] 76 | then 77 | command_string+=(--generate-baseline="$ACTION_GENERATE_BASELINE") 78 | fi 79 | 80 | if [ -n "$ACTION_MEMORY_LIMIT" ] 81 | then 82 | command_string+=(--memory-limit="$ACTION_MEMORY_LIMIT") 83 | fi 84 | 85 | command_string+=(--ansi) 86 | 87 | if [ -n "$ACTION_ARGS" ] 88 | then 89 | command_string+=($ACTION_ARGS) 90 | fi 91 | 92 | dockerKeys=() 93 | while IFS= read -r line 94 | do 95 | dockerKeys+=( $(echo "$line" | cut -f1 -d=) ) 96 | done <<<$(docker run --rm "${docker_tag}" env) 97 | 98 | while IFS= read -r line 99 | do 100 | key=$(echo "$line" | cut -f1 -d=) 101 | if printf '%s\n' "${dockerKeys[@]}" | grep -q -P "^${key}\$" 102 | then 103 | echo "Skipping env variable $key" >> output.log 104 | else 105 | echo "$line" >> DOCKER_ENV 106 | fi 107 | done <<<$(env) 108 | 109 | echo "Command: " "${command_string[@]}" >> output.log 2>&1 110 | 111 | docker run --rm \ 112 | --volume "$phar_path":/usr/local/bin/phpstan \ 113 | --volume "${GITHUB_WORKSPACE}/vendor/phpstan:/usr/local/phpstan" \ 114 | --volume "${GITHUB_WORKSPACE}":/app \ 115 | --workdir /app \ 116 | --env-file ./DOCKER_ENV \ 117 | --network host \ 118 | ${docker_tag} "${command_string[@]}" 119 | --------------------------------------------------------------------------------