Playing
01. Sniff Out Unclean Code with PHP CodeSniffer

Episodes

0%
Your progress
  • Total: 9m
  • Played: 0m
  • Remaining: 9m
Join or sign in to track your progress

Transcript

00:00
In this video, we're going to talk about coding style guides and how we can use something called PHP code sniffer
00:07
to detect any problems with a particular standard we're trying to adhere to. Now in my case, I'm going to be using the PSR 2 coding style guide.
00:17
If you head over to phpfig.org, you can go ahead and click the PSR 2 link here. That will take you through to this coding style guide. If you don't currently code to a specific style,
00:29
this might cause problems later down the line, particularly when other people come onto your project. Or if you're working as part of a development team, this can be a massive problem.
00:39
Because you want to make sure your code adheres to a specific style to avoid all the mess and problems that you get by working in different styles. And it doesn't mean that you implement
00:50
different functionality. It just means that specifically, the style of your code is the same across different developers. And it does help for just yourself as well,
01:01
just to keep your code nice and clean and follow a specific style. In this case, like I said, we're using PSR 2. Now we're going to be using PHP code sniffer on the command
01:12
line to detect any problems with our coding style. You could also then go ahead and run this on some kind of build process, which we're not going to cover in this video.
01:23
OK, so let's go ahead and start to create just a file or something like that. And then we'll go ahead and run our PHP code sniffer once we get it installed as a dependency.
01:35
So you are going to need to install Composer as well, which is a PHP dependency manager. And just head over to getcomposer.org to grab this. So inside of my project then, let's just
01:47
create a source directory maybe. This is just going to be where we store all of our controllers, things like that, anything else. So I'm going to create a directory called Controllers
01:58
here. And I'm going to go ahead and create a new file down here. Let's call this homecontroller.php. So let's define this home controller then.
02:11
And let's create a method in here called index. I'm purposely going to leave this bracket on this line. And I'm going to go ahead and create an if statement. Again, this doesn't follow PSR 2,
02:24
so we'll see the problems that we get from this. We'll also notice that there's something wrong with the fact I'm tabbing as well. We'll look into that in a moment as well.
02:33
So in here then, I'm just going to say return it is true. And then down here, I'm just going to return 1. Something really silly, just a very basic example. So now what I want to do is I want to include PHP code
02:49
sniffer as a development dependency. So inside of my project directory, I'm going to create a new file. And I'm going to call this composer.json.
02:59
Now inside of composer.json, with Composer, you can define things like requirements, development requirements, auto-loading. In this case though, we're just going
03:08
to look at development requirements. So it's require-dev. And let's just fix that up. And in here then, we want to pull in this as a dependency.
03:21
So if we scroll down inside of the GitHub repo, you can see that the dependency is squislabs.phpcodesniffer. And we're going to pull in 1.star. So we can just go ahead and copy and paste this.
03:33
That's fine. OK, so now that that's there, we can go ahead and as long as we're in the directory of our composer file, we can install these dependencies.
03:46
This won't take a moment at all because we've only got one very small one. There we go. OK, so now that's inside of or that's
03:53
created a vendor directory with a bin directory with phpcodesniffer inside of it. So we can run this on the command line. So let's now run this and check out our source directory
04:06
and everything within it and define the particular coding style that we want to follow. So we run vendor bin phpcodesniffer. Now we can define a standard here.
04:19
So I'm going to say standard flag and I'm going to say psr2. Then I'm going to choose the directory that I want to check. In this case, it's source. So here you can see that we've got 15 errors.
04:32
Now most of these errors are because we have line indentations incorrect. In this case, it's expecting at least four spaces because we're currently using tabs
04:43
and we need to be using spaces. Now I also have a plug-in installed in my text editor called editor config. And we have a video that covers this.
04:54
So go ahead and check out editor config if you haven't already. In this case, that gives us a few bits here in an example file. We're going to go ahead and create this editor config file. So if you haven't downloaded this, go ahead and do so now.
05:07
And then you can follow along and build up your editor config file. The great thing about this is it works cross editor. And of course, you can include this
05:16
in some kind of GitHub repo for your project. So everyone can benefit from it. And if you don't want to do that, you can just add it to your gitignore file.
05:25
So inside of my text editor then, I'm just going to go ahead and run up package manager. And I'm going to list the packages I have installed. In this case, I have editor config installed,
05:35
which helps developers maintain a consistent coding style. If you don't have this installed, make sure you have package manager installed for the sublime.
05:44
You can go ahead and install a package. And you can go ahead and choose from the list. So just type in editor config. So inside of our main directory then, we'll create a new file.
05:55
And we're going to call this .editorconfig. And we're going to go ahead and use .. So from here then, I want to define that the root is true. Now, I also want to change the indent style to spaces.
06:09
And I want to change the indent size to 4. So I'm going to go ahead and paste this in here. And up here, we'll choose that we want this to apply to all files.
06:19
And up here, we'll say root is true. Now, you don't have to use editor config. By all means, change the settings within your text editor or IDE to have
06:29
a indent style of space and an indent size of 4. Or of course, if you're following any other standard, this will vary. So just set things up the way you want it.
06:39
I just find this a really easy way to do this across different devices I might be working on. So now that we've got this in place, when we head over to our home controllers
06:52
and we save this out, let's just close that and open it back up. And I'm going to go ahead and re-indent all of this. There is a shortcut to this in Sublime. But I'm not going to be using that, just in case
07:03
you're using another editor. So in here then, if we re-indent all of this, you'll notice that we're actually now using spaces rather than tabs.
07:13
Again, there are shortcuts to do this. Let's run the Codesniffer again with the PSR2 standard. And you'll notice that that's significantly brought down the error count to four errors.
07:23
But we still have a couple of problems here. And again, I did these on purpose so we can go ahead and fix them up. So the PSR2 standard follows the standard
07:32
that each class must be in a namespace of at least one level. In this case, this is going to be namespacing your classes so they have some kind of vendor namespace.
07:45
And then we can go ahead and define that we want them controllers, extractors, whatever else. It really depends. So up here, we're going to then define our namespace.
07:56
Hopefully you've worked with namespaces before and this makes total sense. So I'm going to namespace this under something called some project.
08:05
And then I'm going to subnamespace this under controllers. So home controller is now part of some project controller. So now, you'll notice that we bring this down to three errors.
08:18
But we have a couple more things we need to fix up. Opening brace should be on a new line. This is on line five. Here, like I said earlier, I did this on purpose.
08:27
That should be down there. And also, we've got an expected if space, blah, blah, blah, new line. And what we've found is if with no space.
08:38
So we need a space in between here to adhere to PSR2. We've also got expected one new line at the end of file. So in this case, we can use editor config to our advantage to automatically add in a final last line.
08:55
So here, we say end of line. And we define this as LF, so Unix style new line. And then we say insert final new line true. So we can just copy and paste this, go over here,
09:06
and add it to our editor config file, paste that in. Now, when we're over in home controller and we save, notice that it'll add a final new line. When we close it and open it back up, there we go.
09:20
So that's added a final new line in there. So now, when we run this, we get no output. And therefore, we have no errors. So this has been a very basic introduction
09:30
to how you might use PHP code sniffer to sniff the files within a particular directory and make them adhere to a specific standard. Of course, you're likely going to be using another standard.
09:43
It's entirely up to you and your team. But in this case, you now know how to run a command to check your code just to keep it nice and clean.
1 episode 9 mins

Overview

Ever wanted to run a command to check your code against a particular standard? PHP CodeSniffer lets you do that right from the command line!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!