 Thanks for introduction Today I will be talking about in some upgrades what it is and how rector can help you I'm open source consultant and I love PHP So I write about it twice a week and I also help developers to be happier I'll consult companies and help them to convert their private source code to open source in the way they enjoy it I must confess. I'm a github addict. I I go there very often and create open source code So if you procrastinate by going Facebook and Instagram then I go procrastinate by committing The other thing I love is people to hang around That's why I started meetups in in our country in Europe two years ago At the moment we are in many cities and I also love to go abroad some very happy to be here and What I also love is learning and teaching so We try to organize lectures with our best speakers so they can share their skills and so other developers can learn from them So to my talk today, I would like to share with you these three points Why we upgrade? How do we do it and how can we make it better? Just for start. I would like to check the frameworks. What are you zinc? Is there somebody using symphony? cool and Laravel Cool. Are there some other frameworks use here? Yeah, Zen Cool, cool. Great so many frameworks. Okay, so if you use frameworks, you probably know What I'll be talking about When you start your new project, it usually uses the newest PHP newest dependencies newest symphonies and or Laravel and everything is very nice but as the life goes and business business rules are Applied you need to add more features. You need to grow the code. You need to use Rest API, etc. Etc. And not only that even if you would develop Just a little little code application and you want to grow it You also depend on others for example PHP and PHP is released every every year nowadays And it's being deprecated every year the older versions. So you have to adapt to this You can like You can forget about it, which most companies do they like it will work. We don't have to upgrade it We don't want to pay our developers to upgrade applications but in my experience it usually gets them in like eight or ten years, so they have to delete the whole code and start from a brand new and Also frameworks will work like this for example symphony is releasing LTS version every I think two years So every two years you have to you are forced basically to upgrade again And this will soon get you to this shit storm when you have to upgrade code or work with the legacy But legacy code is not something that is bad. It's just something that is hard to maintain So is there a way to work with this Where if you want to upgrade for example from symphony 3 To symphony 4 or any other PHP for example from 5 to 7 etc. We can Do it ourselves. We can study what has changed how do we upgrade it read posts about it and Also use PHP storm if there was some class change or method change We can use PHP storm amazing refactor feature and just rename it, right? But there are also some problems if there are if you are lucky enough to have these upgrade and Change lock in the in the project and open source there might be even some information what has changed and how But for example, there is missing name space So if you see something like this, you have to go to the github explore the repository find the name space and Fix it and also I found a few cases when the name space if this if it's mentioned there It's not right. So you have to again go to github explore git locks git blame, etc which is pity The the same goes not only for the new spaces, but method names arguments type hints, etc and sometimes the precations are Hb and sometimes they are dropped So you have to find out yourself if the class was removed and you have to do it manually Or if it was renamed and you can put it somewhere else Okay, so you can do it like this or you can hire somebody you can hire expert you can hire the author of The open source project and he will explain what has changed how how to refactor or how to do it yourself It's also a way it costs not time, but it costs money and I think it's that's a very effective way way than to do it yourself because if you do it yourself you will find a lot of blind parts and The third thing is you can also also said for some For some frameworks like this Laravel there are no services That can upgrade the code for you You will just give them a couple of bucks and they will bump the version of framework you are using But the problem is that your project probably is not using just one or two packages This is a composite JSON content from Middle-sized symphony project and as you can see there's much more than just symphony So in the end you will end up having many many trainings that are very costly for you and Or you will have to stick with the old version Actually, I was or still am one of these guys and go to companies and I try to explain them How to do it in you symphony how to do it in you net as just check framework Because I'm kind of like github edict. So I always follow the news But after like 10th training I was starting to weird that I'm telling the same stuff over and over again to many developers companies are paying for that, but It's still very slow, right? You have to teach every single person about these news So I wanted something that's better that doesn't depend on a person or money Or even a framework. I don't want to use just one single package that is upgraded I want to use everything that is on github available So that's why I thought that this could be automatic, right? It's still learn explain do forget learn explain do forget with new framework with the symphony for You can forget what you learned for symphony to basically because there are always news and news So that's why I started to think about it in a different way automated way That there could be tool that could Change such code. So I made a rector a year ago. I started on it In short, it's a command-line tool built on symphony php parser and php stem That will automate these a to be Changes for you. You won't you don't have to learn anything. You don't have to pay any Trainings you just run the tool It at the moment it changes php code and a little bit of why YML and And you can use it not only for Insult upgrades like these symphony level, etc But if you have your own internal framework and you want to upgrade for example 100 classes in some way or You want to refactor it to Some open source code. You can also use it When I name some basic changes it can rename basic elements of php it can remove name space and It can work or modify method arguments It can also add type hints, which is something you will often have to do if you use third party third-party code with php7 and Change visibility But it can do also much more. It can work with the logic of the framework. It can get in the container of the framework and Does it work? Yeah, and for example, you see that this in old version of symphony, you had a global container available everywhere with Locator of service with string, but now it's preferred construction injection So if you want to refactor to new symphony, you actually have to find all these places Whereas this get Find the service in the container get the type Replace it in constructor and replace it here, which is really tiring if you have hundred controllers and you have to do it everywhere So this all can rector do Yes, and How does these rules look like? Actually, they are already Preverton definitions. For example, this one is for method name replace when you for example have Get line and it's changed to get template line So instead of using PHP storm find and replace and using for get line you can use this, but it also Works with the context. So if you have The week note and this get line it will detect it. But if there is for example Template class with get line, it will be skipped Which which is what PHP storm cannot do I try to make the Make the usage as broad as possible So I try to support more and more frameworks at the moment these are the one who are supported and I will try to make a little demo. So let's see how it works Okay Maybe can I ask you? Thank you. So this is a this is a symphony application We have some kernel which is basically like Simple container with some services. There's the source code. Where is controller? Form and some dump service and there is a small test So as I showed you if you have old Old symphony, where's this get? And you want to change it to construct injection? You can either do it manually Like this and try to type hint Yeah, you see it's very slow. So Instead you can use Rector Rector is just normal Composer package that you install by Composer require Rector Rector and Here are some predefined Rules that you can run second Okay, so it's normal binary file you can run with bin Rector process and The director you want to process dry run is just dry run. So if you want if you run a Rector on our Controller with dry run you will see that it will find the service of its type will create the property Constructor and replace it in the code For every service you you also have there But as you can see it's not very nice because we don't have spaces here and this is not important important So instead of doing it just like this you can use with style and Yeah, it didn't work, sorry But actually it will it will run your own coding standard tool if you have any available and it will it will use the Then coding set you have so you can run Rector with your only coding standard But as you can see it replaced what it should replace. So there is a added property in constructor and replaced in the code Some other change that happened and to show you that it's not only just string and replace Here we have form type text and here we have it too In symphony this was actually replaced by class type. So we have to find What is the new class for this and replace it here and import it? Or again, you can run it with Rector This is the second one So we just run process a CRS form and see the file Yeah, and you see that it replaced the string with the class type, but it left the other string B, so if you would be doing this with Replacing PHP storm you would probably have many false positives But Rector knows that he is in a method where there's a method called Add and it's the second argument and it's specific string. So The next case is just very simple one for PHP unit You can try it at home if you want actually not a big deal, but If you have Expected expression annotation and you want to change it to method which is recommended and you want to do it manually again like this Expected exception and copy pasting this You can use level which is basically just a set of rules combined together with a version of the of the project and It will actually remove these Renotations and turn them into methods and many many more for PHP and there's also one for YAML Because not only not only PHP code changes, but also our configuration and sometimes it's renamed shift up shift down And also you don't want to do it manually In this case, there's a Symphony security bundle and there was changed Just one line But you have to find where it is and What does it do? So here was changed key to secret again? It's very simple, but you don't have to do it manually in your whole application and this is also useful if you for example do many changes in the bulk and you want to send it for code review and you will get feedback and You will have to git rebase, etc. You would have to again do it manually, but With vector you can do it. You can rebase and just rerun vector again. The last thing I would like to show you is to How it's actually configured? As you can see above I have two rules here these are basically Just services as you can see It's normal container from from From symphony and you will just define your services here Now I would have to Enable the symphony plug-in so I have how to complete Yeah, so if you want to type some Pretty fine vector You can just try method rector And you will see What everything is there and if you want to for example replace method by some type You will just type method rector hit control space It will show you we will pick it up Then there is usual constructor argument you have to complete So we'll use it here and then we just write some type old method new method And that's it and this is this is the way you will you can simply use it for yourself, but The best way to use it If you are like starting with director is to use some use level as you could saw Yeah, if you use some level that doesn't exist it will show you all the available levels So there are for example something for doctrine for cake php twig Silver stripe, etc. And these are just for example if you look at cake php three six These are again just set of rules replace method replace class and so on So thank you. I hope just one thing went wrong. Okay I often have a question. What's the most complex rector because these are basically just replacing of something according to logic and This one this one is I made two months ago because in rector I have at the moment hundred and twenty rules and They had three methods And I needed to merge two of them to one But also respect some arguments and instead of return null return false and so on so instead of doing this manually you can actually use rector and write your own rule and Do these bulk changes for you? You can actually see it on github on rector rector in these examples Okay, so I'd like to finish up with something that this might sound like Too futuristic for you, but I believe that when more and more frameworks will add this approach instead of writing a great MD We can soon enjoy the newest version of anything for almost free Thank you. So do you have some questions? Okay. Yes Is there a rector plugin for PHP storm? No But you can yeah, but you can I think Some better do's are just not made for plugins. For example, if you have a plugin for code sniffer It doesn't work well, but you can use it already with the command line Have you tried to migrate symphony to to symphony for this guy? I saw that you give an example of symphony Yeah, I have a friend who was on symphony two point seven and he migrated to symphony three point four And he had some templates and rotation stuff Okay, but not from two to four directly Actually usually migrate from mind revision to mind revision So you just run four or five sets and you will get from symphony to point something to a four oh Okay. Thank you. Mm-hmm. Do by the way, do you have project on symphony two? and two point eight or That's good Okay, thank you. Thank you for addition