SARB and PHP Language Extensions with Dave Liddament

SARB and PHP Language Extensions with Dave Liddament

May 08, 2025

In this episode, we talk with Dave Liddament about his open-source static code analysis tool SARB which provides a tool agnostic method to create a baseline for your static code analysis and his PHP Language Extensions library which provides a way to extend PHP using PHP attribute's and PHPstan. 00:00 Intro 00:50 What is SARB? 09:33 PHP Language Extensions Library 15:39 HoneyBadger 16:33 Workshop on Rector 21:39 Outro Links: HoneyBadger.io - https://HoneyBadger.io Our Discord - https://discord.gg/aMTxunVx Dave's Social Media: https://x.com/DaveLiddament https://www.linkedin.com/in/daveliddament/ https://phpc.social/@DaveLiddament https://bsky.app/profile/daveliddament.bsky.social Scott's Social Media: Bluesky - https://bsky.app/profile/scottkeckwarren.bsky.social LinkedIn - https://www.linkedin.com/in/scott-keck-warren-91689810/ Mastodon - https://phpc.social/@scottkeckwarren

Transcript

Scott Keck-Warren Scott Keck-Warren 0:05
Hello developers, and welcome to the PHP Architect Community Corner, where we have conversations with members of the web development community. I'm your host, Scott Keck Warren, and today we're talking with Dave Liddiment about his talks at PHP Tech, as well as his open source tools. Dave Liddiment is a director and developer at LAMP Bristol, a software consultancy. He's been writing software commercially for over 20 years in many languages, including C, Python, Java, and PHP. Dave is keen to pass on his knowledge. He speaks at conferences and user groups and is the organizer of PHP SW. He is also the author of the open source tool called SARB, which is Static Analysis Results Baseliner, and the creator of the PHP Language Extension Library. Thank you, Dave, for finding some time to talk to us today.
B
Speaker B 0:48
No, thank you for having me on.
Scott Keck-Warren Scott Keck-Warren 0:50
Yes, absolutely. So, um, I, you came kind of into my view because you're gonna be one of the speakers at PHP Tech, but you also like do some open source development and the two libraries that you develop that interest me in your portfolio are this SARB library and the PHP Language Extension library. Can you talk to people about what those things are and how they help them with their software?
B
Speaker B 1:13
Yeah, sure. So I'll start with SARB, which is Static Analysis Results Baseliner. And this came about because I had a legacy project that I wanted to use PHPStan, PHPStan on to do some analysis and help find potential issues. And of course, if you run PHPStan or Psalm on a project, or, you know, that's legacy, it will find thousands, maybe tens of thousands of issues, especially at the stricter levels. And there's no way you can go and solve them all at once. And there was a time when both Psalm and PHPStan, they didn't have a baseliner built into the tool, and I really wanted to use them on this project. So I actually developed SARB to help with that problem, and eventually PHPStan and Psalm added their own baseliners, but they kind of work in a slightly different way. So SARB is a standalone tool. You can use it with pretty much any static analysis tool that you like, as long as you can convert the output from the static analysis tool to something that SARB will understand, or SARB also provides lots of plugins to use many of the common static analysis tools out there. So, I mean, that came about just because I had a genuine problem. It's like all good open-source software, really. There was a genuine problem that you had, and then you kind of make a tool. And the great thing about open-source software is you can then, you know, let everyone else know about it, and then potentially other people find out about it. So, yeah, I think I'm probably one of the main users of it. I'm not sure many other people use it.
Scott Keck-Warren Scott Keck-Warren 2:59
I find it really fascinating because we're a heavy user of CodeSniffer. And occasionally I will go, oh, it would be really nice if CodeSniffer could help us prevent this thing. And then I go, oh, there's a rule for that. And then I add the rule in and then there's like a million lines that have— a million is too much, but thousands of lines potentially that are an error. And it's just too much to clean up. And so, like, our, like, our fix for this is like, we have a, we have a, a pure, like, this is the optimal coding standard, uh, like rule set. And then we have one that's like more, more allowable that like everything passes. And then we just have like a crazy whitelist system where we whitelist the files that, and this like fixes that. Yeah. It's, it's insane. It was, I didn't know this was a solution. This is like a way better solution to the problem. And so I'm like, okay, well, we're switching to this now because I don't have to maintain. A tool. Yeah. Yeah. So, it's really like a fascinating solution to our problem in particular. I'm sure other people also have that problem as well. So, what is the limitation? Most of the tools it looks like that I use, which is PHPStan, CodeSniffer, I think the Mess Detector is also, and the Magic Number Detector. Are there other tools that people usually— what tools common tools do not usually work for this?
B
Speaker B 4:16
Well, I mean, we've actually even used it for ESLint. So it's not restricted to just PHP. It's a standalone application that you can run. And we had a problem, we had a project, and it's similar to what you were describing. You've kind of got the strict rules and the less strict rules. And we would run the less strict ones, and, you know, rule sets, and you couldn't have anything that would pop up in there. But then we ran ESLint on an even stricter version, and of course, it's legacy code, so there's many, many issues found. And we would use Saab to only show the ones we'd introduced, and obviously have the baseline and make sure we didn't make anything any worse. So, yeah, we've used it cross-language as well. I mean, you could use it, You could use it anywhere, really. But the main ones I use it on is just PhpStorm and ESLint. And I did— we stopped using Psalm, but we used it for a bit on Psalm as well. But we just, in the end, we just went PhpStorm all the way. Trying to keep 2 tools happy is hard work at times.
Scott Keck-Warren Scott Keck-Warren 5:32
Absolutely. And so, how does it keep track of, like, all of the errors? Is there a file that you have to maintain, or?
B
Speaker B 5:38
So what actually does it slightly different. So what happens is on tools like PhpStorm and Psalm, what they do is they look through each file that they're analyzing and they would say, in this person.php file, we've got, you know, 1 error of this type, 3 errors of this type, 4 errors of some other type. And then what happens in PhpStorm world is it would do its next analysis and it would tot up the number of errors. And if you had an increase in any of the errors in that file, it would say, well, you've introduced an issue. Now, so that's good, but the limitation of that way of doing things is if you renamed, say, person.php to employee.php, that kind of baselining would suddenly say, well, this is a completely new file, so all of these issues I'm going to report as new issues. even though they were there before, you've just renamed the file. What SARB does is it uses a bit of magic from Git, and it associates every issue with a line of code in a particular file. And then you create your baseline, and at the point you create the baseline, it also remembers the Git SHA at that point. And then what happens is later on, you're obviously going to change your code, and behind the scenes, it does a diff from where you are now to that Git SHA, and it can say, I've got an issue on line 13 of, like, right now I have an issue on line 13 of person.php. Where was line 13 of person.php at the point we took the baseline? And either it wasn't in the baseline, in which case this is a new issue you've got to fix. Otherwise, Git will say, you know what, that was line 17 of employee.php at the baseline. And then it goes, all right, did I have this kind of error on line 17 of employee.php at the time the baseline was taken? And if it was, we go, well, it's the same issue, we've just moved it around. And if there wasn't an issue at that line, you go, well, this must be a new issue, so we've got to tell someone this has been introduced since the baseline. So if you are doing— if you're using tools like PhpStorm, and you are moving files around quite a lot, then maybe have a look at Saab because it approaches the problem slightly differently. That might be more useful for what you're doing. But yeah, behind the scenes, Saab uses the cleverness of Git to work out where a line of code was at the point it was when the baseline was taken.
Scott Keck-Warren Scott Keck-Warren 8:23
That sounds amazing. I didn't even know that was something that Git could do. It seems like that would be so many steps involved in trying to figure out where that line had been at that point in time. So that's another fascinating piece of information.
B
Speaker B 8:34
Yeah, it took me— well, of course, at the time, I hadn't seen anyone work out how to do baselines. And in reality, how tools like PHPStan have done it is a lot more simpler because you don't have to truck things between Git, you don't have to do complicated, like, working out, you know, counting lines in diffs and things like that. So actually, I kind of wish I thought of the simpler idea first because that would have been much easier to implement. And you learn a lot about obscure command line options you've got to give Git to make sure it can track things across file moves as well. So, I mean, that was interesting in itself.
Scott Keck-Warren Scott Keck-Warren 9:16
Yeah. I always find Git just infinitely powerful, where I'm just like, oh, I needed to learn this one little thing, and it's like, well, there's 15 command line switches you give it, and then here's your information. It's like one report, essentially. It's always fascinating. Yeah.
B
Speaker B 9:30
It's like Vim. You could learn it forever. You'd learn something new every day, forever.
Scott Keck-Warren Scott Keck-Warren 9:33
Yeah, yeah, absolutely. Can you talk to us a little bit about the PHP Language Extension Library as well? Because that kind of like is the same helpful tool, or not the same helpful tool, but it's, you know, it helps people find issues as well.
B
Speaker B 9:45
It helps people. So I've used many languages apart from PHP. And often what happens is you find there's things in other languages you just wish PHP had those features. And of course, in PHP world, you can do that. You can submit an RFC and hopefully persuade enough people that this is a good idea. And then eventually, maybe 2, 3, 4, 5 years later, it becomes part of the language. But there's lots of things you could do using static analysis. So in the PHP Language Extension library, I introduced some additional visibility modifiers. So, in PHP, we've got public, protected, and private. But sometimes you're writing some code, and you think this should only really be used by other things in the same namespace. I want to limit the visibility of this class or of this method, so it can only be called by things in the same namespace. And in Java world, there's a visibility modifier called package. But what I've done is I've added an attribute called namespace visibility, and you can either put that on the class or you can put that on the method. So the method has to be public. But what happens is static analysis, PHPStan in this case, it looks at all the times where that method is called from, and it says, if it's got the namespace visibility attribute on it, it says, well, has it been called from something in the same namespace? And if it has, it lets it go. And if it hasn't, then it will say, well, you're not allowed to call this. Really, if this was supported in the language, you wouldn't be able to make this call to this method. So, again, I think it helps. I mean, generally, the projects I work on are quite long-living projects. One of them has been going for 12, 13 years, another's 10 years. And I find that you have to be really— it's helpful if you can express what you were thinking at that time in a coded way. You know, like, this class should only be used with other collaborators, or collaborate with other classes in the same namespace, is an example of that. So, the ability to add in essentially additional language features to PHP via static analysis I think is a really powerful thing because there's lots of benefits, you know, compared to the RFC method. For example, I mightn't be the only person who thinks this is a good idea. So I don't have to convince 2/3 of the people on the internals mailing list that this is a good idea. And then I don't have to implement it in C, and then I don't have to wait years for it to happen. I can implement this in a few hours and I've got it forever. And in fact, actually, I'm gonna do a talk at PHP Tech where I show how to make It's a concept called friend, and we go through that bit in about 30 minutes, and the code almost fits on a slide. So it is incredible what tools like PhpStorm offer through their custom rules combined with attributes. You can add many, many new features to the language. So yeah, and that's out there, check it out on my GitHub. There's kind of 2 packages at the moment. One of them just defines the attributes, and then I've got an implementation using PHPStan custom rules. And if someone's watching this and they know how to make Psalm custom rules or make plugins for PhpStorm, then you can use the same base library which defines the attributes, and you can just make your own implementations of tools that check that.
Scott Keck-Warren Scott Keck-Warren 13:37
Yeah. And, and I like, I'm, I, I was looking through them. I was like, oh, I can use like most of these attributes. Like I was just like, like immediately I was just like, oh, I see a use case for every one of these pieces. And like, it's, it's in our backlog. Like adding this to our, to our static code analysis tools is in our backlog. 'Cause it's specifically, 'cause I have like 5 cases where I wanna use like the different attributes. So it's definitely like, well, like, well, it's useful. I can definitely see that. Like just instantly I said, oh, I see the use case for all of these.
B
Speaker B 14:03
That's really good. I mean, again, they've all come from mistakes I've made. Every single one of them, I've made a mistake. And this is another great thing about static analysis tools generally. If you make a mistake once, you can ask yourself, is there a root cause to that? And can I create some kind of custom static analysis rule, or can I use some existing static analysis rules out there to prevent this mistake from ever happening again? And that is the real power of these tools. I don't know if people have— if you just take PhpStorm and use it out of the box, you have so much. But if you learn how to create these custom rules, you can do anything. You can have your coding standards codified in PhpStorm rules, And then they never get forgotten, and they always get enforced when you run static analysis. And I think people, generally, I've found, you know, if you're telling someone they've made a mistake, sometimes in code reviews or whatever, that can get, you know, oh, God, is it really a mistake? But if a tool tells you you've made a mistake, there's nothing you can do. You just have to fix it, you know? So, I don't know. I could talk about this for hours.
Scott Keck-Warren Scott Keck-Warren 15:22
The one that really like intrigues me is like, there's this like must use result attribute. And like, cause I've seen, I get that a lot, like in our, like in our code reviews, essentially I'm just like, well, you calculated a value here, but then just left it. Like, you know, just like lost that train of thought. So I'm like looking forward to just throwing that into places now and be like, well, now it's broken. So go fix it, please. And we'll be back after this word from our sponsors. As a developer, you know that production monitoring can be complicated. There are tons of tools and techniques, but you just want to know that your app is working and your users are happy. When your customers encounter a problem, you need clear, actionable insights, not walls of charts and reams of logs. That's why they built Honeybadger, the monitoring tool that you've always wanted, a tool that's there when you need it and gets out of your way when you don't. With Honeybadger, you'll know when critical errors occur, which affects customers, respond instantly when your systems go down, improve the health of your system over time, and fix problems before your customers can report them. Honeybadger is the application health monitoring tool built for you, the developer who cares about a quality product and happy customers. Start monitoring today at honeybadger.io. It's free to get started and setup takes less than 5 minutes. Once again, that's honeybadger.io. And we're back with Dave Liddeman. You are one of the speakers at PHP Tech this year in 2025. Um, I know you've already talked about how you're going to talk about the static code analysis or extending the language. Um, you have another talk. Do you want to talk a little bit about that as well?
B
Speaker B 16:45
That's right. I'm also doing a workshop. about a tool called Rector, and Rector helps you refactor or modify your codebase or do upgrades. So out of the box, Rector has many rules. So for example, if you were going from one version of PHP to the next, you can run Rector on your codebase and it will find maybe things you can't do in the previous version of PHP, and it will update it to the new way of doing things. Or it could do things like, if, for example, when property promotion was introduced, it can take your code and change it all to use property promotion, because, you know, it's a bit neater way of doing things. So, again, Rector has a lot of rules out there to help you with most of your use cases, but sometimes you'll be working on something. So, I started using Rector a lot when I had an upgrade. I was going from Laravel 4.2 to Laravel 9, and we've done lots of things which were kind of not really the Laravel way of doing things. So we didn't really have any help from the tooling to do the upgrades we wanted. But what I'm gonna— what you can do is you can create custom Rectors, and what that does is it takes your code and modifies it from where you want it to be to— from where it is to where you want it to be. And we're gonna do a workshop that shows you how to make those rules. We're gonna do it in a test-driven way. And again, it's such a powerful tool. If you've got, like, a project where you've gotta go, I've gotta go and make 20 or 30 or 100 changes, and all of these changes are essentially the same thing, then it's much more interesting to say, right, can I automate this with Rector? Can I create some Rector rules or custom Rector rules that do this? Oh, great, I can. And the more you do this, the quicker you become at it, and then the more likely you are to do these things. So, it's another great tool that helps developers just make their code changes as quickly as possible and as safely as possible. Humans are not very good at doing the same thing over and over again. They get bored. Whereas if you've worked out how to do it once as a rectable, it will just apply it to your entire codebase. Incredible tool. If you haven't checked it out, definitely check it out.
Scott Keck-Warren Scott Keck-Warren 19:17
It's one of the static code analysis tools that I include in my pantheon, for lack of a better term, of stuff, right? We use it heavily to enforce there's no dead code, we're upgrading to the newest version features when we can. So it is definitely a great feature, but I've never tiptoed into this whole writing a custom rule. So I'm really looking forward to seeing how that all works out because that sounds just completely fascinating.
B
Speaker B 19:43
I mean, it's kind of like the— if you write a PHPStan rule to tell you where you've made a mistake, there are some times when you could completely safely fix that. So it's a bit like when you use a coding style tool, You know, you can write the code however you like, and then it'll just reformat it and put the semicolons and the open and close braces wherever you want them to be. There were some issues that you could find using PHPStan, and then you'd have to go and manually fix them. But the question you always wanna ask is, oh, actually, could I have a Rector rule to just fix it automatically? And then the same way you run CSFix or CodeSniffer, before you check your code in, you just want to run Rector, and it would go, oh, I can just change these things safely. And then after that, you would then run PHPStan, and it would find anything that you can automatically change. So, yeah, it's kind of both the things I'm talking about do work really well together.
Scott Keck-Warren Scott Keck-Warren 20:45
Yeah, absolutely. So, is there anything else that you wanna direct our listeners to before we let you go for today?
B
Speaker B 20:50
Well, if you ever are in Bristol, in the UK, then please come along to PHP Southwest. We are the 2nd Wednesday of the month, and it's a friendly group meetup. We're always talking about something PHP-related, and I love talking about any of these things. So if you are coming to PHP Tech and you have your own questions about PhpStorm or Rector, then just find me, hallway track. I love talking about this, you know, we can talk about it as much as you want. And if you're not coming to PHP Tech, then you can find me, I'm @davidvidermunt on all of everything. So find me on Twitter and Bluesky and LinkedIn. Yeah, and if you've got any questions about any of these things, just love talking about them. So
Scott Keck-Warren Scott Keck-Warren 21:38
yeah. Awesome. That's great to hear. So thank you so much for finding some time to talk to us today.
B
Speaker B 21:42
No, thank you very much.
Scott Keck-Warren Scott Keck-Warren 21:44
Yes, thank you. I have to say another heartfelt thank you to Dave for all of his time and also to you for all of your time for listening to the podcast. If you're not already a subscriber, make sure you subscribe with whatever your preferred method is so you can get episodes as soon as they're released. And Maybe share this episode with a coworker so they know what's going on with static code analysis tools. This podcast is available both as an audio version on the PHP Architect website, as well as a video version on our YouTube channel. This is Scott Keck Warren for the PHP Architect Community Corner, signing off and reminding you to keep listening, keep coding, and keep reading.