Marcos Rezende
Posted on May 21, 2022
Everytime we think to start a new Symfony project we need to think about which libraries are needed to have a good code quality. It happens to me and with every single other Symfony developer, for sure.
For this reason, I decided to create a script which creates a symfony skeleton project with all needed dependencies to improve its quality.
With this script you can create your next symfony skeleton project using the four libraries, in my opinion, should be used on every symfony project.
Save the code bellow and run it as follows:
./symfony-setup-project.sh {project-name}
#!/usr/bin/env bash
set -e
PROJECT=$1
composer create-project symfony/skeleton ${PROJECT}
mkdir ${PROJECT}/tools
mkdir ${PROJECT}/tools/php-cs-fixer
cd ${PROJECT}
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
composer require --dev phpstan/phpstan
composer require --dev phpunit/phpunit ^9.5
composer require --dev symfony/phpunit-bridge
composer require --dev qossmic/deptrac-shim
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src
php vendor/bin/phpstan analyse src
php bin/phpunit
php vendor/bin/deptrac analyse
What it will do:
1. Create the new Symfony application
composer create-project symfony/skeleton ${PROJECT}
It will create a new ${PROJECT}
directory, download some dependencies into it and even generate the basic directories and files you'll need to get started.
2. Install the PHP Coding Standards Fixer
mkdir ${PROJECT}/tools
mkdir ${PROJECT}/tools/php-cs-fixer
cd ${PROJECT}
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
Following the recommendations from the PHP CS Fixer page, the above lines will install the PHP CS Fixer tool which fixes your code to follow standards; whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc., or other community driven ones like the Symfony one.
3. Install the PHP Static Analysis Tool
composer require --dev phpstan/phpstan
PHPStan is a important library which helps you on finding errors in your code without actually running it,catching whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.
4. Install the PHP Testing Framework
composer require --dev phpunit/phpunit ^9.5
composer require --dev symfony/phpunit-bridge
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.
5. Install Deptrac
composer require --dev qossmic/deptrac-shim
Deptrac is a static code analysis tool for PHP that helps you communicate, visualize and enforce architectural decisions in your projects.
Conclusion
If you use these four libraries into your Symfony Projects, you will be able to enforce that you follow the same coding standards on every class of your project, to catch bugs even before of running your project or writting tests, to build tests for your project and check it every time you delivery a new code's version, and to keep your architecture clean defining your architectural layers over classes and which rules should apply to them.
I intend to write more about these libraries, but you can easily follow the links of each one to learn more about them.
If I missed some important library here, please leave a comment helping me to improve this knowledge.
Posted on May 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.