Laravel Pint + VSCode: Start Automating Code Style to Keep Your Laravel Code Clean & Consistent

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in our previous video we took a deep dive into phpcs fixer and explored how it can help you maintain clean and consistent PHP code if you missed that video be sure to check it out I will leave a link in the video description today we are going to talk about another fantastic tool for PHP developers and that is laravel pain similar like phpcs fixer laravel pint is a coding style and formatting tool but specifically for laravel projects so in this video I will show you how to use laravel pint in your laravel application and additionally I will also show you how to set up laravel pint in Visual Studio code so let's get [Music] started so first what exactly is laravel pint and why should you consider using it in your larel projects laravel pint is an opiniated PHP code style fixer it is built on top of phpcs fixer but it is specifically designed for laravel projects pint comes with a set of default rules that follow the laravel coding style guide this me means that you can start using pint right away without having to configure anything the main difference between phpcs fixer and laravel pint is that pint is more opiniated and pint is specifically designed for laravel projects making it the preferred choice if you are working primarily with laravel PHP CS fixer on the other hand a standalone tool that can be used used with any PHP projects including laravel another key difference is that pint is easier to set up with phpcs fixer you need to install the package and need configurations with pin you just install the package and start using it no additional configuration required one more thing if we see this phpcs fixer from this link you can see it supports these phv versions on the other hand in this laravel pain if you see this composer. Json here you can see it requires minimum PHP 8.1 version now let's see laravel pain in action by default laravel pain automatically installed on create a fresh laravel application to verify this in the editor this is the newly created laravel application and if you see the composer. Json and here you can see laravel pint is already installed if you want to install this in your existing project then let's go to the GitHub repository and here is the official documentation for larel pint let's open this in the new tab using this composer require command you can install larel pint in your application this is all you need and it is ready to use in our case it is already installed so we can skip this after installing this the Pint binary file is available in the vendor bin directory let's see so in the terminal LS vendor bin and here you can see we have paint so to fix PHP file vendor bin pain it will automatically fix formatting style according to laravel coding standards let's run this and see it has fixed all these files and it has also mentioned the rules it has used to fix all these files and let's go to the editor and in the gate we can see it has fixed all these files and here we can see see the changes it has made now let's discard all these changes and on Terminal with this command we can also pass dash dash test this flag is for a dry run to show all the files that need fixing without actually fixing them so let's run this and here we go it has given the list of files that need fixing but if we see this in the editor we cannot see any changes it has made with this command we can also pass Das V flag to see what changes it need so press enter here you can see in the routes web.php here it needs two rules no extra blank lines right here and single blank line at the end of file after this line similarly all these changes it needs for these files next as I already mentioned there is no need for any configuration for paint but if you want to customize the presets or rules for this you need to create a file called pint dojason in the root directory of your project let's see this so in the edit here let's create a new file in the projects rout directory and that is paint. Json and here we have an object and here we need option preset by default pint uses the laravel preset which follows laravel's opiated coding style to fix issues you may specify different presets for example you can specify same phony or psr2 now if you see the documentation scroll down currently pin supports these predefined presets now let's try this psr2 rule set so in the terminal here we can run vendor bin paint this command automatically detects the pin. Json configuration file in the project R directory but if you want to load painter. Json from a specific directory then you can use D- config and then path of pin. Json in our case it is in the projects rout directory so we do not need to specify this so press enter to test this and now you can see these are the files fixed according to the psr2 rule set these presets are predefined group of rules so if you want you may also enable or disable specific rules in your pin. Json file as I already mentioned pint is built on top of phpcs fixer which means that you can use any of the phpcs fixer rules to fix code style issues in your project so let's see an example Le to specify a rule here we will use a rules and then we can use rules from phpcs fixer for example in the previous video we have implemented this phpcs fixer configuration file so let's see this rule AR sytax so I'm going to copy it from here and next paste that here because this is a Json file so we need to change this in the Json format like this and here right now it is using array syntax short let's say we want to use array syntax long and now in the user.php right here we have array syntax short so now let's fix this using pint so here let's run vendor bin paint and now here you can see it has fixed array syntax rule in all these files let's see this in the user.php and here we have array long syntax how cool is that let's set this to array short and format this again now this time it has changed to array short syntax let's see now it is back to array short syntax now let's set this preset to laravel and let's run this and let's run this vendor bin pint and this time it has formatted these files according to laravel preset with this laravel p print tool the entire team can use this configuration to maintain consistent code style throughout the project next we will configure the laravel Pint with the VSS code editor this will allow pint to automatically fix issues in the file when you save it so for that first let's close all of these and now here in the extensions let's search for laravel paint this one and remember that this is not an official extension let's install this it is installed so let's close this and let's open the settings so press control comma and select this user tab to open Global settings. Json and in the end of the file here you can see in the previous video we have configured the the phpcs fixer and right here we have set the default formatter phpcs fixer for PHP files so here what we want for all other PHP projects we want to use phpcs fixer but for laravel we want to use laravel pint so for that we need to override this setting in the workspace settings so in the settings let's select this workspace and click this icon to open workspace setting and this is the example setting that I have shown in the previous video to override settings in the projects workspace so let's command this and now here in the users Global setting first here I want to set laravel pint enable fals so for all other projects I want to disable it and it has asked me to reload the vs code editor so let's reload this and now let's copy these settings and paste it here next here we want to use default formatter open sou slel pain the extension vendor name and the extension name and here lar will paint enable and again it has asked to reload the window so let's reload this so here what we are doing globally by default it is going to use default formatter phpcs fixer but here in the work space setting we have override this and set the default formatter laravel pint and it should be enabled here we need to set one more option this one let's copy this and paste it here and set phpcs fixer on Save false great now let's test this so in the user.php here let's add some bad code style like this now on press contrl s to save file it should fix this error using LEL paint and it is fixed so to make sure it is fixed by laravel paint let's open the paint. Json and here array syntax let's add this to long now in the user.php scroll down and here here we have array short syntax so on save file it should change to array long syntax so contrl s it is now array long syntax great so let's undo this array short syntax and save file it is back to array short syntax it is working next in the terminal instead of running this long command on Terminal every time we can can add this in the script section of the composer. Json let's see so composer Json file right here and in this script section in the previous video we have added the these two commands for phpcs fixer similarly we can add paint and here we can say vendor Bane paint like this and now on Terminal we can run composer paint like this and it has executed this vendor bin paint and right now we do not have any files to fix so in theator let's discard these changes and run this composer paint again it has fixed all these files great you can also configure larav will pint with get pre-commit hooks to ensure that lar will pint scans and fix all staged PHP files before every commit so in the package.json I have already created video on Husky and lint stage so I recommend you to watch that video to set up Husky and lint stage in your projects so in the package.json here we have option for link stage in the previous video we have configured this phpcs fixer similar like that you can add here command for laravel pain so here you can say vendor bin paint like this now on Commit changes it should fix the coding style let's see so in the user.php let's add some bad formatting Style and also let's remove this command control shift p to save it without formatting now in the terminal let's add kit add app models user.php and get status now here we can see here we have one stage file that is user.php model get commit F&M let's say fix user.php the commit is successful and in the user.php it has fixed the bad coding style how cool is that this is all in this video we have covered everything you need to know about installing and using laravel pain in your laravel projects if you found this tutorial helpful don't forget to give it thumbs up and hit the Subscribe button for more inside coding tips thanks for watching happy coding [Music] C
Info
Channel: QiroLab
Views: 2,512
Rating: undefined out of 5
Keywords: vs code, laravel development, laravel tutorial, automated code formatting, php coding standards, clean code laravel, laravel vscode extension, code linter, static analysis, php coding best practices, laravel workflow, laravel tips and tricks, laravel code quality, laravel community, code consistency, laravel clean architecture
Id: fNMEGyKN5-Q
Channel Id: undefined
Length: 17min 13sec (1033 seconds)
Published: Thu Feb 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.