|
| 1 | +--- |
| 2 | +title: Discovering Symbols |
| 3 | +showBanner: false |
| 4 | +--- |
| 5 | + |
| 6 | +<div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-4" role="alert"> |
| 7 | + |
| 8 | +Discovering Symbols is a new feature in PHPStan 0.12.26. Make sure to upgrade to the latest version and take advantage of the latest features! |
| 9 | + |
| 10 | +Looking for the deprecated [autoloading](/user-guide/autoloading) instead? |
| 11 | + |
| 12 | +</div> |
| 13 | + |
| 14 | +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: |
| 15 | + |
| 16 | +* Analysed paths (files and directories) passed as [command line arguments](/user-guide/command-line-usage) and in the [configuration file](/config-reference#analysed-files). |
| 17 | +* Composer dependencies of the analysed project |
| 18 | + |
| 19 | +This covers most common needs. |
| 20 | + |
| 21 | +However, there are some advanced scenarios that might require some additional configuration. |
| 22 | + |
| 23 | +Third party code outside of Composer dependencies |
| 24 | +--------------------------- |
| 25 | + |
| 26 | +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: |
| 27 | + |
| 28 | +```yaml |
| 29 | +parameters: |
| 30 | + scanFiles: |
| 31 | + - Foo.class.php |
| 32 | + scanDirectories: |
| 33 | + - classes |
| 34 | +``` |
| 35 | +
|
| 36 | +Relative paths in the `scanFiles` and `scanDirectories` keys are resolved based on the directory of the config file is in. |
| 37 | + |
| 38 | +Global constants |
| 39 | +--------------------------- |
| 40 | + |
| 41 | +Global constants used in the analysed code need to be defined in bootstrap files. |
| 42 | + |
| 43 | +Create a file a that looks like this: |
| 44 | + |
| 45 | +```php |
| 46 | +<?php |
| 47 | +
|
| 48 | +define('MY_CONSTANT', 1); |
| 49 | +``` |
| 50 | + |
| 51 | +And add it to your [configuration file](/config-reference): |
| 52 | + |
| 53 | +```yaml |
| 54 | +parameters: |
| 55 | + bootstrapFiles: |
| 56 | + - constants.php |
| 57 | +``` |
| 58 | + |
| 59 | +Please note that bootstrap files will actually be executed by the PHP runtime. |
| 60 | + |
| 61 | +Class aliases |
| 62 | +--------------------------- |
| 63 | + |
| 64 | +This is similar to global constants above. Class aliases used in the analysed code need to be defined in bootstrap files. |
| 65 | + |
| 66 | +Create a file a that looks like this: |
| 67 | + |
| 68 | +```php |
| 69 | +<?php |
| 70 | +
|
| 71 | +class_alias(\Foo::class, 'Bar'); |
| 72 | +``` |
| 73 | + |
| 74 | +And add it to your [configuration file](/config-reference): |
| 75 | + |
| 76 | +```yaml |
| 77 | +parameters: |
| 78 | + bootstrapFiles: |
| 79 | + - classAliases.php |
| 80 | +``` |
| 81 | + |
| 82 | +Please note that bootstrap files will actually be executed by the PHP runtime. |
| 83 | + |
| 84 | +Custom autoloader |
| 85 | +--------------------------- |
| 86 | + |
| 87 | +If you're using some other autoloader than the one in Composer, PHPStan can take advantage of it to discover files with autoloaded classes. |
| 88 | + |
| 89 | +You can register the custom autoloader in two ways: |
| 90 | + |
| 91 | +1) By passing the PHP file that registers the autoloader as [`--autoload-file|-a` on the command line](/user-guide/command-line-usage#--autoload-file|-a). |
| 92 | +2) By using the `bootstrapFiles` option: |
| 93 | + |
| 94 | +```yaml |
| 95 | +parameters: |
| 96 | + bootstrapFiles: |
| 97 | + - my_autoloader.php |
| 98 | +``` |
| 99 | + |
| 100 | +Please note that the file with the autoloader will actually be executed by the PHP runtime. |
0 commit comments