Skip to content

Instantly share code, notes, and snippets.

@ncuesta
Created February 5, 2011 22:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncuesta/812858 to your computer and use it in GitHub Desktop.
Save ncuesta/812858 to your computer and use it in GitHub Desktop.
PHPDoc blocks inspector for Symfony 2.0's components.
<?php
include_once __DIR__.'/autoload.php.dist';
/**
* PHPDoc block inspector for Symfony 2.0's classes.
* At the moment, this inspector tells which methods have missing PHPDoc blocks
* or have not been completely written (it checks the number of @param elements
* in the PHPDoc block against the number of parameters in every method).
*
* @author José Nahuel Cuesta Luengo <ncuesta at cespi.unlp.edu.ar>
*/
class PhpDocInspector
{
protected $_reflection_instance;
public function getClasses($component)
{
$classes = array();
$directory = new RecursiveDirectoryIterator(__DIR__.'/src/'.(str_replace('\\', '/', $component)));
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($regex as $file => $bla)
{
$class = preg_replace('/'. preg_quote(__DIR__.'/src/', '/').'/', '', $file);
$class = preg_replace('/\.php$/i', '', str_replace('/', '\\', $class));
$classes[] = $class;
}
return $classes;
}
public function inspect($component, $verbose = false)
{
$classes = $this->getClasses($component);
foreach ($classes as $class)
{
$verbose and print "Inspecting $class class:\n";
try
{
$reflector = new ReflectionClass($class);
}
catch (ReflectionException $exception)
{
echo "Class $class couldn't be found.\n";
continue;
}
$methods = array();
/* @var $method ReflectionMethod */
foreach ($reflector->getMethods() as $method)
{
if ($method->getDeclaringClass() != $reflector)
{
// We're only looking for methods declared inside $class
continue;
}
$doc_comment = $method->getDocComment();
$doc_params = preg_match_all('/\@param/', $doc_comment, $matches);
if ($method->getNumberOfParameters() != $doc_params || '' == trim($doc_comment))
{
if ($verbose)
{
echo sprintf(" * %s: Diff.\n", $method->getName());
}
else
{
$methods[] = $method->getName().'()';
}
}
else if ($verbose)
{
echo sprintf(" * %s: Ok.\n", $method->getName());
}
}
if (!$verbose && count($methods) > 0)
{
echo sprintf("%s: %s.\n", $class, implode(', ', $methods));
}
else if ($verbose)
{
echo "No methods found.\n";
}
}
}
}
if ($argc < 2)
{
die("Missing component.\n");
}
$component = $argv[1];
$verbose = $argc > 2 && (bool) $argv[2];
$inspector = new PhpDocInspector();
$inspector->inspect($component, $verbose);
@ncuesta
Copy link
Author

ncuesta commented Feb 5, 2011

This file should be placed right on the Symfony 2.0's root directory and invoked directly passing it at least 1 parameter: the name of Component to inspect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment