At work we’re using PHPStan to do a static analysis of our code, checking if the provided types match the required types. We are using the most restrictive setting that forces us to have type annotations for arrays. Recently, this led to an error when using Doctrine DBAL, using DriverManager::getConnection(). In this article I’ll explain how to use better type annotations for the parameter of DriverManager::getConnection(). Let’s say we have a test environment class that initializes a database connection, used by the tests for the database adapter.
Many PHP database libraries use a
while loop with assignment to iterate
over database results. In this article I’ll show why this pattern is so
pervasive, why it opens the door for errors and what you can use instead.
“Transposing” or “pivoting” rows to columns in a database query is a common task. These are my notes on how I evolved a SQL query to get the desired result.