What if I told you that you don’t need a 100% code coverage?

Compiled languages need to know about the type of every variable, return type of every method etc. before the program runs. This is why the compiler needs to make sure that the program is “correct” and will happily point out to you these kinds of mistakes in the source code, like calling an undefined method or passing a wrong number of arguments to a function. The compiler acts as a first line of defense before you are able to deploy the application into production.

On the other hand, PHP is nothing like that. If you make a mistake, the program will crash when the line of code with the mistake is executed. When testing a PHP application, whether manually or automatically, developers spend a lot of their time discovering mistakes that wouldn’t even compile in other languages, leaving less time for testing actual business logic.

There are several static analyzers available for PHP that substitute the role of the compiler from other languages. By finding bugs without even having to execute the code, they can save a lot of effort that’d be spent on tedious writing of boring unit tests. In this talk, I will share tips and tricks on how to structure your code to maximize benefits from using a static analyzer. You won’t stop writing unit tests, but you will focus your efforts on areas where they count the most.

Comments

Comments are closed.

Great examples irl and code.

Great talk about testing and application structure but I was expecting an intro into an automated CI strategy using PHPstan.

Matt Griffith at 11:23 on 3 May 2019

Good informative talk. Would have liked a little more detail about getting started with PHPstan. Great examples for once you are setup though.

Roger Creasy at 11:48 on 3 May 2019

Great talk with some good examples of bad and good code. Ondřej does a great job of explaining why some code is bad. I am taking back some things I need to change in my code and how to better use a static anylyzer in my workflow.

Jim Wigginton at 13:06 on 3 May 2019

Good talk! A bit opinionated, just as Jeffrey Wray's infamous Visual Debt video was, but I think opinionated is good as they can be thought provoking, foster discussion, etc. Thanks!

Scott C Sims at 13:08 on 3 May 2019

Nice code examples!

Provided some good basic things you can start doing today to make your code better. Would have been nice to see some examples, but otherwise a great overview and "sell" of why you need PHPStan.

Sarah Carroll at 15:19 on 3 May 2019

I really liked the code examples given. The talk was full of useful examples and best practices. The examples gave me some ideas of things I want to refactor in my own code. I liked how the talk pointed out where using static analysis is helpful for finding issues with wiring code and unit tests are super important for testing the business logic. I would have liked to see a little bit more hands on about how to run PHPStan/set it up in my own pipelines. Overall, the examples given were really helpful! Looking forward to reading the article mentioned at the end of the talk.

Philip at 19:36 on 3 May 2019

Great

Omni Adams at 13:04 on 4 May 2019

A great introduction to how static analysis can both augment and in some cases replace your normal testing strategy.