PHP Manual
Senior Developer

Inappropriate use of the Garbage Collector

11. 02. 2023

You are a developer of a large legacy application, into which you are gradually introducing PHPStan. You start with level 0, which is quite challenging, but eventually you get it right. You move on to the next levels, where one part of your code starts reporting an unused $lock variable that you should remove.

The code looks like this:

public function processOrder(int $orderId): void
$lock = Lock::createLock('order-' . $orderId);
// There's some logic here...

You tell yourself that there must be a lock stored in the variable that someone forgot to release later, or maybe it's happening inside other methods that are called later. So you decide to remove the unused variable, keeping only the call to the static method that creates the lock.

Can this decision cause a critical error?

If so, why, and how might the original mechanism have worked?

If not, why not, and how do you know that this is always a safe operation?

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

All systems normal.