DEV Community

Cover image for How to Avoid Adding New Code that Uses Deprecated Code?
Henrik Gemal
Henrik Gemal

Posted on

How to Avoid Adding New Code that Uses Deprecated Code?

Spring cleaning your code? Developers are constantly improving code and adding new features. Sometimes, this includes deprecating older code as newer, faster alternatives become available. However, it's not always feasible to immediately update all instances where the deprecated code is used.

At DinnerBooking, we've tackled this challenge using PHPStan. Hereโ€™s how:

Mark Deprecated Code

First, ensure all deprecated code is clearly marked so that static code analyzers like PHPStan can identify it. Typically, it looks like this:

/**
 * @deprecated
 */
function count() {
}
Enter fullscreen mode Exit fullscreen mode

Install PHPStan Deprecation Plugin

Now install the PHPStan deprecation plugin from GitHub.

Generate a PHPStan Baseline

Generate a baseline that identifies all instances of deprecated code. You can do this by adding --generate-baseline to your PHPStan command. The baseline is saved in phpstan-baseline.neon.

Integrate with CI

Integrate this baseline into your CI pipeline to ensure that no new code referencing deprecated code is introduced.

By following these steps, we ensure that our codebase remains clean and maintainable, preventing the addition of new code that relies on deprecated functions.

Top comments (0)