Skip to content

Instantly share code, notes, and snippets.

@Flower7C3
Created June 26, 2018 21:45
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 Flower7C3/20126340a227a807e9f318deb81b2799 to your computer and use it in GitHub Desktop.
Save Flower7C3/20126340a227a807e9f318deb81b2799 to your computer and use it in GitHub Desktop.
Search errors in PHP classes annotations with Symfony ClassMapGenerator, ReflectionClass and ReflectionMethod.
<?php
use Symfony\Component\ClassLoader\ClassMapGenerator;
$loader = require_once __DIR__ . '/../app/bootstrap.php.cache';
require_once __DIR__ . '/../app/AppKernel.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$kernel->boot();
$container = $kernel->getContainer();
$errors = [];
$classes = ClassMapGenerator::createMap(__DIR__ . '/../src/');
foreach ($classes as $className => $classPath) {
$reflectionClass = new \ReflectionClass($className);
$classAnnotations = $container->get('annotation_reader')->getClassAnnotations($reflectionClass);
if (!is_array($classAnnotations)) {
$errors[$className] = gettype($classAnnotations);
}
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
$methodAnnotations = $container->get('annotation_reader')->getMethodAnnotations($reflectionMethod);
if (!is_array($methodAnnotations)) {
$methodName = $reflectionMethod->getName();
$errors[$className . '.' . $methodName] = gettype($methodAnnotations);
}
}
}
if (empty($errors)) {
echo 'No errors found!' . PHP_EOL;
} else {
echo 'Found ' . count($errors) . ' errors:' . PHP_EOL;
var_dump($errors);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment