Strict Variable Types in Laravel: Practical Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video we will talk about strict types and specifically declare strict types in LEL and in PHP recently I saw this pull request in the open source project find a PR by Ash Allen well-known laral developer and a blogger and all the changes in that pull request were declare strict types in all the files and I thought what is the real practical benefit of that I know some people use that I know it's kind of a better practice to be strict but in what real practical cases it actually helps to solve any bugs and I asked on Twitter and got quite a few interesting real life cases scenarios and examples and in this video I will show you them one by one and at the end of this video you will decide how useful it is to have types at all and strict types specifically first I remember that in larel 10 there was quite a big hype where no Maduro spent a lot of time implementing strict types all over laral ecosystem pack PES and I had a video about that and I will link that in the description below with examples of why types are important but with this specific tweet about declare strict types I received these scenarios so my colleague modestas showed me this example let me Zoom that in of charging so for example if you don't Define the value and the tax types it's not clear the tax is 21% as integer or 0.21 as as a float and I tried to reproduce it in my own laral project so this example B modestas is just PHP but in laral case for example we have a service class for some calculations and we call that service methods from controller so the first scenario is if we don't Define any types here integer or float and then if we call the same function for calculation of tax as with 21% or 0 21 both would work just return different results in reality it expects 21 as integer but the results as you can see are different in the browser now if we Define float price for example but tax should be integer and we don't change anything in the controller and we refresh and it still works but with different results so what it does if you don't Define strict types text will be rounded into int from 0.21 to0 I guess it may depend on your PHP configuration and may cause some work learning but in my case in default laral new installation it just rounded the float to integer zero now what changes if we had declare strict types in that service class we refresh and nothing changes it still works what does affect if we add declare strict types in the controller in the place where the functions are called and as you can see now PHP storm even underlines that 0.21 as Incorrect and now if we refresh that we will get an exception internal server error now I need to zoom out must be of type in float given so this is an example where declare strict types in the controller or in best case it should be in all files just to make sure actually saves you from the bug of course it could be improved even with variable names for example so tax percentage for example would be more clear in terms of just variable name or maybe some comment in the code would be also valuable on top here but still declare strict types is the actual final kind of final form tier final middleware to restrict this case another example in the same project is based on another comment by K it's about zero or false for example let's remove declar strict types again for a while and then there's another function called calculate price which looks like this in the service so calculate price and on some condition it returns false otherwise it returns some number and then what happens as I understood Kai's thought so it returns false and then in the controller that false or whatever is returned from that function is passed to another function another method and now what happens fals is automatically transformed into zero and doesn't throw any errors it just shows zero at the end so zero with TX but if you define declare strict Types on top we refresh and we we have oh that error comes from the second one so let's comment that one out we're refresh and this ISS the error must be of type float bleen given here so this is another example how PHP kind of transforms the types between each other which is not necessarily what we need in real life of course this example is also kind of artificial and probably returning false is a bad code structure anyway it probably should throw some exception which should be caught in the control but you get the idea if you have false it is transformed to zero with unpredictable consequence I also got a few more comments from that tweet which I was unable to reproduce actually so Wendell told the example of UPC numbers with leading zeros and of course they may be misinterpreted as numbers but they should be strings and then similar example is followed up by add with us zip codes that also could start with leading zeros so if you don't specify the R for them then maybe in some case I was unable to actually reproduce that but maybe while exporting to Excel for example Excel May interpret them as numbers for example instead of strings so declare strict types also can save from that and a few more comments related to the same thing if you work with data that is unpredictable for example by third party some API maybe your colleague more Junior developer or not that thorough with the code basically you don't control the the data or The Code by other developers so this is a comment by donatas explaining that scenario also exat pointed the Legacy application which is also for example the code from some older PHP version or laral version which worked with PHP types in different ways maybe and getting back to the team aspect different developers so already threat about declare strict types and one of the most upvoted answers was about massive Symphony projects and our developers have varying skill levels so it's kind of enforcing strict types and enforcing more rules is kind of an insurance extra layer of rules to avoid bigger Errors By less experienced developers and finally if you're kind of convinced that you should use declare strict types for bigger project or bigger team you can also go a step further and automate some stuff so Ash Allen in the original repository that I've shown in the beginning of this video on top of declaring strict types he implemented the past architectural tests to actually test if all files contain strict types so here it is I've cloned down that repository by Ash Allen and in the test architecture HTTP test controller test for example have this test expect all files in controller folder namespace to use strict types so if we run PHP Artis on test on that repository It All Passes including that controller's test but if if we go to any random controller and remove that decare strict types from Top we rerun PHP it and test and then it fails so you can actually automate some stuff from past architecture level and I have a separate video about it about past autod detection of EnV DD and other leftovers which is part of their architectural testing Suite you can look it up in the past documentation finally finishing the thought about automation we have a separate course on Lille daily about laran which is a layer on top of PHP stand for static analysis and one of the lessons is specifically about missing types so your static analysis to like Lan could check if the types are missing so for example you have quite a big file with a big function inside and you can set so-called level six in configuration which would actually detect that some method has no return type specified also has a parameter with no type specified and then when we fix by providing array here for example or request here rerun Lan again we get another error that return type has no value type specified and then another fix is to add a dog block optionally if you want to and then you have okay no errors so yeah this is just one example of automating that type strictness in listan I will link that lesson as well in the description below so yeah overall are you convinced that you should use types in the first place and then the second second layer declare them as strict types or do you have other examples to convince others to use those share your experience in the comments below if you have any real bugs or real stories or real scenarios where type mismatch caused real bugs in production projects that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 5,237
Rating: undefined out of 5
Keywords:
Id: EHuklaqhjss
Channel Id: undefined
Length: 9min 19sec (559 seconds)
Published: Wed Jun 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.