1 00:00:00,660 --> 00:00:03,620 This is a more controversial recommendation than PSL1, 2 00:00:03,620 --> 00:00:04,800 which is widely used and adopted. 3 00:00:04,800 --> 00:00:07,510 PSL2 is used widely too, but 4 00:00:07,510 --> 00:00:10,230 has a few parts that some developers are happier about more than others. 5 00:00:11,240 --> 00:00:14,160 The reasoning here, is that PSL2 is a style guide and 6 00:00:14,160 --> 00:00:16,900 each developer has their own unique preferences when it comes to style. 7 00:00:17,940 --> 00:00:20,400 Regardless of the personal preferences of a developer, 8 00:00:20,400 --> 00:00:23,160 using PSL2 has many benefits for your code. 9 00:00:23,160 --> 00:00:26,650 And as everyone conformed to a single code style guide, which helps avoid all sorts 10 00:00:26,650 --> 00:00:31,040 of obscure problems with version control and co-contribution from third parties. 11 00:00:31,040 --> 00:00:34,170 It's also a great set of common rules to follow when you work on a team. 12 00:00:34,170 --> 00:00:37,460 To stop any potential arguments that might pop up about where a bracket should go. 13 00:00:37,460 --> 00:00:38,820 You can just do what PSL 2 does. 14 00:00:40,420 --> 00:00:42,280 I'm gonna take you through the rules in the overview. 15 00:00:42,280 --> 00:00:44,630 One at a time and explain what they mean. 16 00:00:44,630 --> 00:00:45,730 Code must follow PSR1. 17 00:00:45,730 --> 00:00:50,730 To be PSR2 compliant, you also have to follow the rules in PSR1. 18 00:00:50,730 --> 00:00:51,920 PSR2 adds on top of these rules. 19 00:00:53,430 --> 00:00:56,345 Code must use four spaces for indenting not tabs. 20 00:00:56,345 --> 00:00:58,880 This seems counter-intuitive to many. 21 00:00:58,880 --> 00:01:01,440 The tabs versus spaces wars have been going on forever and 22 00:01:01,440 --> 00:01:04,780 will never end, but the important thing is consistency. 23 00:01:04,780 --> 00:01:08,793 The more projects use the same thing, the less IDE config switching you need to do. 24 00:01:08,793 --> 00:01:11,880 PSR2 pick spaces, so that's what we'll do. 25 00:01:11,880 --> 00:01:14,860 Any good editor, can be set to enter up to four spaces when you hit 26 00:01:14,860 --> 00:01:17,280 the Tab key on your keyboard, so this isn't a problem. 27 00:01:18,890 --> 00:01:21,710 There must not be a hard limit on line length. 28 00:01:21,710 --> 00:01:27,380 The soft limit must be 120 characters, and lines should be 80 characters or less. 29 00:01:27,380 --> 00:01:30,750 This just means it would be cool if you kept your code at less than 80 characters, 30 00:01:30,750 --> 00:01:32,450 but that's not always possible. 31 00:01:32,450 --> 00:01:35,180 It probably shouldn't be more than 120 either but if it is, 32 00:01:35,180 --> 00:01:36,460 just give a warning not an error. 33 00:01:37,850 --> 00:01:41,450 There must be one blank line after the name-space declaration. 34 00:01:41,450 --> 00:01:45,390 And there must be one blank line after the block of use declarations. 35 00:01:45,390 --> 00:01:46,780 This just helps things look pretty, 36 00:01:46,780 --> 00:01:49,160 by adding some white space around the namespace declaration. 37 00:01:50,710 --> 00:01:53,430 Opening braces for classes must go on the next line, and 38 00:01:53,430 --> 00:01:56,680 closing braces must go on the next line after the body. 39 00:01:56,680 --> 00:01:59,290 That just means put curly brackets for classes on their own lines. 40 00:02:00,420 --> 00:02:03,430 Opening braces for the methods, must go on the next line, and 41 00:02:03,430 --> 00:02:06,660 closing braces, must go on the next line after the body. 42 00:02:06,660 --> 00:02:08,450 Put curly brackets for methods on their own lines too. 43 00:02:09,870 --> 00:02:12,860 Visibility must be declared on all properties and methods. 44 00:02:12,860 --> 00:02:16,100 Abstract and final, must be declared before the visibility, and 45 00:02:16,100 --> 00:02:18,320 static must be declared after the visibility. 46 00:02:19,410 --> 00:02:20,810 This sounds a little confusing. 47 00:02:20,810 --> 00:02:23,710 But if you're creating an abstract or a final class which is public, 48 00:02:23,710 --> 00:02:27,130 the word abstract or final, should come before public. 49 00:02:27,130 --> 00:02:28,620 If you're using static classes, and 50 00:02:28,620 --> 00:02:31,640 that method is public, then you should write public before you write static. 51 00:02:31,640 --> 00:02:35,950 You might not have learned about abstract final or static classes yet. 52 00:02:35,950 --> 00:02:37,430 These are all fairly advanced features. 53 00:02:37,430 --> 00:02:39,560 So if you're unsure what they are, just ignore this rule for now. 54 00:02:40,960 --> 00:02:44,160 Control structure keywords must have one space after them, and method and 55 00:02:44,160 --> 00:02:45,920 function calls must not. 56 00:02:45,920 --> 00:02:51,660 Control structure keywords are things like if, else, switch, do, while and for each. 57 00:02:51,660 --> 00:02:54,550 You need a space after these words, and before the opening bracket. 58 00:02:55,830 --> 00:02:59,310 Opening braces for control structures, must go on the same line. 59 00:02:59,310 --> 00:03:02,910 And closing braces, must go on the next line after the body. 60 00:03:02,910 --> 00:03:06,690 This means things like if, for each and while, need to have their curly bracket on 61 00:03:06,690 --> 00:03:09,220 the same line as the keyword, instead of dropping it down a line. 62 00:03:10,560 --> 00:03:11,490 Opening parentheses for 63 00:03:11,490 --> 00:03:14,210 control structures, must not have a space after them. 64 00:03:14,210 --> 00:03:18,620 And closing parentheses for control structures, must not have a space before. 65 00:03:18,620 --> 00:03:20,980 That means no white space after the opening bracket, and 66 00:03:20,980 --> 00:03:22,670 no white space before the closing brace. 67 00:03:23,780 --> 00:03:26,040 It can be quite hard to understand what all of this means. 68 00:03:26,040 --> 00:03:29,650 To help us, we can install a tool called PHP_CodeSniffer. 69 00:03:29,650 --> 00:03:32,560 This will allow you to test your code matches against the standard. 70 00:03:32,560 --> 00:03:34,960 Let's hop over to workspaces to see how CodeSniffer works. 71 00:03:36,500 --> 00:03:38,570 If we want to start working with PHP_CodeSniffer, 72 00:03:38,570 --> 00:03:40,660 the first thing we should do is install it. 73 00:03:40,660 --> 00:03:47,440 As with many things, we can use the console on here and type, composer require. 74 00:03:47,440 --> 00:03:49,740 The name of the vendor is Squizlabs. 75 00:03:50,970 --> 00:03:54,020 The name of the package, PHP_CodeSniffer. 76 00:03:54,020 --> 00:03:56,460 And once again, this is a dev dependency. 77 00:03:56,460 --> 00:03:58,470 We are only using this for development and not production. 78 00:03:59,510 --> 00:04:01,470 It's gonna ask us to pick a version constraint. 79 00:04:01,470 --> 00:04:02,480 Once again, we don't really care. 80 00:04:02,480 --> 00:04:04,870 So there you can see Composer doing the work. 81 00:04:04,870 --> 00:04:07,200 It's installed CodeSniffer, and now it's there. 82 00:04:08,260 --> 00:04:12,100 Now, just like with PHPUnit, we can run vendor. 83 00:04:13,530 --> 00:04:16,180 Bin, this is where our code lives. 84 00:04:16,180 --> 00:04:20,950 And phpcs, is the executable for CodeSniffer. 85 00:04:20,950 --> 00:04:23,750 Then we pass src as an argument, 86 00:04:23,750 --> 00:04:25,760 as that is the only directory we want to sniff right now. 87 00:04:25,760 --> 00:04:28,510 We can leave our tests and vendor folders alone. 88 00:04:28,510 --> 00:04:31,610 Next, we have to specify the standard option. 89 00:04:33,020 --> 00:04:34,440 Two hyphens. 90 00:04:34,440 --> 00:04:36,910 And we want to pass PSR2 as the value to that, 91 00:04:36,910 --> 00:04:40,360 because that's the name of our standard that we want to test against. 92 00:04:40,360 --> 00:04:43,840 If you run this, CodeSniffer should tell us anything that it finds that's 93 00:04:43,840 --> 00:04:48,370 not PSR2 complying in any of the PHP files that exist inside the source folder. 94 00:04:49,380 --> 00:04:50,765 Lets have a look at what it's found. 95 00:04:50,765 --> 00:04:58,935 Okay, here we go, it's found errors inside, src example.php. 96 00:04:58,935 --> 00:05:00,740 Cuz it has, that's the only file that we have. 97 00:05:01,970 --> 00:05:03,840 And there's quite a few errors here. 98 00:05:03,840 --> 00:05:05,920 These numbers here, are line numbers. 99 00:05:05,920 --> 00:05:07,830 This is the severity of the error. 100 00:05:07,830 --> 00:05:09,730 Warnings you can kind of ignore. 101 00:05:09,730 --> 00:05:10,980 Errors are a problem, and 102 00:05:10,980 --> 00:05:14,120 it has a human readable message explaining what's actually going wrong. 103 00:05:14,120 --> 00:05:17,450 Let's have a look at example.php and try and fix it. 104 00:05:17,450 --> 00:05:21,010 So, there must be one blank line after the namespace declaration. 105 00:05:21,010 --> 00:05:22,031 That makes sense. 106 00:05:22,031 --> 00:05:24,404 It also probably shouldn't be jammed up there either, 107 00:05:24,404 --> 00:05:26,500 must be one blank line after the you statement. 108 00:05:26,500 --> 00:05:27,830 Zero found. 109 00:05:27,830 --> 00:05:28,570 Okay. 110 00:05:28,570 --> 00:05:29,680 We can put a line in. 111 00:05:29,680 --> 00:05:30,210 Nice and easy. 112 00:05:31,220 --> 00:05:33,870 Let's try and shrink this list of errors down a little bit. 113 00:05:37,510 --> 00:05:38,590 Okay. What happening now? 114 00:05:38,590 --> 00:05:40,410 Line six, that's here. 115 00:05:40,410 --> 00:05:44,780 Opening brace of a class must be on the line after definition. 116 00:05:44,780 --> 00:05:47,170 Oh, okay. Well the definition line is right here, 117 00:05:47,170 --> 00:05:49,575 and then we have a space and a bracket. 118 00:05:49,575 --> 00:05:52,720 PSR2 says that it should be on a line by itself. 119 00:05:52,720 --> 00:05:53,220 Run again. 120 00:05:54,330 --> 00:05:55,490 Excellent. We're starting to get rid of some of 121 00:05:55,490 --> 00:05:56,670 these errors now. 122 00:05:56,670 --> 00:05:58,920 Class constants must be uppercased. 123 00:05:58,920 --> 00:06:01,040 Expected FOO, but found lowercase foo. 124 00:06:02,310 --> 00:06:03,950 Easy fix. 125 00:06:03,950 --> 00:06:07,380 Don't forget to change any references to this constant as well. 126 00:06:07,380 --> 00:06:09,850 Expected function abc, but 127 00:06:09,850 --> 00:06:14,530 found function abc with a space in between the first parenthesis. 128 00:06:14,530 --> 00:06:15,940 So we don't have a function called abc. 129 00:06:15,940 --> 00:06:17,570 This is just a template error. 130 00:06:17,570 --> 00:06:19,040 What they're really trying to say is, 131 00:06:19,040 --> 00:06:22,230 that on line ten which is right here, there shouldn't be a space here. 132 00:06:22,230 --> 00:06:23,110 So you can get rid of that. 133 00:06:25,190 --> 00:06:26,300 Get rid of a few more of these errors. 134 00:06:26,300 --> 00:06:28,530 Starting to get much more manageable now. 135 00:06:31,580 --> 00:06:34,550 So line ten, still problems with this line. 136 00:06:34,550 --> 00:06:35,750 What's wrong? 137 00:06:35,750 --> 00:06:41,790 Well, we need to put the bracket, opening brace it calls it, down here. 138 00:06:41,790 --> 00:06:43,900 Line 14 is our next error. 139 00:06:43,900 --> 00:06:47,450 The line numbers have changed, so let's update this. 140 00:06:47,450 --> 00:06:49,740 Okay 15, there's a lot of problems on line 15. 141 00:06:49,740 --> 00:06:52,680 We have four different errors with this one piece of code. 142 00:06:52,680 --> 00:06:53,240 So let's take a look. 143 00:06:53,240 --> 00:06:58,310 The static declaration must come after the visibility declaration. 144 00:06:58,310 --> 00:07:01,280 Visibility declaration is public, private or protected. 145 00:07:01,280 --> 00:07:05,570 So it means static, needs to go, there. 146 00:07:05,570 --> 00:07:09,160 Once again, we have the same message, function abc bracket. 147 00:07:09,160 --> 00:07:11,350 Not found, found abc space bracket. 148 00:07:11,350 --> 00:07:14,773 So that's easily fixed. 149 00:07:14,773 --> 00:07:17,600 Okay, getting there. 150 00:07:17,600 --> 00:07:19,370 15, still more problems with 15. 151 00:07:19,370 --> 00:07:21,380 This is a very controversial line. 152 00:07:21,380 --> 00:07:24,500 Expected zero spaces between the opening bracket. 153 00:07:24,500 --> 00:07:26,760 An argument bar, one found. 154 00:07:26,760 --> 00:07:30,950 Expected zero spaces between argument bar and closing bracket, one found. 155 00:07:30,950 --> 00:07:35,450 That means, it expected zero spaces here but found one, delete that space. 156 00:07:36,470 --> 00:07:41,330 It expected, zero spaces there, but found one, so delete that. 157 00:07:41,330 --> 00:07:44,930 And if we look down here it says, opening brace should be on a new line. 158 00:07:44,930 --> 00:07:46,660 So, that's simple. 159 00:07:46,660 --> 00:07:47,410 Punt that down a line. 160 00:07:47,410 --> 00:07:49,160 Go to the white space. 161 00:07:50,630 --> 00:07:51,370 Let's run this again. 162 00:07:53,430 --> 00:07:53,990 Okay. 163 00:07:53,990 --> 00:07:56,030 White space found at end of line. 164 00:07:57,030 --> 00:07:59,420 There's a few extra spaces there. 165 00:07:59,420 --> 00:08:00,610 Get rid of that. 166 00:08:00,610 --> 00:08:03,260 Inline control structures are not allowed. 167 00:08:03,260 --> 00:08:07,680 Now if you're familiar, control structure is something like if, do, while. 168 00:08:07,680 --> 00:08:11,300 And, inline means that it's all happening on the same line. 169 00:08:11,300 --> 00:08:14,580 PHP allows you to do an inline control statement if there's only one 170 00:08:14,580 --> 00:08:15,940 statement happening. 171 00:08:15,940 --> 00:08:21,300 The problem is, if somebody does something like, then that 172 00:08:21,300 --> 00:08:22,830 doesn't happen inside the IF. 173 00:08:22,830 --> 00:08:26,440 That happens outside of the IF, which is very confusing. 174 00:08:26,440 --> 00:08:31,621 So, to avoid any potential confusion, you just don't use inline control structures 175 00:08:31,621 --> 00:08:35,852 [BLANK_AUDIO] 176 00:08:35,852 --> 00:08:38,260 Okay. There's still problems with line 17. 177 00:08:38,260 --> 00:08:39,730 Expected zero spaces and found one. 178 00:08:39,730 --> 00:08:41,810 Get rid of that, and that. 179 00:08:43,570 --> 00:08:44,690 Keep on running CodeSniffer. 180 00:08:44,690 --> 00:08:46,760 And so we haven't got any errors left. 181 00:08:46,760 --> 00:08:49,570 True, false, and no, must be lower cased. 182 00:08:49,570 --> 00:08:52,790 Expected false but found capital F false. 183 00:08:52,790 --> 00:08:54,700 So, someone used a weird style there. 184 00:08:54,700 --> 00:08:55,210 Let's change that. 185 00:08:55,210 --> 00:08:59,140 And you'll see that now PHP recognizes that as a, as a keyword instead. 186 00:09:00,260 --> 00:09:02,780 On line, 18 we can see that it's not been indented correctly, 187 00:09:02,780 --> 00:09:04,730 because there is only two spaces. 188 00:09:04,730 --> 00:09:06,320 They should be four. 189 00:09:06,320 --> 00:09:06,980 Line 21. 190 00:09:06,980 --> 00:09:10,140 Gotta get rid of these spaces. 191 00:09:10,140 --> 00:09:12,970 Also on 21 true, false and null must be lowercased. 192 00:09:12,970 --> 00:09:15,490 Again, someone's used caps lock instead, so we can change that. 193 00:09:17,090 --> 00:09:18,640 Let's run CodeSniffer again. 194 00:09:19,650 --> 00:09:20,750 It all fits on one screen now. 195 00:09:20,750 --> 00:09:22,170 We're getting there. 196 00:09:22,170 --> 00:09:25,730 Expected one space after the function key word. 197 00:09:25,730 --> 00:09:26,850 Which line is this? 198 00:09:26,850 --> 00:09:27,710 30. 199 00:09:27,710 --> 00:09:29,020 Okay, so 200 00:09:29,020 --> 00:09:33,750 when using a function when using an anonymous function, we need to put a space in. 201 00:09:33,750 --> 00:09:37,580 Also on 30, closing breaks must be on a line by itself. 202 00:09:37,580 --> 00:09:38,080 Okay. 203 00:09:39,080 --> 00:09:40,240 One new line at the end of a file. 204 00:09:40,240 --> 00:09:42,440 Oh, we'll get to that in a second. 205 00:09:42,440 --> 00:09:45,210 Each PHP statement must be in a line by itself. 206 00:09:45,210 --> 00:09:46,110 We've still got a problem there. 207 00:09:48,810 --> 00:09:50,530 Very close to having this complete now. 208 00:09:50,530 --> 00:09:53,450 Expected one new line at the end of the file, found zero. 209 00:09:53,450 --> 00:09:54,420 Okay? 210 00:09:54,420 --> 00:09:55,080 There's an empty line. 211 00:09:56,100 --> 00:09:59,490 The closing brace for the class must go on the next line after the body. 212 00:09:59,490 --> 00:10:02,680 What that means, is that the closing bracket for 213 00:10:02,680 --> 00:10:05,560 the class, this, must be at the end of the body. 214 00:10:05,560 --> 00:10:06,510 That's here. 215 00:10:06,510 --> 00:10:08,370 So, delete that line. 216 00:10:10,210 --> 00:10:10,850 Perfect! 217 00:10:10,850 --> 00:10:11,510 All the errors are gone. 218 00:10:13,470 --> 00:10:16,460 Many IDs and code editors will either have code sniffer built in. 219 00:10:16,460 --> 00:10:18,240 Or available as a plug-in. 220 00:10:18,240 --> 00:10:21,820 Using interactive tools like CodeSniffer, should really help you learn about PSR2. 221 00:10:21,820 --> 00:10:25,380 You don't always need to follow PSR2 in your projects. 222 00:10:25,380 --> 00:10:27,710 But it's slowly becoming far more common for people to do so.