Deprecated Class
ID |
php.deprecated_class |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Php |
Tags |
code_smell, deprecation |
Description
Reports uses of a class declared in the same file and marked deprecated — either through a
@deprecated docblock tag or a PHP 8 #[Deprecated] attribute. Flagged uses include
instantiation (new Old()), inheritance (class X extends Old), static references
(Old::method(), Old::CONST, Old::$prop) and type hints (function f(Old $x)).
Detection is limited to classes declared in the same file; resolving deprecation across files is out of scope.
Rationale
A class marked deprecated is scheduled for removal or replacement. New code that instantiates, extends or otherwise depends on it is coupled to an outgoing API, so every such use is migration debt that has to be unwound later — often under time pressure when the class finally disappears. Surfacing the dependency early lets it be redirected to the supported replacement while the change is still cheap.
<?php
/** @deprecated use NewProcessor */
class OldProcessor {}
#[Deprecated]
class LegacyHandler {}
class NewProcessor {}
class Consumer extends OldProcessor // FLAW - extends a deprecated class
{
public function build(OldProcessor $p): void // FLAW - type hint on a deprecated class
{
$a = new OldProcessor(); // FLAW - instantiates a deprecated class
$b = new LegacyHandler(); // FLAW - deprecated through an attribute
$c = new NewProcessor(); // OK - the supported replacement
}
}