feature: Phan Plugin
This commit is contained in:
parent
f8217d837f
commit
0923f86a09
|
@ -413,6 +413,7 @@ PHP Censor',
|
|||
'package_build' => 'Package Build',
|
||||
'pdepend' => 'PDepend',
|
||||
'pgsql' => 'PostgreSQL',
|
||||
'phan' => 'Phan',
|
||||
'phar' => 'Phar',
|
||||
'phing' => 'Phing',
|
||||
'php_cs_fixer' => 'PHP Coding Standards Fixer',
|
||||
|
|
|
@ -414,6 +414,7 @@ PHP Censor',
|
|||
'package_build' => 'Package Build',
|
||||
'pdepend' => 'PDepend',
|
||||
'pgsql' => 'PostgreSQL',
|
||||
'phan' => 'Phan',
|
||||
'phar' => 'Phar',
|
||||
'phing' => 'Phing',
|
||||
'php_cs_fixer' => 'PHP Coding Standards Fixer',
|
||||
|
|
|
@ -397,6 +397,7 @@ PHP Censor',
|
|||
'package_build' => 'Package Build',
|
||||
'pdepend' => 'PDepend',
|
||||
'pgsql' => 'PostgreSQL',
|
||||
'phan' => 'Phan',
|
||||
'phar' => 'Phar',
|
||||
'phing' => 'Phing',
|
||||
'php_cs_fixer' => 'PHP Coding Standards Fixer',
|
||||
|
|
167
src/Plugin/Phan.php
Normal file
167
src/Plugin/Phan.php
Normal file
|
@ -0,0 +1,167 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Plugin;
|
||||
|
||||
use PHPCensor\Builder;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Plugin;
|
||||
use PHPCensor\Model\BuildError;
|
||||
|
||||
/**
|
||||
* Launch Phan.
|
||||
*/
|
||||
class Phan extends Plugin
|
||||
{
|
||||
/**
|
||||
* Ignore directories.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $ignore;
|
||||
|
||||
/**
|
||||
* @var string Location on the server where the files are stored. Preferably in the webroot for inclusion
|
||||
* in the readme.md of the repository
|
||||
*/
|
||||
protected $location;
|
||||
|
||||
/**
|
||||
* @var string Directory which needs to be scanned
|
||||
*/
|
||||
protected $directory;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $allowedWarnings;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function pluginName()
|
||||
{
|
||||
return 'phan';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(Builder $builder, Build $build, array $options = [])
|
||||
{
|
||||
parent::__construct($builder, $build, $options);
|
||||
|
||||
$this->directory = isset($options['directory'])
|
||||
? $options['directory']
|
||||
: $this->builder->buildPath;
|
||||
|
||||
$this->location = $this->builder->buildPath.'phan_tmp';
|
||||
$this->ignore = isset($options['ignore']) && is_array($options['ignore']) ? $options['ignore'] : [];
|
||||
|
||||
if (empty($this->ignore) && is_array($this->builder->ignore)) {
|
||||
$this->ignore = $this->builder->ignore;
|
||||
}
|
||||
|
||||
$this->allowedWarnings = isset($options['allowed_warnings']) ? $options['allowed_warnings'] : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes Phan.
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
if (!file_exists($this->location)) {
|
||||
mkdir($this->location, (0777 & ~umask()), true);
|
||||
}
|
||||
|
||||
if (!is_writable($this->location)) {
|
||||
throw new \Exception(sprintf('The location %s is not writable or does not exist.', $this->location));
|
||||
}
|
||||
|
||||
// Find PHP files in a file
|
||||
$cmd = 'find -L %s -type f -name "**.php"';
|
||||
|
||||
foreach ($this->ignore as $ignore) {
|
||||
$cmd .= ' | grep -v '.$ignore;
|
||||
}
|
||||
|
||||
$cmd .= ' > %s';
|
||||
|
||||
$this->builder->executeCommand($cmd, $this->directory, $this->location.'/phan.in');
|
||||
|
||||
$phan = $this->findBinary('phan');
|
||||
|
||||
// Launch Phan on PHP files with json output
|
||||
$cmd = $phan.' -f %s -i -m json -o %s';
|
||||
|
||||
$this->builder->executeCommand($cmd, $this->location.'/phan.in', $this->location.'/phan.out');
|
||||
|
||||
$warningCount = $this->processReport(file_get_contents($this->location.'/phan.out'));
|
||||
|
||||
$this->build->storeMeta('phan-warnings', $warningCount);
|
||||
|
||||
$success = true;
|
||||
|
||||
if ($this->allowedWarnings != -1 && $warningCount > $this->allowedWarnings) {
|
||||
$success = false;
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the Phan Json report.
|
||||
*
|
||||
* @param $jsonString
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function processReport($jsonString)
|
||||
{
|
||||
$json = json_decode($jsonString, true);
|
||||
|
||||
if ($json === false || !is_array($json)) {
|
||||
$this->builder->log($jsonString);
|
||||
throw new \Exception('Could not process the report generated by Phan.');
|
||||
}
|
||||
|
||||
$warnings = 0;
|
||||
|
||||
foreach ($json as $data) {
|
||||
$this->build->reportError(
|
||||
$this->builder,
|
||||
'phan',
|
||||
$data['check_name']."\n\n".$data['description'],
|
||||
$this->severity($data['severity']),
|
||||
$data['location']['path'] ?? '??',
|
||||
$data['location']['lines']['begin'] ?? 0,
|
||||
$data['location']['lines']['end'] ?? 0
|
||||
);
|
||||
|
||||
$warnings++;
|
||||
}
|
||||
|
||||
return $warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform severity from Phan to PHP-Censor.
|
||||
*
|
||||
* @param int $severity
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function severity($severity)
|
||||
{
|
||||
if ($severity == 10) {
|
||||
return BuildError::SEVERITY_CRITICAL;
|
||||
}
|
||||
|
||||
if ($severity == 5) {
|
||||
return BuildError::SEVERITY_NORMAL;
|
||||
}
|
||||
|
||||
return BuildError::SEVERITY_LOW;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue