website/src/user-guide/discovering-symbols.md
PHPStan needs to be able to locate symbols (classes, functions, constants) used in the analysed codebase. By default, it looks for them in these two places:
This covers most common needs.
However, there are some advanced scenarios that might require some additional configuration.
If your project uses some code that isn't part of your Composer dependencies, but you don't wish to analyse it, you can take advantage of scanFiles and scanDirectories config options:
parameters:
scanFiles:
- Foo.class.php
scanDirectories:
- classes
Relative paths in the scanFiles and scanDirectories keys are resolved based on the directory of the config file is in.
As of PHPStan 1.7.0 nothing special is required to analyse code with global constants.
<details> <summary class="text-blue-500 font-bold">Show obsolete instructions for older PHPStan versions</summary>Global constants used in the analysed code need to be defined in bootstrap files.
Create a file that looks like this:
<?php
define('MY_CONSTANT', 1);
And add it to your configuration file:
parameters:
bootstrapFiles:
- constants.php
Please note that bootstrap files will actually be executed by the PHP runtime.
</details>This is similar to global constants above. Class aliases used in the analysed code need to be defined in bootstrap files.
Create a file that looks like this:
<?php
class_alias(\Foo::class, 'Bar');
And add it to your configuration file:
parameters:
bootstrapFiles:
- classAliases.php
Please note that bootstrap files will actually be executed by the PHP runtime.
If you're using some other autoloader than the one in Composer, PHPStan can take advantage of it to discover files with autoloaded classes.
You can register the custom autoloader in two ways:
--autoload-file|-a on the command line.bootstrapFiles option:parameters:
bootstrapFiles:
- my_autoloader.php
Please note that the file with the autoloader will actually be executed by the PHP runtime.
When you install PHPUnit with Phive, you might encounter similar errors when analysing your tests with PHPStan:
App\MyTest extends unknown class PHPUnit\Framework\TestCase.App\MyTest::assertIsArray().That's because PHPStan does not see PHPUnit classes from inside phpunit.phar.
The way to fix it is to put phpunit.phar in bootstrapFiles in your configuration file:
parameters:
bootstrapFiles:
- tools/phpunit.phar
If you have difficulties discovering symbols from PHAR files and the above PHPUnit trick does not work for you, try this instead:
parameters:
scanDirectories:
- phar://%currentWorkingDirectory%/bin/robo.phar
PHPStan provides a tip with a link to this article whenever it encounters symbol discovery errors.
To disable that tip, add the following to your configuration:
parameters:
tips:
discoveringSymbols: false