Installing phpcsfixer0:00
Let's move on to formatting. So imagine you want to follow PSR-2. Well, because we have that IntelliFence package, we get some of that available out of the box. So imagine we have something that is not PSR-2. I can do Command Palette Format or Shift Option F, and yes, that will fix it. But it's not overly configurable. Sometimes you'll find little irregularities, things like this, and maybe you even want more control. Like when I format, I also want to remove unused classes and things like that. So instead, if you want, you might consider pulling in phpcsfixer. Why don't we grab this one. So we'll install that, reload. So now let's go back to that Foobar class. We'll make a change here, and let's give it a shot. So phpcsfixer, fix this file, and there we go. Let's see how that works. If we click through to Settings, we can configure all of that. So if you have a custom install of phpcsfixer, like through a global composer install,
Enable Format on Save0:46
and there we go. Let's see how that works. If we click through to Settings, we can configure all of that. So if you have a custom install of phpcsfixer, like through a global composer install, you can reference that, and we can even run this on save. And by the way, the default is PSR 2, so we don't have to change a thing. Okay, so why don't we Command Comma, we're going to look for phpcsfixer, and change this to true. Okay, so let's give that one a shot. I'll bring it back to how we had before, and now Command S to save, and it instantly formats it. However, let's go back one more time. So if we scroll down, you'll see this section for rules. So it's defaulting to PSR 2, but there's countless rules you can use. For example, I happen to have this installed on my machine, and let's do the readme there. So there's a lot to take in here, but if I scroll up, we can view all of the various rules or fixers that can be applied. Things like single class
Custom Rules Configuration1:33
my machine, and let's do the README there. So there's a lot to take in here, but if I scroll up, we can view all of the various rules or fixers that can be applied. Things like single class element per statement, or a simplified null return, or I know there's one I often use called remove, let's find it, remove unused. There it is, no unused imports. So this means when you save the file, it will check for any imports that are no longer being used, like after a refactor, and it will strip those away. So there's dozens and dozens of fixers like this. Let's see how to use it. If we scroll down, we can use a custom configuration file, like so, and then we can set this up. Okay, so let's give it a shot. Once again, it's going to look for this by default. So let's go into cd, there should be a VS Code directory, and it looks like it will search for the file there. So we'll touch .phpcs, and let's open that. Oh, but actually, we haven't reviewed that yet. So this is an aside, but there
Using Code CLI Command2:23
be a vs code directory, and it looks like it will search for the file there. So we'll touch .phpcs, and let's open that. Oh, but actually, we haven't reviewed that yet. So this is an aside, but there should be, if we search for shell, install code command and path. So now, if you open up a new tab, you can use this code keyword to instantly open any file within the directory. Okay, so let's see. It's giving us, where is it? Here we go. So we can just grab all of this junk right here, right here, and paste it in. Now, you'll see that all of these refer to various rules or various fixers. So once again, if I say fixer README, let's look for one. Let's say combine consecutive unsets. So you can search for that. There you go. So calling unset on multiple items should be done in one call. So in our case, we wanted that no unused. Where is it? no unused. Yeah, there we go. So let's just uncomment that section entirely. Okay, so let's see if it works. I'm going to go
Fixing Config Path Issues3:12
in one call. So in our case, we wanted that no unused. Where is it? No unused. Yeah, there we go. So let's just uncomment that section entirely. Okay, so let's see if it works. I'm going to go back to foobar, and we're going to pull in something like request. And if I save it, that should be stripped out. Save. Ah, but now it's not working. Maybe we have to give it the full path. So let's go back to VS Code. We do have that there, correct? Yes, we do. So let's get the present working directory, and let's use that path instead. So phpcsfixer, we'll change that to be this. Okay, let's see if that works now. Save. And yeah, there we go. So one more time, let's make some changes, like so. I will hit save. It should remove request, and it should put the braces on their own line. Save, and we're done. Now, I do recommend reviewing all of the rules, because there's some cool stuff it'll do. For example, maybe you have a section with the old
Showcasing Fixer Examples4:06
braces on their own line. Save, and we're done. Now, I do recommend reviewing all of the rules, because there's some cool stuff it'll do. For example, maybe you have a section with the old array syntax, like names is one, two, three. Well, phpcsfixer will fix that on the fly. Save, and now you have the new form. And you'll see if we take a look, I'm not sure what it's called, array. Let's look. There we go. Array syntax. So we're saying we want to use the short version. The same with single quotes. So if we have, let's change this to three. Save. You'll notice it changes that to single quotes. So this allows you to automate so many of these things. Another one I often use is a short if statement, and then when I save it, it will expand it. So I could say something like val is true, and then if val, then return good, or whatever. If I save this, it will now expand for me. So yeah, imagine how useful all this is. We have
So I could say something like $val is true, and then if $val, then return good, or whatever. If I save this, it will now expand for me. So yeah, imagine how useful all this is. We have $vals. It's once again some old array syntax. We have a short if statement here. Maybe at one time we were using the request, but we later decided to just redirect somewhere. Yeah, so all of these things, the moment you hit save, it's going to fix it for you. So it should remove the request, switch to short array, add the braces here, and adjust for PSR-2. So command s, and there we go. Short array, long braces. Oh, but in this case, yeah, of course, it didn't strip out the request class because we are using it here. So if I undo that, and you say, okay, I'm no longer using that, command s, and it's still not working. You know what? I bet that's a bug. I bet maybe it's seen this here, and it's thinking it can't remove it. Yep, and now it goes away. Nothing works, right?
