tomkyle / boilerplate-php
PHP package boilerplate. Automatically runs PHPUnit, PHPStan, Rector, and PHP-CS-Fixer on file changes.
Requires
- php: ^8.3
- ext-json: *
- psr/cache: ^1.0|^2.0|^3.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/log: ^1.1|^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.67
- guzzlehttp/guzzle: ^7.0
- nyholm/psr7: ^1.3
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
- rector/rector: ^2.0
- symfony/cache: ^5.0|^6.0
- tomkyle/find-run-test: ^1.0
Suggests
- guzzlehttp/guzzle: PSR-18 client (as of v7)
- monolog/monolog: PSR-3 Logger of choice
- nyholm/psr7: PSR-17 factories
- symfony/cache: PSR-6 and PSR-16 Cache engines
README
A template repository for PHP package.
Start new project
$ composer create-project tomkyle/boilerplate-php new-project
$ cd new-project
$ composer install
$ npm install
Initial setup
On first install, the composer install
command will create two things:
1. Pre-commit hook
The pre-commit hook runs the following checks before allowing any git commit
:
- PHPUnit tests
- PHPStan static analysis
- Rector to fix code style issues
- PHP CS Fixer to format the code
2. Local PHP-CS-Fixer configuration
While the .php-cs-fixer.dist.php
in the repo basically sets @PER-CS as coding standard, the local .php-cs-fixer.php
override sets the sophisticated @PhpCsFixer coding standard. Its opinionated rules are compatible with PER-CS. This setup matches my taste but leaves room for customization.
Requirements and suggestions
PSR standard | require | suggest |
---|---|---|
PSR-3 Logger implementation | psr/log | Monolog Logger |
PSR-6 Cache Implementation | psr/cache | Symfony Cache component |
PSR-17 HTTP factory* implementation | psr/http-factory | nyholm/psr7 |
PSR-18 HTTP client | psr/http-client | Guzzle 7 |
$ composer require monolog/monolog $ composer require symfony/cache $ composer require nyholm/psr7 $ composer require guzzlehttp/guzzle
Development
Watch PHP source code
Watch the file system for PHP code changes. Unit and code quality tests are automatically triggered. To manually trigger a test run, see package.json for a list of all test tasks:
$ npm run watch
Whenever a PHP file is changed, the following tasks are run:
- PHPUnit test — only for that very file
- PHPStan static analysis
- Rector to fix code style issues
PHP CS Fixer is not automatically applied, but you can invoke it manually with npm run phpcs
or npm run phpcs:apply
to apply the changes. It will, however, automatically be executed on git commit
.
Available npm scripts:
$ npm run
Overview:
- watch watches
src/
andtests/
directory - phpstan runs PHPStan static analysis
- phpcs runs PHP CS Fixer as a dry run, use
phpcs:apply
to actually apply changes. - rector runs Rector as a dry run, use
rector:apply
to actually apply changes. - phpunit runs PHPUnit tests with textdox and coverage report if available.