Skip to content

Instantly share code, notes, and snippets.

@amnuts
Created January 6, 2020 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnuts/f28c1c975f2031014536243e50371248 to your computer and use it in GitHub Desktop.
Save amnuts/f28c1c975f2031014536243e50371248 to your computer and use it in GitHub Desktop.
Using Better Reflection to get details on classes in multiple directories
<?php
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
require __DIR__ . '/vendor/autoload.php';
$refSubscriptions = [];
$astLocator = (new BetterReflection())->astLocator();
$directoriesSourceLocator = new DirectoriesSourceLocator([__DIR__ . '/src/Components'], $astLocator);
$reflector = new ClassReflector($directoriesSourceLocator);
foreach ($reflector->getAllClasses() as $class) {
echo sprintf("%s (%s)\n", $class->getName(), $class->getNamespaceName());
foreach ($class->getProperties() as $prop) {
if (!$prop->isPublic()) {
continue;
}
$dockBlock = $prop->getDocComment();
$propStrings = $prop->getDocBlockTypeStrings();
$nullable = in_array('null', $propStrings);
$removable = strstr($dockBlock, '@removable') !== false;
if (preg_match('/@on (?P<class>.*)::(?P<property>.*)/i', $dockBlock, $matches)) {
$refSubscriptions[$matches['class']][$matches['property']] = [
'prop' => $prop->getName(),
'value' => ''
];
}
if (($key = array_search('null', $propStrings)) !== false) {
unset($propStrings[$key]);
}
echo sprintf("\t%s (%s) %s\n", $prop->getName(), join(', ', $propStrings), $nullable ? '🤫' : '');
foreach ($prop->getDocBlockTypes() as $propType) {
//var_dump($propType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment