Deprecated Method

ID

php.deprecated_method

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Php

Tags

code_smell, deprecation

Description

Reports calls to a method declared in the same file and marked deprecated — either through a @deprecated docblock tag or a PHP 8 #[Deprecated] attribute. Both instance calls ($obj→old()) and static calls (Class::old()) are inspected.

Because the receiver type is not statically known in PHP, the rule matches by simple method name and only reports a name when every declaration of that name in the file is deprecated, so a same-named non-deprecated method in another class is never flagged. Detection is limited to the current file.

Rationale

A method marked deprecated is scheduled for removal or behavioural change. Every remaining call is a code path that will break — or silently change meaning — when the method finally goes away. Flagging the call keeps the migration visible and lets it be redirected to the supported alternative while the change is still small.

<?php
class Repository
{
    /** @deprecated use findById() */
    public function load(int $id): void {}

    #[Deprecated]
    public function fetchAll(): array { return []; }

    public function findById(int $id): void {}
}

function run(Repository $repo): void
{
    $repo->load(1);       // FLAW - calls a deprecated method
    $repo->fetchAll();    // FLAW - deprecated through an attribute
    $repo->findById(1);   // OK - the supported replacement
}

Remediation

Call the replacement named in the deprecation notice. If the deprecated method is the only way to get the behaviour you need, raise the gap with the API owner before it is removed.