Home >Backend Development >PHP Tutorial >How to Avoid Adding New Code that Uses Deprecated Code?

How to Avoid Adding New Code that Uses Deprecated Code?

PHPz
PHPzOriginal
2024-07-17 09:27:38556browse

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() {
}

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.

The above is the detailed content of How to Avoid Adding New Code that Uses Deprecated Code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn