interitty / code-checker
Code checker script provides the way to check the quality of the Interitty codes.
Requires
- php: ~8.5
- ext-simplexml: *
- ext-tokenizer: *
- ext-xml: *
- ext-xmlwriter: *
- dg/composer-cleaner: ~2.2
- friendsofphp/php-cs-fixer: ~3.92
- phpmd/phpmd: ~2.15
- phpstan/extension-installer: ~1.4
- phpstan/phpstan: ~2.1
- phpstan/phpstan-deprecation-rules: ~2.0
- phpstan/phpstan-nette: ~2.0
- phpstan/phpstan-phpunit: ~2.0
- phpstan/phpstan-strict-rules: ~2.0
- slevomat/coding-standard: ~8.25
- spaze/phpstan-disallowed-calls: ~4.7
- squizlabs/php_codesniffer: ~4.0
Requires (Dev)
None
Suggests
- phpunit/php-code-coverage: Add possibility to generate PHPUnit code coverage report compatible with GitLab
- phpunit/phpunit: Run phpunit tests when the root directory of the project contains the phpunit.xml file
Provides
None
Conflicts
None
Replaces
None
README
Code checker script provides the way to check the quality of the Interitty codes.
Requirements
- PHP >= 8.5
Installation
The best way to install interitty/code-checker is using Composer:
composer require --dev interitty/code-checker
To install the git hooks, simply run the following command in the root dir of the project (where .git and vendor
folders are located):
./vendor/bin/git-hook --install
For macOS users, it is necessary to unify the coreutils with the Linux users simply by running the following command:
brew install coreutils
Usage
If the global PATH property contains a path to the folder where the code-check script is, it is possible
to run the command directly in the project folder without any arguments and check if there is an error in the code.
code-checker
Optionally the project folder can be specified by the relative or absolute path
code-checker --ignore-path=./interitty/project/vendor ./interitty/project
More than one path can also be checked by concatenating the --path parameter.
code-checker --base-dir=./interitty/project --path=./src --path=./tests
The code-checker will return the lowest exit code that happens thru execution. When the exit code is 0, everything
is OK in the project. This was used in the git-hook, which can be used as git
pre-commit,
commit-msg,
pre-push hook, or in the CI-CD pipeline.
The git-hook used as commit-msg also checks the commit
message and allows to skip checks for "work in progress" commit where the message starts on WIP: or simply .,
but not for pushing into protected branches (main).
Checkers setup
In some cases, like when a new version of PHP comes, it can be useful to set up the list of enabled checkers.
Because of that, there is an optional --checks=<CHECKS> parameter that allows specifying the enabled checks by
a comma-separated list of their names. The default value for the list contains every check. An unknown check
name is reported as an error rather than silently skipped, so a typo in the list cannot pass as a clean run.
code-checker --checks=static_analysis,code_sniffer,phpunit
An alternative way, useful for CI-CD pipelines, is to set up the CHECKS environment variable.
CHECKS=static_analysis,code_sniffer,phpunit code-checker
Code Sniffer
The code_sniffer step is the project's whole coding-standard and quality gate. It is backed by the
interitty/code-sniffer package and executed through
vendor/bin/code-sniffer check. One engine covers what used to be spread over PHP_CodeSniffer, PHP Mess
Detector and the separate composer, markdown and project checkers: PHP sources, composer.json, Markdown,
Latte, CSS, JavaScript, Bash and the repository's own artifacts are all read as documents of their own type
and held to one ruleset.
Two rulesets ship with the checker, and --ruleset=<NAME> selects between them:
| Name | Ruleset |
|---|---|
Interitty (default) | the standard every Interitty project is held to |
InterittyStrict | the stricter standard, adding the rules a project adopts once it is already clean |
Both are thin files under src/code-sniffer/ that name the sniffer's own bundled ruleset through extends:,
so the standard has exactly one definition and the checker only hands over a stable path. A name carrying a
slash is taken as a path to a ruleset file instead, which is how a project points the checker at a ruleset of
its own:
code-checker --ruleset=./code-sniffer.neon
Such a file typically extends a bundled ruleset and re-parametrises only what it needs:
extends: interitty
sniffs:
Interitty.Spelling.UnknownWord:
dictionaries:
- dictionary.txt
Fixable violations are corrected in place when code-checker runs with --fix. See the package documentation
for the full list of sniffs and their options.
Settings
There are some more settings you can need to fit your suit.
| Script parameter | Description |
|---|---|
--base-dir="…" | The base directory of all paths, which is also used for truncating output messages. |
--baseline="…" | Saved baseline to list the new and the gone findings against; needs --format=table. |
--bin-dir="…" | Path of the composer bin dir where are all executables located. |
--check_arguments_codesniffer="…" | Optional arguments for Code sniffer. |
--check_arguments_phpstan="…" | Optional arguments for PHP Static Analysis Tool. |
--check_arguments_phpunit="…" | Optional arguments for PHP Unit. |
--checks=<CHECKS> | Comma-separated list of enabled checks. See the example above for the default value. |
--color | Enable color output. |
--colors="…" | Specify the colour output in detail. |
--debug, -vv | Increase verbosity level to show also debug messages. |
--fail-fast | Stop processing other tests when the first error happens. |
--fix | Try to fix automatically what is possible. |
--generate-baseline | Generate PHPStan baseline file for the current project. |
--help, -h | Show the help message. |
--ignore-path="…" | The path that should be excluded from checks. |
--memory-limit="…" | Specifies the memory limit in the same format php.ini accepts. |
--no-color | Disable color output. |
--no-output | An alias for --quiet. |
--path="…" | The optional way to specify the path for files or folders for processing. |
--pro | Launch PHPStan Pro. |
--quiet, -q | Decrease verbosity level to 0 to hide all output. |
--ruleset="…" | Code sniffer ruleset to use; a name with a slash is a path to a ruleset file. Default: Interitty. |
--save-baseline="…" | Save the findings as a baseline for a later --baseline; needs --format=table. |
--verbose, -v | Increase the verbosity level so that warning messages are also displayed. |
--xdebug | Allow running with Xdebug for debugging purposes. |
Environment variables
Some of the settings can also be set by the environment variable.
| Environment variable | Description |
|---|---|
CHECK_ARGUMENTS_CODESNIFFER="…" | Optional arguments for Code sniffer. |
CHECK_ARGUMENTS_PHPSTAN="…" | Optional arguments for PHP Static Analysis Tool. |
CHECK_ARGUMENTS_PHPUNIT="…" | Optional arguments for PHPUnit. |
CHECKS="…" | Comma-separated list of enabled checks. See the example above for the default value. |
CLICOLOR=1 | Enable color output. |
CODE_SNIFFER_RULESET="…" | Code sniffer ruleset to use; a name with a slash is a path to a ruleset file. Default: Interitty. |
DEBUG=1 | Increase verbosity level to show also debug messages. |
DOCKER_DEV_CONTAINER="…" | Name of the docker container in which code-checker runs. |
DOCKER_ENVS="…" | Comma-separated list of environment variable names to promote them to the docker. |
GLOBAL_BASELINE="…" | Path where the PHPStan baseline file is located. |
MEMORY_LIMIT="…" | Memory limit for all checks. |
NO_OUTPUT=1 | Decrease verbosity level to 0 to hide all output. |
VERBOSE=1 | Increase the verbosity level so that warning messages are also displayed. |
XDEBUG_MODE="…" | Xdebug mode passed to PHP (e.g. debug, coverage). |
Docker support
If a global environment DOCKER_DEV_CONTAINER is set up and a docker container with the setup <name> settings
is started, the code-checker and git-hook commands are executed in it. To set the global environment value
"permanently" just add the following command into the .bashrc file or the similar file related to the shell
you are using.
export DOCKER_DEV_CONTAINER="<name>"
The docker-exec script is used to run the command in the defined docker container.
docker-exec "/path/command --some-arguments /path/to/project"
Fix IDE
The IDEs help developers in many cases, but sometimes they are not so useful. In the integration between the IDEs and third-party applications, they provide the address where the project's properties file is located instead of the project address. Less complicated but still not very useful is that these applications run from the root folder instead of the project folder.
For these cases, there is a fix-ide script that detects the address containing the nbproject folder with the project
properties and changes them according to the project directory.
For example the following call will detect existence of the /path/to/project-properties/nbproject folder and call
the given command with the project folder, that is defined as the src.dir property in the
./nbproject/project.properties file.
#${PWD} = /
fix-ide /path/command --some-arguments /path/to/project-properties
Previous command will lead to call the following one.
#${PWD} = /path/to/project
/path/command --some-arguments /path/to/project
[!note] Every wrapper honours a memory limit for the underlying PHP tool. Pass
--memory-limit <LIMIT>(the same formatphp.iniaccepts, e.g.512M) or export theMEMORY_LIMITenvironment variable. This is handy when a large file exhausts the default PHP memory limit (typically during an IDE on-save check) — append the option to the wrapper's config /Default Optionsstring, or setMEMORY_LIMITin your shell; it is forwarded into the docker container.
PHP CodeSniffer
The sniffer takes over the slot the IDE keeps for a coding standard. To run the
interitty/code-sniffer from the IDE in the docker container with
the custom ruleset, use the code-sniffer script from the
interitty/code-checker instead of the sniffer's own entry point. The
script carries the IDE path translation, the docker delegation and the memory limit, and it asks the sniffer for the
Checkstyle report, which is the shape the IDE reads to place its markers.
One entry covers everything the sniffer checks, so the coding standard, the mess detector and the coding standards fixer no longer need entries of their own — the single engine replaced all three.
For example, when the interitty/code-checker is cloned in
/opt/srv/interitty/interitty/code-checker folder and docker container is named php-fpm-dev, add following string
into NetBeans config.
/opt/srv/interitty/interitty/code-checker/bin/code-sniffer --color --docker-container=php-fpm-dev --fix-ide
The ruleset defaults to the bundled Interitty one. Append --ruleset=<NAME> for another bundled ruleset, or
--ruleset=<PATH> for a ruleset file of your own; a value carrying a slash is read as a path.
PHP Static Analysis Tool
To run the phpstan from the IDE in the docker container with the custom ruleset, use the same-named phpstan script
from the interitty/code-checker instead of the original one.

For example, when the interitty/code-checker is cloned in
/opt/srv/interitty/interitty/code-checker folder, docker container is named php-fpm-dev and preferred ruleset is
in /src/phpstan/interitty.neon, add following string into NetBeans config.
/opt/srv/interitty/interitty/code-checker/bin/phpstan --color --docker-container=php-fpm-dev --fix-ide
And following string into Configuration.
/opt/srv/interitty/interitty/code-checker/src/phpstan/interitty.neon
Occasionally it can be useful to be able to add something to ignore in PHPStan so that it is not part of the project, since the cause is already resolved and just not yet part of the current version. In this case, it is possible to generate a PHPStan baseline file into the project root or home folder.