Rector is a tool for making changes to PHP code, which powers tools that assist with upgrading deprecated code in Drupal. When I recently made some refactoring changes in Drupal Code Builder, which were too complex to do with search and replace regexes, it seemed like a good opportunity to experiment with Rector, and learn a bit more about it. Besides, I'm an inveterate condiment-passer: I tend to prefer spending longer on a generalisation of a problem than on the problem itself, and the more dull the problem and the more interesting the generalisation, the more the probability increases. So faced with a refactoring from this return from the
to this:
which was going to be tedious as hell to do in a ton of files, obviously, I elected to spend time fiddling with Rector. The first thing I'll say is that the same sort of approach as I use with migrations works well: work with a small indicative sample, and iterate small changes. With a migration, I will find a small number of source rows which represent different variations (or if there is too much variation, I'll iterate the iteration multiple times). I'll run the migration with just those sources, examine the result, make refinements to the migration, then roll back and repeat. With Rector, you can specify just a single class in the code that registers the rule to RectorConfig in the rector.php file, so I picked a class which had very little code, as the dump() output of an entire PHP file's PhpParser analysis is enormous. You then use the rule class's I rewrote
Returning NULL from With the arrays that made it through the filter, I needed to make a new node that's a class instantiation, to replace the array, passing the same values to the Rector's list of commonly used PhpParser nodes was really useful here. A
This doesn't have any parameters yet, but running Rector on this with my sample set showed me it was working properly. Rector has a dry run option for development, which shows you what would change but doesn't write anything to files, so you can run it over and over again. What's confusing is that it also has a cache; until I worked this out I was repeatedly confused by some runs having no effect and no output. I have honestly no idea what the point is of caching something that's designed to make changes, but there is an option to disable it. So the command to run is: Once that worked, I needed to convert array items to constructor parameters. Fortunately, the value from the array items work for parameters too:
That gave me the values. But I wanted named parameters for my constructor, partly because they're cool and mostly because the CodeFile class's Inspecting the
Using named parameters here too to make the code clearer to read! It's also possible to copy over any inline comments that are above one node to a new node:
The constructor parameters are passed as a parameter to the
Once this was all working, I decided to do some more refactoring in the CodeFile class in DrupalCodeBuilder. The changes I was making with Rector made it more apparent that in a lot of cases, I was passing empty values. Also, the Changes like this are really easy to do (though by this point, I had made a git repository for my Rector rule, so I could make further enhancements without breaking what I'd got working already).
And that's my Rector rule done. Although it's taken me far more time than changing each file by hand, it's been far more interesting, and I've learned a lot about how Rector works, which will be useful to me in the future. I can definitely see how it's a very useful tool even for refactoring a small codebase such as DrupalCodeBuilder, where a rule is only going to be used once. It might even prompt me to undertake some minor refactoring tasks I've been putting off because of how tedious they'll be. What I've not figured out is how to extract namespaces from full class names to an import statement, or how to put line breaks in the Originally from Drupal.org aggregator https://ift.tt/SIt4JQm
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
April 2023
Categories |