phpdocumentor / reflection
Reflection library to do Static Analysis for PHP Projects
Installs: 6 446 574
Dependents: 104
Suggesters: 8
Security: 0
Stars: 119
Watchers: 12
Forks: 52
Open Issues: 12
Requires
- php: 8.1.*|8.2.*|8.3.*|8.4.*
- nikic/php-parser: ~4.18 || ^5.0
- phpdocumentor/reflection-common: ^2.1
- phpdocumentor/reflection-docblock: ^5
- phpdocumentor/type-resolver: ^1.2
- symfony/polyfill-php80: ^1.28
- webmozart/assert: ^1.7
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- doctrine/coding-standard: ^12.0
- mikey179/vfsstream: ~1.2
- mockery/mockery: ~1.6.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.8
- phpstan/phpstan-webmozart-assert: ^1.2
- phpunit/phpunit: ^10.0
- psalm/phar: ^5.24
- rector/rector: ^1.0.0
- squizlabs/php_codesniffer: ^3.8
- 6.x-dev
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.1
- 5.1.0
- 5.0.1
- 5.0.0
- 4.x-dev
- 4.0.1
- 4.0.0
- 4.0.0-beta4
- 4.0.0-beta3
- 4.0.0-beta2
- 4.0.0-beta
- 4.0.0-alpha6
- 4.0.0-alpha5
- 4.0.0-alpha4
- 4.0.0-alpha3
- 4.0.0-alpha2
- 4.0.0-alpha1
- 3.0.x-dev
- 3.0.1
- 3.0.0
- 2.0.0-alpha2
- 2.0.0-alpha1
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-feature/php8.4
- dev-composer/bump-php-parser
- dev-feature/expressions-in-arguments
- dev-feature/php8-attributes
- dev-feature/220_readonly_properties
This package is auto-updated.
Last update: 2024-11-22 15:14:30 UTC
README
Reflection
Using this library it is possible to statically reflect one or more files and create an object graph representing your application's structure, including accompanying in-source documentation using DocBlocks.
The information that this library provides is similar to what the (built-in) Reflection extension of PHP provides; there are however several advantages to using this library:
- Due to its Static nature it does not execute procedural code in your reflected files where Dynamic Reflection does.
- Because the none of the code is interpreted by PHP (and executed) Static Reflection uses less memory.
- Can reflect complete files
- Can reflect a whole project by reflecting multiple files.
- Reflects the contents of a DocBlock instead of just mentioning there is one.
- Is capable of analyzing code written for any PHP version (starting at 5.2) up to and including your installed PHP version.
Features
- [Creates an object graph] containing the structure of your application much like a site map shows the structure of a website.
- Can read and interpret code of any PHP version starting with 5.2 up to and including your currently installed version of PHP.
- Due it's clean interface it can be in any application without a complex setup.
Installation
In order to inspect a codebase you need to tell composer to include the phpdocumentor/reflection
package. This
can easily be done using the following command in your command line terminal:
composer require phpdocumentor/reflection:~5.0
After the installation is complete no further configuration is necessary and you can immediately start using it.
Basic Usage
This Reflection library uses PSR-4 and it is recommended to use a PSR-4 compatible autoloader to load all the files containing the classes for this library.
An easy way to do this is by including the composer autoloader as shown here:
include 'vendor/autoload.php';
Once that is done you can use the createInstance()
method of the \phpDocumentor\Reflection\Php\ProjectFactory
class to instantiate a new project factory and
pre-configure it with sensible defaults. Optional you can specify the parser version that shall be used as an argument of createInstance()
.
By default the php7 parser is prefered. And php5 is used as a fallback. See the documentation of phpparser for more info.
$projectFactory = \phpDocumentor\Reflection\Php\ProjectFactory::createInstance();
At this point we are ready to analyze your complete project or just one file at the time. Just pass an array of file paths to the create
method of the project factory.
$projectFiles = [new \phpDocumentor\Reflection\File\LocalFile('tests/example.file.php')];
$project = $projectFactory->create('My Project', $projectFiles);
When the process is ready a new object of type phpDocumentor\Reflection\Php\Project
will be returned that
contains a complete hierarchy of all files with their classes, traits and interfaces (and everything in there), but also
all namespaces and packages as a hierarchical tree.
See the example script for a detailed and commented example