در حال بارگذاری ...

File Symbol Navigation0:00

All right, let's keep going. I'd like to review more of phpStorm's refactoring capabilities. Okay, so once again, I'm within this AI chat class, and I'd like to navigate to a method within this file called send. All right, so of course, I can scroll the page, and that's fine. But another option is to view the structure of this file, and I'll show you how to do that. First, I need to go up to View, Appearance, and I can turn on Tool Window Bars, or on the Mac, I can double-press the Command key, and you'll see that temporarily toggles the window.

So again, notice this is by default bound to Command F12. But if I run it, now this shows me a floating panel of all of the various symbols within this file. And yeah, again, I can filter anything. So I could type Send, and that brings it up, hit Enter, and that instantly brings me there. And yeah, generally, that's how I will get around a file. So once again, if I'm at the top of the file, and I need to go to the Send method, I will hit F12. Or again, I don't love that binding, but it'll do for now. So once again, bring it up, type Send, hit Return, and we're there.

Or again, I don't love that binding, but it'll do for now. So once again, bring it up, type Send, hit Return, and we're there. Okay, so just keep in mind that this will only work for the symbols within the current file. However, if I close this out, and I don't have any file open, I can still navigate to that unique method by pressing Command P and searching for Send. Just keep in mind, this is going to show me all occurrences and files and symbols related to Send. But yeah, you'll notice PHPStorm is pretty smart. And at the top, it found a method called Send, and that's the one I want.

Extracting Response Content2:24

But yeah, you'll notice PHPStorm is pretty smart. And at the top, it found a method called send, and that's the one I want. All right, so again, that's a blazing fast way to navigate your project. Okay, so anyways, let's keep talking about some refactoring options. So right down here, we send all of the messages to OpenAI, and we receive a response. And then if we want to grab the actual text content of that response, so if I said, hello, OpenAI, and its response is, hello, back to you, well, we would actually go into the choices array, grab the message object, and then the content. And yet that would be equal to something like, hello, back to you, that comes from OpenAI. Okay, cool.

Introducing a Variable3:28

So I will press Command C just a few times to bring it back to what we started with. Instead, with all of this selected, once again, I will bring back that refactoring panel. And yeah, as we learned, I can either right click and go to refactor, or we can use the shortcut, which defaults to Control T. Okay, so now, what do I want to do here? I want to extract a variable. And here, it looks like they call it actually introduce variable. That's fine. Select it, what do we want to call it? content is fine.

Select it, what do we want to call it? Content is fine. Hit return to effectively commit the change. And yeah, that does the trick. Just a couple little things to talk about there. So once again, I will hit Command C to bring this back. If instead my cursor is just somewhere like right here, and I bring that up, if I introduce a variable, yeah, notice we have various expressions that we could choose from. And as I cycle down here, it selects larger pieces of that full expression. So yeah, if I only want to extract the responseChoices array into a variable, I could select

And yeah, this is something I reach for all the time as an aside. So maybe if I wanted to select this entire array, I wouldn't use the mouse. Instead, I would just put my cursor anywhere here and press Option-Up until I reach the array there. Okay, so let's try it out. Here, I want to extract this to a variable. All right, Option-Up just a few times. I will bring up the refactoring menu. I will type V-A-R for variable. I will extract a variable called content and then press Return to commit that, so to speak.

I will type V-A-R for variable. I will extract a variable called content and then press Return to commit that, so to speak. Okay, very cool. That works great. Okay, but next I'm seeing we have this response variable, and it's only used right here. So maybe I could inline this entire thing and remove that variable entirely. Okay, so again, traditionally, you might just cut that out and then here, replace it like so and get rid of the variable, and that's fine. Sometimes I still do that, but of course, PHPStorm can inline the variable automatically. So let's bring it back to what we had before, and this time, I'm just going to put my cursor

Inlining Variables6:16

Sometimes I still do that, but of course, PHPStorm can inline the variable automatically. So let's bring it back to what we had before, and this time, I'm just going to put my cursor on top of the variable. I will bring up the refactoring panel with Ctrl-T, and this time, we want inline, or again, notice the shortcut if you want to memorize that. So let's run that, and now it's going to say, hey, do we want to inline all references and remove the variable? Yes, we do. Run the refactor, and yeah, notice it does the exact same thing. So yeah, all of these little refactoring tricks and shortcuts that I'm showing you, trust

Run the refactor, and yeah, notice it does the exact same thing. So yeah, all of these little refactoring tricks and shortcuts that I'm showing you, trust me, I reach for them constantly. And what's cool is because it's so fast, you can play around. Like maybe you decide, well, for that matter, we only declare the content variable once. Would it be okay to inline it here? Let's try it out. Inline, run it, and here, you might decide, oh, that's fine, or you might think, oh, there's maybe a little too much going on here, and just depending on the day, you might come out one way, and another day, you'll say, oh, I'd rather have the variable, but yeah,

Renaming Variables Safely7:14

maybe a little too much going on here, and just depending on the day, you might come out one way, and another day, you'll say, oh, I'd rather have the variable, but yeah, it's very easy to test out these little refactors without much effort at all, which is really cool. Okay, so now, why don't we finish up with one more? I think content is fine. I mean, it does point to that, but maybe in our heads, it fits better if we call it response. All of this is generating a text response from OpenAI. Okay, so again, if I want to rename this traditionally, you would just do something like this, response, and then you'd find any usages and update those as well, but again, we could use the

Okay, so again, if I want to rename this traditionally, you would just do something like this, response, and then you'd find any usages and update those as well, but again, we could use the refactoring panel if we want, and that can often be a safer option as well. So let's put the cursor on the variable, bring up the refactoring panel with Control-T, and yeah, it's number one, rename. Okay, so now it's going to ask us, do we want to rename just the code occurrences of content or all occurrences? So notice with the second one, that also includes the content key, which is a string, but I don't want that in this case. So we will do the first, and let's change it to response, and hit return to commit it.

don't want that in this case. So we will do the first, and let's change it to response, and hit return to commit it. Yeah, let's try it one more time. Control-T, enter to rename, and change it back to content, and yeah, it's so fast and safe. So you can imagine if down here, we appended something to it, like, okay, done, you know, this is dumb, but if we had to append to content in some way, well, right up here, when I rename it, it would rename all occurrences and references to that variable. So if I change this to response, notice how they all update on the fly using multiple cursors, which I promise we will review soon.

So if I change this to response, notice how they all update on the fly using multiple cursors, which I promise we will review soon. All right, so yeah, I hope you're seeing for yourself what a time saver these refactoring capabilities can really be, but we're not done yet. So let's keep going in the next episode.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟