Introducing Refactoring Demo0:00
Okay, now, easily one of the most compelling features of phpStorm is, of course, its refactoring capabilities. So why don't we have a look at that now? And to illustrate this, I'm going to use a tweaked version of a Laracast-specific class called AIChat. Okay, now, don't worry about the code too much, but it does what you would expect it to. It's a way to prepare a conversation and send it over to OpenAI, fetch a response, remember it. Yeah, exactly what you would expect.
Rendering Doc Comments0:32
it. Yeah, exactly what you would expect. All right, so the first thing I'm going to do here is, well, have a look at the doc blocks. We can actually make this a little bit prettier by hovering to the left, right-clicking, and choosing Render All Doc Comments. Okay, so now it's a slightly different presentation. It might take a little getting used to, but these days, I very much prefer it. Okay, very good. So now we're going to scroll down to this prompt method. We have a bunch going on here, and I think we can refactor it a little bit.
Reviewing Prompt Method0:57
So now we're going to scroll down to this prompt method. We have a bunch going on here, and I think we can refactor it a little bit. So quickly, let's do the five-second pass to see what it does. Now, if we call this prompt method, we can pass through a message like, hi, how are you? When we do that, we record that prompt within a messages array, and then we pass that array to OpenAI. And yeah, if you're curious, when you're actually performing a chat with OpenAI, it doesn't remember previous messages magically. Instead, what you're doing is you are personally tracking it within an array like this, and then when you send a new message, you're actually sending through the entire conversation, and
Extracting Write Method2:00
Okay, so yeah, I think we can make some slight tweaks here. First up, this is fine initially, but we might run into situations where, for example, we want to write a message without immediately sending it to OpenAI. Okay, so it sounds like maybe this right here represents writing a message. So, how could we do this? Well, again, traditionally, with a simple code editor, we might extract a method, like a public function, write, and then I could grab all of this, cut it, and paste it in. We will then need to accept a prompt, and yeah, if we want, we could then return the current instance, and we can add the return type like so. Okay, so now up here, I can say this, write, and send through the prompt.
Using Extract Method Refactor2:41
current instance, and we can add the return type like so. Okay, so now up here, I can say this, write, and send through the prompt. And yeah, that works. I've done that a million times, but of course, we are using an IDE that is much more powerful, so why don't we just let it do the work? All right, I will hold down Command-Z to undo that. There we go. All right, this time, I will select the code, and I'll show you a few ways to do this. First, I can right-click, scroll down to Refactor, and then I can choose Extract Method. But also, while we're here, notice that there's already a key binding for this, and on the
First, I can right-click, scroll down to Refactor, and then I can choose Extract Method. But also, while we're here, notice that there's already a key binding for this, and on the Mac, that would be Option-Command-M, so we could use that as well if we want. Okay, and now, what will we call this? Well, we named it write. Hit Return, and we're done. Yes, so, so much faster. So, how can we look at this? Well, I could scroll down, or, once again, I could press Command and click on the method name, and that'll take me there.
Well, I could scroll down, or, once again, I could press Command and click on the method name, and that'll take me there. Cool. Yeah, and this is pretty much what I want. However, I do want it to return the current instance. So, in this case, I will manually update it, and that's fine. Looks good. Okay, now, how do I go back to where I was before? Once again, you can just scroll up, or, as we learned, on the Mac, I can press Command-{, and that takes me to my previous edit point.
Chaining Return Values5:57
All right, I will type Method, and, yeah, that looks good. And we said Send. All right, so, yeah, now notice, because we returned this as part of that extraction, it is smart enough to know, all right, well, this, or the current instance, is returned from this method, so I can then return what is returned from that method from this method, and that matches the return type. So, once again, I will Command-Click on Send, and this is what we have. All right, finally, let's return to our prompt method, like so. And, yeah, now, if I want, because the write method already returns the current instance, I could optionally chain these, like this.
