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

Reviewing Tlint approach0:00

Let's start with what we know. We know that the method would need to have a public visibility. So, we can start there. Now, we're going to need to know a bit more. So, for now, I'll turn this false to make sure we short-circuit it. Of course, phpStorm is not going to like that. But, we want to return Relationship for the type in the event that this matches. Let's take another look at Tlint. They have this IdentifiesModelMethodTypes. And, I want to see how they're taking a look at getting the different relationships.

They have this IdentifiesModelMethodTypes. And, I want to see how they're taking a look at getting the different relationships. Looks like they have a pretty similar constant in this trait of all the different relationship names. So, I'm going to go ahead and copy that. The rest of these are just methods for the same type of logic we already wrote. We're just using some of the newer php 8 functions. Okay, here it is. isRelationshipMethod. Alright, they're actually analyzing the statements within that class method. That's pretty deep. So, if it has a return type, that's one of the relationship methods.

Choosing regex alternative1:08

That's pretty deep. So, if it has a return type, that's one of the relationship methods. Or, it has a return statement where the variable is this and the method name is one of the relationship methods. Then, return true. Otherwise, it's always false. We could definitely update our finder to take a look at things like this. But, this is one of the rare cases where you're going to get the same level of accuracy from a regular expression. And, it's only going to be one line of code. Going back to our example License model, Tlint's really looking for this segment in the abstract syntax tree.

Going back to our example License model, Tlint's really looking for this segment in the abstract syntax tree. It has to have that segment. That is a return statement that calls one of the relationship methods on this. It can't be anything else. So, all that abstract syntax tree code really just boils down to this string. And, we can do that in a much more streamlined way with a regular expression. So, let's go back to our Order model and fill that out. We can use preg_match. And, that takes a pattern and operates on a string.

Building relationship regex2:10

We can use preg_match. And, that takes a pattern and operates on a string. In this case, it would be the method block. This is equivalent to the string version of all the statements from the abstract syntax tree. Now, we don't have access to this inside of this method. So, we'll add that in a second. Let's focus on the pattern. What we need is a return statement followed by some number of spaces followed by this.

followed by some number of spaces followed by this followed by one of the relationship methods which will be suffixed with an opening parenthesis. And, I have all of those relationship methods in my paste buffer. So, let me use some php Storm magic to try and clean this up a bit into a regular expression syntax. So, we'll replace these with our alternation. And, let's break these all back down onto one line. Great.

And, let's break these all back down onto one line. Great. So, we have all of the different relationship methods. hasOne belongs to all the way up to the MorphToMany. And, we don't need that closing alteration. So, this is our pattern. Pretty ugly, but also pretty straightforward. And, again, it's just one line and does everything that we saw from Tlint. Now, we need to get this block into the method. So, let's bubble back up and we'll pass this in here as block.

Passing method block through3:26

Now, we need to get this block into the method. So, let's bubble back up and we'll pass this in here as block. And, let's see where we're calling method type. Let's make sure to pass this in here. So, what we can do is say this.extractDefinitionForMethod(). And, we need the lines. So, I'll add this to the closure. And, continuing to bubble up, we'll pass it into addMethodType. Let's add our type ints here for consistency.

Sorting methods by type3:52

And, continuing to bubble up, we'll pass it into add method type. Let's add our type ints here for consistency. Looks like I forgot them earlier. And, finally, all the way back up top, pass in lines. Alright, let's see where things are. Okay, it looks like it's still outputting the methods, but they don't seem to be in any kind of order that we want. So, let's take a look at our orderBy type. And, of course, we're just returning the methods back. Now's our chance to actually sort these by type or name, depending on where we are.

And, of course, we're just returning the methods back. Now's our chance to actually sort these by type or name, depending on where we are. So, let's do a user sort on methods. Pass in our own function. Again, this is a comparator, so we'll do a and b, where a and b are methods. And, now that those definitions have type, we can actually use that to reference the index of our order array. And, by comparing those values, ultimately sort the methods. So, let's bring that code to life.

And, by comparing those values, ultimately sort the methods. So, let's bring that code to life. We can return the ArraySearch, which gives us the index of a's type within method order, subtracted by the ArraySearch for b's type in method order. Alright, this line's a little dense, so let's use an example. If type a is a constructor, then that's going to be an index of zero. And, type b is a relationship method, then that would be four. If we take zero minus four, that's going to give us a negative value, which is good, because our comparator needs to return a number less than zero when a is less than b.

Validating sorted output6:54

for a's name versus b's name. And if we run this one more time, great. We'll see that our custom methods are sorted in alphabetical order, and if we keep scrolling up, we'll see that our static methods are there, sorted, and we'll see that our relationship methods are there, sorted. Devices is now ahead of users. Awesome. This comparator now achieves custom sorting for a method within the context of a Laravel model.

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