2016-06-10 00:08:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace CodeInspector;
|
|
|
|
|
2016-06-13 21:33:18 +02:00
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
class ProjectChecker
|
|
|
|
{
|
2016-06-26 19:45:20 +02:00
|
|
|
/**
|
|
|
|
* Cached config
|
|
|
|
* @var Config|null
|
|
|
|
*/
|
|
|
|
protected static $config;
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
public static function check($debug = false)
|
2016-06-13 21:33:18 +02:00
|
|
|
{
|
2016-06-26 21:33:51 +02:00
|
|
|
if (!self::$config) {
|
|
|
|
self::$config = self::getConfigForPath(getcwd());
|
|
|
|
}
|
2016-06-26 19:45:20 +02:00
|
|
|
|
|
|
|
foreach (self::$config->getIncludeDirs() as $dir_name) {
|
2016-06-26 21:25:38 +02:00
|
|
|
self::checkDirWithConfig($dir_name, self::$config, $debug);
|
2016-06-13 21:33:18 +02:00
|
|
|
}
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
IssueBuffer::finish();
|
2016-06-13 21:33:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkDir($dir_name, $debug = false)
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2016-06-26 19:45:20 +02:00
|
|
|
if (!self::$config) {
|
|
|
|
self::$config = self::getConfigForPath($dir_name);
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-26 21:25:38 +02:00
|
|
|
self::checkDirWithConfig($dir_name, self::$config, $debug);
|
|
|
|
|
|
|
|
IssueBuffer::finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function checkDirWithConfig($dir_name, Config $config, $debug)
|
|
|
|
{
|
|
|
|
$file_extensions = $config->getFileExtensions();
|
|
|
|
$filetype_handlers = $config->getFiletypeHandlers();
|
2016-06-13 21:33:18 +02:00
|
|
|
|
|
|
|
/** @var RecursiveDirectoryIterator */
|
2016-06-28 20:28:45 +02:00
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_name));
|
2016-06-13 21:33:18 +02:00
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
$files = [];
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
|
|
|
if (!$iterator->isDot()) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$extension = $iterator->getExtension();
|
|
|
|
if (in_array($extension, $file_extensions)) {
|
|
|
|
$file_name = $iterator->getRealPath();
|
|
|
|
|
|
|
|
if ($debug) {
|
|
|
|
echo 'Checking ' . $file_name . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($filetype_handlers[$extension])) {
|
|
|
|
/** @var FileChecker */
|
|
|
|
$file_checker = new $filetype_handlers[$extension]($file_name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$file_checker = new FileChecker($file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_checker->check(true);
|
2016-06-13 21:33:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
public static function checkFile($file_name, $debug = false)
|
|
|
|
{
|
|
|
|
if ($debug) {
|
|
|
|
echo 'Checking ' . $file_name . PHP_EOL;
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
if (!self::$config) {
|
|
|
|
self::$config = self::getConfigForPath($file_name);
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
$extension = array_pop(explode('.', $file_name));
|
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
$filetype_handlers = self::$config->getFiletypeHandlers();
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
if (isset($filetype_handlers[$extension])) {
|
|
|
|
/** @var FileChecker */
|
2016-06-28 20:28:45 +02:00
|
|
|
$file_checker = new $filetype_handlers[$extension]($file_name);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-28 20:28:45 +02:00
|
|
|
$file_checker = new FileChecker($file_name);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
$file_checker->check(true);
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
IssueBuffer::finish();
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-26 19:45:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Config object from an XML file.
|
|
|
|
* Searches up a folder hierachy for the most immediate config.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
protected static function getConfigForPath($path)
|
|
|
|
{
|
|
|
|
$dir_path = realpath($path) . '/';
|
|
|
|
|
|
|
|
if (!is_dir($dir_path)) {
|
|
|
|
$dir_path = dirname($dir_path) . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$maybe_path = $dir_path . Config::DEFAULT_FILE_NAME;
|
|
|
|
|
|
|
|
if (file_exists($maybe_path)) {
|
|
|
|
$config = \CodeInspector\Config::loadFromXML($maybe_path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir_path = preg_replace('/[^\/]+\/$/', '', $dir_path);
|
|
|
|
}
|
|
|
|
while ($dir_path !== '/');
|
|
|
|
|
|
|
|
if (!$config) {
|
|
|
|
throw new Exception\ConfigException('Config not found for path ' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
2016-06-26 21:33:51 +02:00
|
|
|
|
|
|
|
public static function setConfigXML($path_to_config)
|
|
|
|
{
|
|
|
|
if (!file_exists($path_to_config)) {
|
|
|
|
throw new Exception\ConfigException('Config not found at location ' . $path_to_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$config = \CodeInspector\Config::loadFromXML($path_to_config);
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|