Refactor Into Method Workflow 0:00Alright, so check this out. You've probably written code just like this multiple times. And when we have that throughout our controllers, it's fine, but it can get a little muddy. Maybe it would be nicer if we could extract it to our base controller. So what kind of workflow might we use in PHPStorm? Well, we will take two passes. The first time, I will do it quickly, just to show you the workflow, and the second time, we'll slow down a bit to go over everything. So what I might do is hit Ctrl-T to refactor to a method. We will call this RedirectBackWithErrors, alright? Push Method to Base 0:28So what I might do is hit Ctrl-T to refactor to a method. We will call this RedirectBackWithErrors, alright? And if I decide that I do want this in the base controller, once again Ctrl-T, and I will push that up and do the refactor, and I'm done. Now I can simply copy and paste this and finish up. Pretty darn quick, right? Also if I check out the base controller, sure enough, here's our method along with the doc blocks. So that's pretty nice, right? Alright, so let's take a second pass and do it a little bit slower this time. Second Pass, Step-by-Step 0:56So that's pretty nice, right? Alright, so let's take a second pass and do it a little bit slower this time. I will hit Cmd-Z just a handful of times to undo everything. Okay, so the first step is, one, you could select everything and hit Ctrl-T, but very much like copying a full line by simply placing the cursor on the line and pressing Cmd-Z, the same is going to be true with a refactor in some cases. In other cases, you will need to be more specific with what you select. Anyhow, Ctrl-T to perform a refactor, I will choose method or type M-E-T is what I usually do. Next, I give it a name, redirect back with errors.do. Next, I give it a name, redirect back with errors. If you need to set the visibility, you can. Let's refactor. Okay, but now I want to pull this method up, and when we say pull up, think of that as saying I want to move this method to a parent class. And by the way, I access that class by pressing Cmd-Click, alright? So let's try it. Ctrl-T, and usually I just type up. There we go.Ctrl-T, and usually I just type up. There we go. Now notice that it's smart enough to detect that we have a private method, and it's going to let you know, hey, if you follow through with this refactor, I'm going to change it to protected. So do the refactor, and we're done. Finally, we just have to update this reference right here. So Cmd-C to copy the whole line, Cmd-V, and then finally Cmd-X, and we're done. So notice how phpStorm makes all this refactoring so incredibly simple, and that's vital. From my experiences, when you use an editor like Brackets or Sublime Text, even though Why IDE Refactoring Matters 2:21So notice how phpStorm makes all this refactoring so incredibly simple, and that's vital. From my experiences, when you use an editor like Brackets or Sublime Text, even though they're great, they don't make these sorts of refactorings easy. So as a result, many times you don't do the refactor just out of laziness. But in phpStorm, these sorts of tasks couldn't be simpler. So in a weird way, using this IDE almost encourages and promotes clean code.