Importance of Code Style0:00
The next thing that's going to help us with automation is knowing that the code has a consistent style. Knowing that a curly brace is on a new line, or the same line, that the indentation level is two spaces, or four spaces, or a tab, allows us to make safe assumptions about the code. This helps when we're scanning the code, for example, with a regular expression, or when we're refactoring the code, and we need to make sure that it's consistent with the rest of the code in the project. Now, just as before, there are all sorts of packages out there to help us do this. We can go with something tried and true, like phpCodeSniffer, we can go with something like phpCSFixer, or we can go with Pynt, which is from the Laravel community. I like Pynt not only because it's a first-party Laravel package, but by default it automatically applies the Laravel code style. It also has a really nice JSON configuration file, and at the end of the day, it's really built
Installing and Running Pynt0:50
it's a first-party Laravel package, but by default it automatically applies the Laravel code style. It also has a really nice JSON configuration file, and at the end of the day, it's really built on top of phpcsfixer. So anything you can do with phpcsfixer, you can do with Laravel Pynt. So let's go ahead and require Laravel Pynt into our project. All right, and to use this, we can simply do vendor/bin/pynt, and it actually comes with a test command, and we'll see right away that we actually have some issues. In fact, we still have that parse error. Let's go clean that up, and we'll run the test command again. Okay, so now we just have issues with the linter, which is kind of expected because we were working on that pretty fast and loose. And here's where we get the automation for free. We can actually just run Pynt, and it automatically fixes the code in our project. There's no reason for us to do any script. It gives us the best of
Configuring Pynt Rules1:38
And here's where we get the automation for free. We can actually just run Pynt, and it automatically fixes the code in our project. There's no reason for us to do any script. It gives us the best of both worlds. So while I don't necessarily want to make this a video on Pynt, let's do take just a second to see how easy it is to configure. That way, if you want to make some small tweaks for your project, you'll know how to do it. So if we go back to our linter script, there's something that I noticed that Pynt fixed for us, and that was this concatenation operator. This is just one small thing that the Laravel code style follows that I just can't get used to. I'm just an old school php developer. I like seeing a little bit of space, a little bit of breathing room between these operators to see what I'm doing. So let's configure Pynt to respect that. We'll make a pynt.json configuration file, and if we open that configuration file in here, we basically just tell
these operators to see what I'm doing. So let's configure Pynt to respect that. We'll make a pynt.json configuration file, and if we open that configuration file in here, we basically just tell it the preset that we want to use. In this case, we do want to keep Laravel's preset, but we want to adjust a few things with it, such as some of the rules. Specifically, this concatSpace rule. So I'm going to copy this, and I'm going to change this to concatSpace one. So we'll run Pynt again, and it looks like rule must be enabled or disabled or configured as an associative array. Okay, I think what this actually is is spaces and one. So let's get rid of all that and try this one more time, see if we get lucky. No, we don't. Defined options are spacing. Okay, cool. So we're actually just hacking away here, although we probably could have read the documentation to see what we needed. Hey, there we go. Everything passes. So if I go back to the linter, it preserved
Wrapping Up and Cleanup4:08
code, which we'll see in future videos, as well as not have to worry about getting pixel perfect with code that we automatically change. We can just call the project's code formatter and allow it to do it for us. Speaking of code format, let's clean up this file before we stop. There we go. Much better.
