تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing Rector Tool0:00

Hey there. Welcome back. On this lesson I want to talk a little bit about Rapture React is an automated refactoring tool made by TOMA with Ruba, and honestly, it's pretty close to magic. Now, you might be thinking it's not an actual refactoring tool is that it doesn't actually automatically refactor code for you. And the answer is, it actually does. So yes, it's pure magic.

And the answer is, it actually does. So yes, it's pure magic. It's one of my favorite tools in php. And the easiest way to explain Rector is to actually show you all what it does. So let's jump into the code. First, let's take a look at Rector's homepage. Rector is a PHP tool that you can run on any PHP project to get an instant upgrade or automated refactoring. It helps you with PHP

Online Demo Refactoring0:39

to get an instant upgrade or automated refactoring. It helps you with PHP and framework upgrades, in-house framework migrations, and improving your code quality to deliver features faster than competition. Let's take a look at, try it online. Okay, so here we have a PHP snippet, we have a demo file class, we have a run method, and we have an E stream method. And now we have our rector config.

and we have an Eloquent stream method. And now we have our rector.php config. We're gonna get to this in a few minutes for now. Let's just run this. And as you can see, this is incredible. Okay, so it tells us which roles were applied and we had three roles being applied and now we have the depth. And you can see that it added return types for us automatically and the return types are correct. There was no human involvement here.

Installing and Configuring Rector1:15

for us automatically and the return types are correct. There was no human involvement here. This is essentially what Rector can do for you. You can automatically refactor your code. So now let's go to Rector's GitHub page and let's see, how can we solve this? Okay, first we have to require a composer package. Let's go ahead and require that. And now we simply have to run vendor/bin/rector. It tells us that no rector.php config file was found.

And now we simply have to run vendor/bin/rector. It tells us that no rector.php config file was found. So we can ask you to generate for us as well. Let's run that. Awesome. So before we rerun Rector, let's take a look at what we have on the file. Okay, so we are configuring Rector, we are telling it which paths it should run on, and then we have some additional methods and those are rector rules.

and then we have some additional methods and those are rector rules. So first things first, there are a few directories where we don't want to actually run Rector. We don't want to run in bootstrap or config or public or resources. We just wanted on those three directories, okay, we have some methods that dictate type coverage, that code level and code quality. For now, let's just run Rector.

Dry Runs and PHP Sets2:17

that code level and code quality. For now, let's just run React. But instead of running it like this, I'm going to pass a flag call, try running. And what Dry Run does is it'll tell us which changes we're going to be made, but you didn't wanna actually commit those changes. So let's run this and you can see nine files would have been changed. So on this one, on this route, it would add a return type.

would have been changed. So on this one, on this route, it would add a return type. Same thing here. Let's go up a little bit and let's pretty much it return type, right? Not a lot going on here. Let's clear this. And now let's go here and let's say with Hpat. And if you look at the signature for this method, we can pass which php version we are on and your reactor will automatically apply a bunch of roles. It has already registered some prebuilt roles.

and your reactor will automatically apply a bunch of roles. It has already registered some prebuilt roles that match features that were added on each php version. So if we say something like PHP 8.4, which is the version I am running through, let's rerun rector run and see what we get. Okay, so 14 files would've been changed. Let's see what those changes are. Okay, here we can see some additional roles closure to Arrow, this guy right here.

Okay, here we can see some additional roles closure to arrow, this guy right here. And closure of wide return type, something we had already seen. If we scroll up a little bit, same thing closer to arrow. Let's see, what else do we have? Oh, this is pretty cool. So we were doing get_class on this object. Now it's just using the class notation. This is the class on object Vector. Here we have a no strict string and call.

This is the class on object vector. Here we have a no strict string and call. So it is cast in message and author into strings because they're being used by trim. Here we have a closure to arrow function. Same thing here, closure to arrow function as well. Also adding the cast critical, right? Let's clear this. So now let's actually run reactor without try run and see what happens. Okay, call a bunch of files have been changed.

run and see what happens. Okay, call a bunch of files have been changed. Let's jump into one of them. Let's jump into, for example, InvoiceInterestSimulationController. And we can see that yes, it did update us to use a narrow function. That's pretty cool. Remember that in one of the php sent lessons, we added a bunch of doc blocks to our models specifically.

of the php sent lessons, we added a bunch of doc blocks to our models specifically. So the relationships, I went ahead and I removed all of them. So if we go to Invoice, you're gonna notice the relationships do not have those doc blocks. Now that is not because we don't need them anymore, we still need them, but this is also something React can do.

Adding Laravel Rector Rules4:44

we still need them, but this is also something Rector can do. So let's jump into GitHub and let's take a look at Rector Laravel. This is a really cool project that adds a bunch of Laravel specific rules to Rector. So let's scroll down and see how we can install this. Let's copy this command, let's run this. Sweet. We have that installed. And all this package there is just add a bunch of rules.

Sweet. We have that installed. And all this package there is just add a bunch of roles and a bunch of SATS to be used by React. So let's go into act. Let's go back to GitHub and let's take a look at the available roles. Let's go down a little bit and look at that. Add generic return types to relations to act. This is exactly what we're looking for. So let's copy this. Let's go back to React,

This is exactly what we're looking for. So let's copy this. Let's go back to React, and let's add a method called withRoles. And let's actually remove this comment. We can pass an array and now we can pass a list of roles we want to apply. So let's say we want to apply a generic return type. Let's rerun true, let's do a dry run first. And you can see that it automatically adds a bunch of those stock blocks.

And you can see that it automatically adds a bunch of those stock blocks. So let's go ahead and actually run this. Awesome. If we go to our Invoice model, it now has the necessary doc blocks. If we go back to GitHub, another useful R is this one. Add extends an ation to model factories. So it tells the model factories which model is being used. Let's add that one as well. So let's go to Rapture. Let's add a new role.

Let's add that one as well. So let's go to Rapture. Let's add a new role. Let's run rapture in dry mode. You can see it didn't find anything, and that's because we don't have database within our paths. So let's add database where model factories are stored. Let's try run and it does some other changes, but if we scroll up, we can also see it added the extends to some of our factories, to all of our factories, in fact,

but if we scroll up, we can also see it added the extends to some of our factories, to all of our factories, in fact, all the ones that were missing it. Super, super handy. So let's run this factory. And if we jump to TeamFactory, you can see that it extends Factory in re passing the model as the generic type rector works on top of roll, each roll does one thing. So each roll is a rapture and it only works against one type of thing.

Rules, Sets, and Best Practices7:07

So each roll is a rapture and it only works against one type of thing. And Rector also supports SATs. And SATs are basically call actions of roles. So you can compose your own rows, you can add all the ones you want, which you can also add predefined sets. For example, every time that a new php version is released, Rector also ships a set for the php version. So for example, let's say that you were running PHP 7.3

reactor also ships a set for the php version. So for example, let's say that you were running PHP 7.3 and PHP 7.4 was released and you wanted to automatically refactor code to use the newly introduced features from PHP 7.4, such as error functions. You could simply instruct Vector to apply the PHP 7.4 set and you would automatically do these changes for you. If it finds any block of code where a usual closure could be refactored.

If it find any block of code where a usual closure could be refactored to a error function like we've seen on this project, it would automatically refactor that for you. It's a fantastic tool. You can mostly leverage sets and then you can have a few extra rules for things that you think fit your project. Rector does a bunch of things, so I do recommend that you go through documentation.

Rector does a bunch of things, so I do recommend that you go through documentation. Just go to getrector.com, click on docs and read at the very least the first steps. Once you're done with this, click on fine row and you have some grips. So for example, let's go with php, and this is going to list you all of the available rows on Vector. Now, there are a lot of rules, so it'll be hard for you

of the available rows on Vector. Now, there are a lot of rules, so it'll be hard for you to go through them, but I think it's good to at least get your eyes through some of them just to have an idea of the things that React can help you with. You can also go to SATs and filtrate a little bit more. So for example, let's take a look at what we have for PHP 8.5. And for now we only have one rail. So this would be refactor to array, array_first and array_last.

And for now we only have one rail. So this would be refactor two, array, array first and array last. If we go to PHP 8.4, let's take a look at what we have. So removing parenthesis, which I love at Skate Parkman, and that's kind of math grounding mode. So a lot of things that I didn't even know change between PHP 8.3 and 8.4, react is a great tool to make sure your code stays up to date. Explicit knowable is a warning that I already saw on a lot.

to make sure your code stays up to date. Explicit knowable is a warning that I already saw on a lot of open source projects, and if people were using Rector, they wouldn't even see those warnings because this road would've fixed it. You can also always change the groove. So let's take a look at PHPUnit, for example, let's select the PHPUnit 10.0 sat. And we have a bunch of roles.

let's select the phpunit 10.0 sat. And we have a bunch of roles. We have some dedicated to converting annotations into attributes. Let's see, what else do we have? Public dataProvider. So as someone who did a migration from php unit 9.0 to phpunit 11 recently, fracture was a saver. It really, really saved me a lot of time. Imagine instead of having to go through a bunch of data providers instead of having to go through a bunch

Imagine instead of having to go through a bunch of data providers instead of having to go through a bunch of annotations just running true and having it fixed for you, it's such a blessing. Now, going back to our project into our Raptor configuration, my suggestion is start with the least amount of rows as possible. And if possible, try to, for example, should a separate PR for each new row added, maybe not at first, maybe not when you're setting it up,

for each new row added, maybe not at first, maybe not when you're setting it up, but later down the road as you're adding more rows and more SATs, trying to do it one at a time. And the reason I say this is because if you add a bunch of rules all at once, it might become really hard to review the pr. Imagine you just did php unit attributes, short closures, adding return types to methods. If you have a bunch of files,

adding return types to methods. If you have a bunch of files, you're gonna have a bunch of changes. So I would suggest doing phpunit as one PR, then return types as another PR, and then short closures as another PR in that example. But overall, React is a fantastic tool. My suggestion is go through the Ture documentation, take a look at the sets, see if there's a set that you feel very comfortable with,

take a look at the sets, see if there's a set that you feel very comfortable with, and then shut up a new branch on your project and play a little bit with Rector. Make sure to take a look at the VO community package. That's super helpful. Lots of really, really helpful Routes and SATs there and have fun with fracture. I bet you're gonna have a lot of fun with this. To as usual, thanks for watching and I see you in the next lesson.

To as usual, thanks for watching and I see you in the next lesson. Bye.

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