در حال بارگذاری ...

Introducing phpcsfixer0:00

Because we now have that Sublime Linter package installed, yes, it's going to pick up on any code standard issues, like even something like this. It'll let you know, hey, that should be on its own line. Or if we have a method like this, once again, it's going to let you know, hey, if you're doing PSR2, you want to make sure that's on its own line. But it would really be nice if it did this for us automatically. So we can use phpcsfixer for this, which is a command line utility, but we also, of course, need like a Sublime wrapper around it that will hook into it. Alternatively, what you could do is set up a new build system for your PHP that will automatically trigger that executable each time you save.

Installing Sublime wrapper0:30

Alternatively, what you could do is set up a new build system for your php that will automatically trigger that executable each time you save. But let's use this package instead. So the first step is we have to pull it in globally, just like we did with Codesniffer. So let's go ahead and run that. And now, while it's doing its thing, yeah, here's the name of the package, sublime-phpcsfixer. So let's install it. phpcsfixer. There we go.

phpcsfixer. There we go. And for configuration, yes, we've pulled that in. All right, so I think we should be all set to go. All right, so let's make a few changes here. All right, it is squawking about that. But now if I look for phpcsfixer, yeah, now we can run that. There we go. We run it, and you'll see that it automatically performs those fixes. But now, it would be nice if we could run that automatically on save.

Auto-fix on save1:15

We run it, and you'll see that it automatically performs those fixes. But now, it would be nice if we could run that automatically on save. So let's switch over. Okay, that's configuration. Let's see what the default settings are. Okay, here are the options we can do. So on save seems to be one. So let's set that. On save will be true. Okay, now that should take care of it for us.

On save will be true. Okay, now that should take care of it for us. So if I click save now, Command-S, it automatically performs the fix. But let's even take it a step further. If I switch to the command line, you can see phpcsfixer is available to us because we installed it globally through Composer. And yeah, there's a README. Why don't we run that to see a list of fixers? You can, of course, configure this however you want. So you don't just have to follow PSR2 or something like that.

Exploring available fixers2:04

You can, of course, configure this however you want. So you don't just have to follow PSR-2 or something like that. So I'm trying to find a list here. Yeah, okay, so here's the documentation. And you can view all of these things that you can optionally add in, like no-unneeded-control-parentheses. Or I think there is a... let me look for unused. No unused imports. So this means when you run the command, it will look for any imports you have that are not being used,

Creating config file2:25

So this means when you run the command, it will look for any imports you have that are not being used, and it will automatically strip those. So if you want to add that in here, let's switch back. And I think it tells us how. All right, so we can create a .phpcsfixer config file as explained here. So it looks like we need a .phpcsfixer.php file where we can specify all of the rules we need.

And now if we scroll down, it looks like it's a php file where we can specify all of the rules we need. Or let's take a look here. So let's look for setRules. And yeah, maybe something like that should do the trick. Okay, we're in vim here, but of course you can do this in php if you want. So we're creating a new configuration. We want PSR2 formatting. We want short arrays. And now let's add that new one here.

We want short arrays. And now let's add that new one here. No unused imports. And we'll set that to true. All right, so that should be good. Let's exit out. But if we switch back, we also have to specify the path to the configuration file. So let's copy that back to Sublime. And we're going to go to csfixer, user settings. And we already have one.

Applying rules with examples3:26

And we're going to go to csfixer, User settings. And we already have one. And now let's specify where the config is located, which would be users/jeffreyway.phpcsfixer. So now notice we are type hinting User and we are importing it. But notice that it's superfluous because we're already within the app namespace. So now, like, let's make a change here so we can see a process. And if we run it, it automatically removes it because, once again, you don't need it. Let's review another example. Like, let's make a controller called UsersController.

Let's review another example. Like, let's make a controller called UsersController. All right, and switch over there. And yeah, maybe we pull in User, but we never actually use it anywhere in the controller. Well, if we run it, it will automatically be stripped. But if we have a method that uses User, in some way, if we run it, you'll see that it does keep User. But also notice that it stripped out request because, once again, you never use that. So that means, like, if I undo this to what we had initially, yeah, if we have this file that is generated by Laravel,

So that means, like, if I undo this to what we had initially, yeah, if we have this file that is generated by Laravel, well, now if we save it, it will detect that request was just added as a convenience for you. And it's not necessary. So we run it, and it will strip that for you automatically. So definitely have a look at the various fixtures you can use. I generally stick with PSR-2 and then one or two little custom ones. So like I said, if we have thing here, and you have the old array syntax like this, well, once again, if we run this, it will automatically convert that to the short array syntax. So it's actually very, very powerful once you add it to your setup.

This is just a simple explanation with no code.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟