Introducing Factor Refactoring0:00
Let's take a moment to talk about a really neat open source project called Factor. Historically, Factor has primarily been a PHP refactoring tool, although in recent years the author has made a lot of its functionality available as a language server as well. Personally, I still prefer IntelliFence, but I'm rooting for Factor to take over. In the meantime, there are still many features we can use from Factor without using its language server. If we take a look on the website and come down to the refactoring section, you'll see we have all these different refactoring tools for doing things like fixes, so adding missing assignments, adding missing doc blocks. We've got generation, creating new classes, changing visibility, extracting methods, all these sorts of cool things. If we come over to the first one here, you'll see that the functionality is available via a CLI tool called Factor,
Installing Vim Plugin0:45
We've got generation, creating new classes, changing visibility, extracting methods, all these sorts of cool things. If we come over to the first one here, you'll see that the functionality is available via a CLI tool called Factor, but then it's also available through a Vim context menu, so it's under the transform section under the context menu, and also as a command in Vim as well. So let's go ahead and install it. I'm going to jump into our plugins.lua file, and we'll come down to after our last plugin as usual, and I'll paste in the code for this one here. So for this plugin, I'm using the ft option of Packer, which tells Packer to lazy load this plugin only when we're in a PHP buffer. Now, because most of Factor is written in PHP,
which tells Packer to lazy load this plugin only when we're in a PHP buffer. Now, because most of Factor is written in PHP, we need to use the run hook to run composer install whenever we install and update the plugin. And Factor has heaps of commands, but to start with, I've just set up leader PM to run Factor's context menu, which will only present us with the options that make sense for whatever is under our cursor. So let's save this and we'll run our Packer sync. And there we go. So let's jump over to our Laravel project and let's open up NeoVim. And how about to start with, we create a whole new controller.
Using Context Menu2:02
So let's jump over to our Laravel project and let's open up NeoVim. And how about to start with, we create a whole new controller. So I'm going to say php artisan make:controller, resource controller called, actually let's call this one WrongController. All right, so that one's created. And then we'll jump over into that controller. And you'll see we have our context menu down the bottom here. We can press any of the letters that are in parentheses to access that function. And some of these are a sub menu. So if I press T for transform file,
And some of these are a sub menu. So if I press T for transform file, then we get some additional options here, such as removing unused imports. We can also access most of these features by going factor and then pressing tab and we'll see all the different options factor provides for us. So have a look through them and see if there's any you want to create mappings for. To demonstrate one of my favorites, I'm going to jump into our routes file and let's come down and we'll create a resource route for users and we'll point it to our wrong controller. Let's use GD to jump back over the controller and we'll jump into our context menu.
Renaming With Move3:04
and we'll point it to our wrong controller. Let's use GD to jump back over the controller and we'll jump into our context menu. And let's use the move option, which is M. So see now I can come in here and I can change the name of this controller to UserController and press yes to the warning. And you'll see now that it has renamed the file. It's also renamed the class. And if we come back over to our routes file, you'll see it's also renamed all of the references to that. There's one other feature I want to show you, but it involves having a factor configuration file. So let's jump over to our .env files repository.
Configuring Templates3:38
There's one other feature I want to show you, but it involves having a factor configuration file. So let's jump over to our .files repository and let's jump into the install script that we created much earlier on. Come down to the bottom and I'll copy these ones and paste these here. And then let's change nvim to factor. Repeat that change twice. Go ahead and save this one. And now let's open up our tree over here. And I'm going to create a new top level directory in here using A and we'll call this one factor. And then inside here we'll create a factor.yaml file.
And I'm going to create a new top level directory in here using A and we'll call this one factor. And then inside here we'll create a factor.yaml file. So the feature I want to enable are factors templates. These are similar to snippets except that they're used to create the initial structure in a file and can automatically fill out the correct namespace and class name. So to configure these, I'm going to drop in this YAML configuration here and this is for the class NewCodeTransformation. I'm setting up a variant here called LaravelTest and I'm pointing it to a folder called LaravelTest as well. So go ahead and save this one.
and I'm pointing it to a folder called LaravelTest as well. So go ahead and save this one. And then in our factory directory here I'm going to create a subdirectory called templates. Inside here I'm going to create another subdirectory called LaravelTest which matches the name above. And then inside here we're going to create a file called sourcecode.php.twig. So let's open up this one. And I'm going to paste in this snippet. So this is creating a standard LaravelTest. And I often prefer this over the php artisan make:test command because often I jump to an empty test file using a tool I'll show you in the next lesson.
And I often prefer this over the php artisan make:test command because often I jump to an empty test file using a tool I'll show you in the next lesson. The syntax here is using twig and the examples on the factor documentation are pretty good. So let's go ahead and save this one. And I might also jump back into our plugins.lua file and add one more keymap which is leader.pn to call factor class new. So let's save that one and we'll run Packet Compile to make sure we get that change. And let's come back over to our Laravel project. So first of all let's close up Vim and open it up again. And now let's create a file at tests/Feature/UserControllerTest.php.
Generating Test From Template5:54
So first of all let's close up Vim and open it up again. And now let's create a file at tests/Feature/UserControllerTest.php. If I run my mapping leader.pn you'll see I'm now presented with some options here. And by default Factory comes with a few standard ones like enums, traits, interfaces and then standard classes. But I'm going to press the L option to use my LaravelTest one. I'll confirm that that file is okay and there we go. The only weird thing to note here is that IntelliFence gives us this error for some reason. So if I save this file and then reload it with :e then IntelliFence reloads and everything's fine. So that's it for this lesson. In the next lesson we'll talk about Projectionist which will allow us to jump between our tests and implementations really, really quickly.
