UndefinedFunction
error native on by defaultGroup: Probable bugs
Report a call to a function that nothing declares: not the project, not an installed Composer package and not PHP or a known extension. Such a call throws an Error when it runs; the name usually has a typo or lacks its use function 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;
str_contians('haystack', 'needle');
Util\slugify('Title');Reported:
- line 4: Fix the name or install the package declaring str_contians().
- line 5: Fix the name or install the package declaring App\Util\slugify().
Detection
- A call to a function written as a name (
foo(),\Lib\foo(),Lib\foo()), resolved with the file's namespace and imports. An unqualified call in a namespace may reach the namespaced function or the global one; neither may exist. - No function of that name is declared by the analyzed project, its installed packages, PHP or the extensions the stubs describe, at any PHP version.
- D3. The project's world is complete:
composer.jsonand the vendor directory'scomposer/installed.jsonexist; 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): a function may come from code that was not seen.
- A file that asks for the function by name with
function_exists()oris_callable()(a literal name), or a call 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 aswitch (true)case ormatch (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')forAcme\open()oracme_open()), or an extension whose name starts the name (extension_loaded('openacme')foropenacme_sign()). 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\orSwoole\), and a namespaced 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 them too. - A global name whose leading word, up to its first underscore, starts no function PHP or a known extension declares (
newext_open()): it may belong to an extension the stubs lack. A global name without an underscore, or with the prefix of a known family (str_contians()), is still reported. - Any name in a file written for a package whose autoloader, registered in a namespace from a file Composer loads, loads a phar (PHPStan's
PHPStan\): a file lying in, importing from, or extending or implementing a fully qualified name of that namespace, such as a PHPStan extension, runs within the package and the libraries it bundles. An autoloader loads classes only, so it hides no other function. - Dynamic calls (
$fn(),call_user_func('foo')) and names in strings.
Fix
None: the intended function cannot be guessed.
PHP versions
All versions. A function PHP declares in another version than the configured one counts as declared; UnavailableFunction reports a call to a function introduced after the configured version.
Configure
In custos.json:
json
{
"rules": {
"UndefinedFunction": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore UndefinedFunction