rikudou / reflection-file
Reflection file implementation
Installs: 10 962
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.3 | ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- php-coveralls/php-coveralls: ^2.1
- phpstan/phpstan: ^0.12.63
- phpunit/phpunit: ^9.5
README
Allows you to get info about PHP file.
Usage
<?php use Rikudou\ReflectionFile; use Rikudou\Exception\ReflectionException; try { $reflection = new ReflectionFile("/path/to/file.php"); } catch (ReflectionException $exception) { var_dump("The file does not exist!"); } // true if the file contains a class $reflection->containsClass(); // true if the file contains any namespace $reflection->containsNamespace(); // true if the file contains any inline html, e.g. content that is not php $reflection->containsInlineHtml(); // true if the file contains any php code $reflection->containsPhpCode(); // true if the file contains echo or print statements $reflection->printsOutput(); // true if the file contains functions (not methods) $reflection->containsFunctions(); try { // returns the namespace as a string, throws exception if the // file does not contain a namespace $reflection->getNamespace(); } catch (ReflectionException $exception) { var_dump("The class does not contain a namespace!"); } // the previous example can be rewritten as follows if($reflection->containsNamespace()) { $reflection->getNamespace(); } try { // returns instance of \ReflectionClass if the file contains a class // otherwise throws an exception $reflection->getClass(); } catch (ReflectionException $exception) { var_dump("The class does not contain a class!"); } // returns array with \ReflectionFunction instances // can throw exception if the functions could not be found // which happnes when the file is not included foreach ($reflection->getFunctions() as $function) { var_dump("Function name: {$function->getName()}"); }
Limitations
The ReflectionFile
cannot parse multiple classes or namespaces.
If the file contains more than one class (or namespace) the latest one will be returned.