Code Navigation Shortcuts0:00
Now, before moving on to some JavaScript-specific optimizations for this editor, let's do a final PHP recap and review. So I have this form series at Laracasts. Why don't we use that as an example? Now, I'm going to use Command-P to visit the Thread class. And now, because we have the IntelliFence package installed, I can, of course, Command-Click to any of these underlying traits, which is great. Or I'll hit Command-P twice to go back to the previous class. I can alternatively use Command-R, which was the shortcut we assigned, to browse the symbols in this file. So, for example, if I want to go to AddReply, but now what if we want to visit a different class, like Reply? Alright, well, if I run that, you'll notice that it takes over the current window. And maybe this is what you want, but sometimes you'll finish here, you'll hit Command-W to close it, and you think,
Open Files in New Window0:44
you'll notice that it takes over the current window. And maybe this is what you want, but sometimes you'll finish here, you'll hit Command-W to close it, and you think, oh, I want to get back to Thread. So you have a couple choices here. One, if you prefer that functionality, then great. But if not, I'll hit Command-Comma to go to Settings, and let's search for Window. Let's see, here it is. Open file as a new window. Yes. So I'm going to turn that on, save, and close it out. Now, if we visit the Reply class, you'll see it loads a new window, which might be what you prefer. Anyways, back to Thread. You'll see we have this class called ThreadFilters. I'll hit Command-G to find where we use it in the file. There it is. But imagine we were in the process of constructing this, and we hadn't yet imported it. Okay, let's go back and remove that, and then I'll hit Control-Minus to go back to where we just were.
Auto-Importing Classes1:26
But imagine we were in the process of constructing this, and we hadn't yet imported it. Okay, let's go back and remove that, and then I'll hit Control-Minus to go back to where we just were. And by the way, that's a good one to know. Control-Minus will always take you back to your previous edit point. Anyways, we're going to remove this entirely and start from scratch. So I will type ThreadFilters, and if I hit Control-Space, you'll see this option to import it. Let's run it. Now, it will be added to the top of the file. What else? Well, let's go to ReplyController. You'll see this update method here, where we authorize the request, we validate it, and then we update the body. Now, in Laravel 5.5, as an aside, it turns out that the validate method will return the validated attributes, which means if you want, you can inline it like so. Let's give this a shot, but I'm going to undo it to what we had before just for a
Refactor with Test Support2:10
the validate method will return the validated attributes, which means if you want, you can inline it like so. Let's give this a shot, but I'm going to undo it to what we had before just for a moment. I'd like to have my test to back up this refactor. So I believe we have a class called ParticipateInThreadsTest, and you'll see this method authorizeUsersCanUpdateReplies. Now, of course, if we already know the method, we could instead use a global search. We have that bound to Shift-Command-R, and I'll paste that in. There it is, and now we can go there directly from anywhere. So because of the BetterPHPUnit plugin, I can hit Command-T to run the test. So we see here, if you're signed in and we have a reply, and we hit the endpoint to update the reply, well then, everything should work correctly. So let's look in the Replies table and make sure we see a body column that has this updated reply. So that means if we were to,
to update the reply, well then, everything should work correctly. So let's look in the Replies table and make sure we see a body column that has this updated reply. So that means if we were to, for example, just use an empty array and rerun the test with Shift-Command-T, of course it's going to fail. Okay, so now we have a test backing up our change. Let's do this. Validated. Like so. Shift-Command-T to rerun the previous test, and now we're back to green. While we're here though, why don't we inline the entire thing? Like so. Run it again. We're still at green, so that's a good refactor. So let's close that out. Now I want to take a look at my source control. So of course I can go to View, SCM, or it's bound to Control-Shift-G by default. But now here I can see we have a number of changes. We have the changes from the last episode in the forum series that I haven't yet committed, but then we also have our little change here.
Stage and Commit Changes3:44
But now here I can see we have a number of changes. We have the changes from the last episode in the forum series that I haven't yet committed, but then we also have our little change here. Now if I click on it, I can see a side-by-side where once again we can switch to inline mode, and we can see, okay, we just inlined that request validation. So why don't we stage this alone, and we'll say inline request validation, and I'll hit Command-Enter to run that. Great. So now if we do a quick git log, we can see that commit there. What else do we have? So we have the changes from the last episode, and we could quickly review those if we wanted, but let's imagine it's right. We'll stage everything and then commit with episode whatever it happens to be, 98. But I'm going to leave that off for the time being. Now let's do one more thing. I'm going to open up the terminal, and we could do this from the UI, but I'm also very comfortable in the terminal,
Debug Validated Attributes4:32
98. But I'm going to leave that off for the time being. Now let's do one more thing. I'm going to open up the terminal, and we could do this from the UI, but I'm also very comfortable in the terminal, so you don't have to sacrifice one for the other. Let's go back to what we had before. So I'm going to do a git reset --soft. Go back to that previous commit, but I don't want to lose any of the changes since then. So if I come back, once again, RepliesController reflects our change. But now I don't want to get out of sync with the form series at Laravel itself, so I don't actually want to commit this change. Instead, let's unstage, and in fact we will discard the changes entirely. Great. So we'll close that out, and we're now back to what we had before. Now let's do some debugging. So you may not know that the validate method actually returns the validated attributes. Why don't we try that out? So we'll call validated, and then I'm going to add a breakpoint.
So you may not know that the validate method actually returns the validated attributes. Why don't we try that out? So we'll call validated, and then I'm going to add a breakpoint right here. I'll then go to view debug or hit Shift Command D, and let's start up the debugger. And then finally I'll trigger my test, which will hit this method. So we can't do it, FYI, we can't do it using the phpunit tool. It's not going to trigger. So we'll have to do it manually. Now I can't remember what the test method name was, so I will hit Command P a couple times, and let's go back to that previous file. There it is. Okay, phpunit, filter it down to that method alone, and there we go. We've hidden our breakpoint. So at this point, again, validated is uninitialized. So if you ever set a breakpoint right on the line, well, it's stopped there. It hasn't yet triggered it. So let's step over, and now we can see here is what validated
Auto-Fix Coding Standards5:58
validated is uninitialized. So if you ever set a breakpoint right on the line, well, it's stopped there. It hasn't yet triggered it. So let's step over, and now we can see here is what validated is equal to. And sure enough, it does contain the validated attributes. In this case, even change foo. So we can close that out. We're done debugging, and we can successfully perform our refactor if we needed to, but we're not doing that in this case. Finally, I'm going to hit shift command e to go back to my tree view, or command 1 to close that out. To close the terminal, control tilde. And yeah, let's go to thread, and we're going to imagine that we're not following psr2. Okay, so we're going to make a few changes here. We're going to have foo. That's an old array syntax. And then maybe at the very top, we're pulling in something that this class actually never uses. How about GuzzleHttp\Request class? Okay, well, because we have the phpcs fixer.
syntax. And then maybe at the very top, we're pulling in something that this class actually never uses. How about Guzzle's Request class? Okay, well, because we have the phpcs fixer extension installed, and we've instructed it to activate on save, well, that means if I hit command s, instantly it detected we don't use that import, so we remove it. And then if we scroll down, it's using the short array syntax. And of course, you can see it's adjusted the braces to follow PSR-2. So once again, I'll hit control shift g to bring up my changes here. And yeah, this is all for demo, so I will discard that. Great. Finally, let's do one more. I'm going to go back to that addReply method. So I will hit shift command r and look for addReply. Or again, notice this is still the command palette. What designates a symbol search is the pound sign. So that means if you want, you can do shift command p and then just start with a pound.
Find References and Peek7:36
notice this is still the command palette. What designates a symbol search is the pound sign. So that means if you want, you can do shift + command + p and then just start with a pound. Add reply. There it is. So now I'm not sure if my project is still using this method. Maybe I can get rid of it entirely. Now we can check this in two ways. One, delete it, run your tests, see if everything still works. If you have good test coverage, that should be enough. But why don't we just use the UI? I will right click, but you'll also remember that we assigned this to option + enter or option + return. So now let's look for all references to this method. All right, we can see, oh, in RepliesController, right here we're calling this method, so I can't delete it after all. That's good to know. So we'll close that out. On the flip side, if I were in RepliesController to begin with, and I will hit command + F to look for
so I can't delete it after all. That's good to know. So we'll close that out. On the flip side, if I were in RepliesController to begin with, and I will hit command F to look for addReply. There it is. If I'm building out this method, sometimes it's useful to peek behind the scenes. So yes, I could command click to switch over there. Or if we go back, we could once again bring up the context menu and let's just peek at the underlying definition. Okay, it accepts a reply, it creates it behind the scenes, it fires an event, and then it returns the reply. That's what I needed to know. All right, so hopefully you're feeling pretty comfortable at this point. In the next episode, let's switch over and review some JavaScript optimizations.
