We have recently deployed a new version of PHP Analyzer. This minor version upgrade contains mostly bug fixes and a couple of new features/improvements.
One of the major new features in this upgrade is deadlock detection. This is mainly intended for CLI scripts where such deadlocks might go unnoticed for a while. PHP Analyzer now checks the exit conditions of loops for whether they are either always satisfied, or can never be satisfied.
Let’s have a look at an example:
function isMergeable(GitHubRepository $repository)
{
$i = 0;
do {
if ($i > 0) {
$waitTime = pow(2, $i) * 1;
sleep($waitTime);
}
$prDetails = $this->api->getPrDetails($repository->getLogin(), $repository->getName(), $prNumber);
if ($prDetails['mergeable'] === true) {
return true;
}
} while ($i < 3);
return false;
}
This code fragment comes from our code-base. It is run as a background process
whenever GitHub notifies us of a pull-request. We pull the GitHub API to check
whether a pull-request is mergeable and return true
or false
. However,
you might have spotted a small mistake in the loop, we are actually missing a $i++
or similar to increase the counter, and as such the condition $i < 3
is
always true.
This did go unnoticed for a while since the actual result, i.e. creating an inspection when the pull-request was mergeable or ignoring the pull-request if it was not mergeable was achieved. However, we saw some errors because jobs exceeded their maximum runtime which eventually led us to find this error.
Even if the cost of this error was not high in this case, PHP Analyzer now
makes sure that we do not slip in any unsatisfiable loops. This check is enabled
by default if you are using the tools
configuration, and can be enabled via
the checks
configuration as follows:
# .scrutinizer.yml
# Only add this if you already have a "checks" section.
checks:
php:
deadlock_detection_in_loops: true
This release also contains a couple of bug fixes, and minor improvements. Some of the highlights below:
... and some more improvements, see the full changelog.
Thanks, and happy inspecting! :)