Skip to content

UndefinedClass ​

error native on by default

Group: Probable bugs

Report a class, interface, trait or enum name that nothing declares, used where PHP must load it: not the project, not an installed Composer package and not PHP or a known extension. Such code throws an Error when it runs; the name usually has a typo or a missing use import. The rule needs a complete view of the code the project can load, so it reports nothing unless every Composer dependency is installed.

Example ​

php
<?php
namespace App;

use App\Model\User;

$user = new Model\Usr();
class Admin extends BaseUser {}

Reported:

  • line 6: Fix the name or install the package declaring App\Model\Usr.
  • line 7: Fix the name or install the package declaring App\BaseUser.

Detection ​

  • A name PHP loads when the code runs or the class is declared: the class of a new, of a static method call, of a static property access or of a class constant access other than ::class; a parent class, an implemented or extended interface; a used trait. Names resolve with the file's namespace and imports; self, static and parent are skipped.
  • No class-like of that name is declared by the analyzed project, its installed packages, PHP or the extensions the stubs describe, at any PHP version, directly or through a class_alias() call with literal names.
  • D3. The project's world is complete, as for UndefinedFunction: composer.json and the vendor directory's composer/installed.json exist; every package the project requires, suggests or requires for development is installed (or replaced or provided by an installed package); every installed package that autoloads code was indexed; every classmap entry, of the project or of a package, was read whole (under the analyzed paths, with no directory or loadable file left out); every files entry was read; and every PHP extension the project requires, suggests or requires for development, or an installed package requires (ext-…), is one the stubs describe.

Exceptions (no report) ​

  • A project without Composer metadata, or whose installed packages do not match its requirements (D3): the class may come from code that was not seen.
  • A file that asks for the class by name with class_exists(), interface_exists(), trait_exists() or enum_exists() (a literal name or a ::class fetch), or a name that a check about it guards: a branch whose condition makes the check (a conditional declaration or guarded code), or an earlier check that guards by its result: an early exit (if (!extension_loaded('x')) { return; }), a short circuit (defined('X') or die()), an else arm, or a switch (true) case or match (true) arm. Such a check counts only when it checks the PHP version, the name itself (method_exists() included), or an extension or constant whose name or leading word is the leading word of the name (extension_loaded('acme'), defined('ACME_READY') for Acme\Handle or AcmeHandle), or an extension whose name starts the name (extension_loaded('openacme') for OpenAcmeSign). A check whose result guards nothing (stored and left unread) does not count. In an instance method, a check the class's constructor makes, throwing or exiting when it fails, guards the whole method: no instance exists without what it checks.
  • A namespaced name in a namespace that a PHP extension uses (such as MongoDB\ or Swoole\), and a name under a PSR-4 or PSR-0 prefix whose code the analysis did not read: a directory outside the analyzed paths, or, below it, a directory or a file left out (excluded by the configuration, hidden, symlinked, a test directory of a package, or too large to analyze). Below a PSR-4 directory only the namespace of what was left out is concerned: the one a skipped directory maps, with its sub-namespaces, or the one holding a skipped file, with its sub-namespaces too, since the file may declare anything there once loaded. A global name whose leading word (up to its first underscore or its second capitalized word, an acronym counting as one word: Sphinx of SphinxClient, PDO of PDOStatement) starts no class-like PHP or a known extension declares (Yac, SphinxClient): it may belong to an extension the stubs lack. A global name with the leading word of a known class (DateTim) is still reported.
  • Any name when an indexed file calls class_alias() with computed names, which may declare any alias, global (framework facades) or namespaced.
  • A name in a namespace whose package registers an autoloader, in that namespace, from a file Composer loads (PHPStan's PHPStan\): it may load classes from where the analysis does not look. When that autoloader loads a phar, any name in a file written for the package (lying in, importing from, or extending or implementing a fully qualified name of that namespace, such as a PHPStan extension), which runs within the package and the libraries it bundles. A name starting with a prefix, of three characters at least, that the callback such an autoloader registers compares its class name with (strpos($class, 'RectorPrefix') === 0), wherever it is registered: Rector's loads its prefixed copies so. The callback is a closure, or a function or method that the registration names exactly ([$this, 'load'], parent::load(...), 'Loader::load'), declared in any analyzed file, a method possibly taken from a trait (under an alias too) or a parent class. A class known only when the code runs counts as each class it may be: $this and static as the class and its subclasses (in a trait, as the classes using it and theirs), except that through $this a private method the class declares stays the one called; self in a trait as the classes using it; and parent in a trait as their parents.
  • Names PHP does not load: instanceof and catch (they compare names; libraries use them for optional dependencies), parameter, return and property types (checked only against a value, and used for optional dependencies), ::class (it only spells the name), attributes (loaded only when read through reflection), imports and PHPDoc.
  • Dynamic class names (new $class, $class::create()) and names in strings.

Fix ​

None: the intended class cannot be guessed.

PHP versions ​

All versions. A class PHP declares in another version than the configured one counts as declared; other rules report version mismatches.

Configure ​

In custos.json:

json
{
  "rules": {
    "UndefinedClass": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore UndefinedClass

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.