![]() ![]() I've started using prettier (and the php parser/plugin) to format PHP code because it's just so consistent. I haven't tried it personally, but Chris likes it, it must be good! Recently Prettier gained the ability to format PHP code. If you want to see an example of a Laravel project where both of these tools are being used, head over to the murze.be repo on GitHub that contains the code that runs this very blog. PHP-CS-Fixer and Prettier can automatically format your code. With this in place you can run Prettier with: yarn format Using a Composer script has the added benefit that, if you should every change to another tool to auto-format your code, you can change the composer script and still run the same command embedded in your muscle memory.Īdd this to your composer.json file: "scripts" : , Of course, you can name it whatever you want. I never run php-cs-fixer with the command above, but using a composer script called. You could run PHP-CS-Fixer like this: vendor/bin/php-cs-fixer fix You should add an entry to ignore this file in your. When PHP-CS-Fixer runs it will create a temporary. Head over to the usage section of the package on GitHub to see all available rules. PHP-CS-Fixer comes with a bunch of additional rules. all the imports will be sorted alphabetically.any usage of array() will be converted to.We're also going to add some extra rules: Our base will be the aforementioned PSR-2 code standard. The second statement contains what we are going to do with the selected files. blade.php files which are view templates). In every other directory, we are going to select files ending on. The storage directory will remain untouched as well. In a Laravel app, the bootstrap directory contains some autogenerated caches. We're going to exclude the vendor because otherwise composer would complain about changes in those files when we upgrade dependencies. In the first statement of this configuration, we're using Finder object to select some files. notPath ( 'vendor' ) -> notPath ( 'bootstrap' ) -> notPath ( 'storage' ) -> in ( _DIR_ ) -> name ( '*.php' ) -> notName ( '*.blade.php' ) return PhpCsFixer\Config :: create () -> setRules (, 'ordered_imports' =>, 'no_unused_imports' => true, ]) -> setFinder ($finder) Here's the configuration that I use for my Laravel projects. php_cs config file in the root of your project. Next, you can configure it by creating a. You can install PHP-CS-Fixer with: composer require - dev friendsofphp / php - cs - fixer ![]() You can install the tool globally, but I recommend to install it into your project, so anybody working on the project can make use of it too. An excellent tool for this is PHP-CS-Fixer. There are excellent paid services to enforce a coding standard, such as StyleCI, but you can also do this in your own project. Not using Laravel? No problem! Most of this can be applied to any PHP project. In this blog post, I'll show you how to automatically format PHP, JavaScript and CSS files in a Laravel project. Luckily there are many tools available that can automatically format this code. It's easy to make mistakes against those formatting rules and bit cumbersome to fix all small formatting errors. Agreeing on a coding standard makes the code more readable for all developers that need to touch the project. A coding standard like PSR-2 in the PHP world specifies rules on where certain characters, like braces of an if statement, or comma's should be put. When working on a project with other people, it's important to pick a coding standard.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |