Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a way to executing analyzes from plugin and to customize reports #202

Merged
merged 4 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ PhpMetrics can parse PHP code from **PHP 5.3 to PHP 7.x**.
You can parse PHP7 code, but your interpreter will detect syntax errors in your code. To prevent this, please
use the `--ignore-errors` option.

## Extensions / Plugins

PhpMetrics supports plugins. Use the `--plugins=<path-of-plugin1>` option, or update your config file as following:

```
plugins:
- <path-of-plugin1.php`

```

Official plugins:

+ [Symfony2](https://github.com/phpmetrics/PhpMetricsSymfony2Extension)

## Conditions of failure

Customizing the conditions of failure is very easy with the`--failure-condition` option. For example:
Expand Down
92 changes: 92 additions & 0 deletions src/Hal/Application/Command/Job/QueueAnalyzeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* (c) Jean-François Lépine <https://twitter.com/Halleck45>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Hal\Application\Command\Job;
use Hal\Application\Extension\ExtensionService;
use Hal\Application\Formater\Chart;
use Hal\Application\Formater\Details;
use Hal\Application\Formater\Summary;
use Hal\Application\Formater\Violations;
use Hal\Application\Rule\Validator;
use Hal\Application\Score\Scoring;
use Hal\Component\Aggregator\DirectoryAggregatorFlat;
use Hal\Component\Bounds\BoundsInterface;
use Hal\Component\Config\ConfigurationInterface;
use Hal\Component\File\Finder;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Queue factory
*
* @author Jean-François Lépine <https://twitter.com/Halleck45>
*/
class QueueAnalyzeFactory
{

/**
* @var ConfigurationInterface
*/
private $config;

/**
* @var OutputInterface
*/
private $output;

/**
* @var InputInterface
*/
private $input;

/**
* @var ExtensionService
*/
private $extensionsService;

/**
* Constructor
*
* @param InputInterface $input
* @param OutputInterface $output
* @param ConfigurationInterface $config
* @param ExtensionService $extensionService
*/
public function __construct(InputInterface $input, OutputInterface $output, ConfigurationInterface $config, ExtensionService $extensionService)
{
$this->config = $config;
$this->input = $input;
$this->output = $output;
$this->extensionsService = $extensionService;
}

/**
* Factory queue
*
* @param Finder $finder
* @param BoundsInterface $bounds
* @return Queue
*/
public function factory(Finder $finder, BoundsInterface $bounds) {
$rules = $this->config->getRuleSet();
$validator = new Validator($rules);

// jobs queue planning
$queue = new Queue;
$queue
->push(new DoAnalyze($this->output, $finder, $this->config->getPath()->getBasePath(), !$this->input->getOption('without-oop'), $this->config->getIgnoreErrors()))
->push(new SearchBounds($this->output, $bounds))
->push(new DoAggregatedAnalyze($this->output, new DirectoryAggregatorFlat($this->input->getOption('level'))))
->push(new CalculateScore(new Scoring($bounds)))
;

return $queue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

namespace Hal\Application\Command\Job;
use Hal\Application\Extension\ExtensionService;
use Hal\Application\Extension\extensionsService;
use Hal\Application\Extension\Repository;
use Hal\Application\Formater\Chart;
use Hal\Application\Formater\Details;
use Hal\Application\Formater\Summary;
Expand All @@ -25,7 +28,7 @@
*
* @author Jean-François Lépine <https://twitter.com/Halleck45>
*/
class QueueFactory
class QueueReportFactory
{

/**
Expand All @@ -43,18 +46,25 @@ class QueueFactory
*/
private $input;

/**
* @var ExtensionService
*/
private $extensionsService;

/**
* Constructor
*
* @param InputInterface $input
* @param OutputInterface $output
* @param ConfigurationInterface $config
* @param ExtensionService $extensionsService
*/
public function __construct(InputInterface $input, OutputInterface $output, ConfigurationInterface $config)
public function __construct(InputInterface $input, OutputInterface $output, ConfigurationInterface $config, ExtensionService $extensionsService)
{
$this->config = $config;
$this->input = $input;
$this->output = $output;
$this->extensionsService = $extensionsService;
}

/**
Expand All @@ -71,18 +81,14 @@ public function factory(Finder $finder, BoundsInterface $bounds) {
// jobs queue planning
$queue = new Queue;
$queue
->push(new DoAnalyze($this->output, $finder, $this->config->getPath()->getBasePath(), !$this->input->getOption('without-oop'), $this->config->getIgnoreErrors()))
->push(new SearchBounds($this->output, $bounds))
->push(new DoAggregatedAnalyze($this->output, new DirectoryAggregatorFlat($this->input->getOption('level'))))
->push(new CalculateScore(new Scoring($bounds)))
->push(new ReportRenderer(true, $this->output, new Summary\Cli($validator, $bounds, $this->output)))
->push(new ReportRenderer($this->config->getLogging()->getReport('cli'), $this->output, new Details\Cli($validator, $bounds)))
->push(new ReportWriter($this->config->getLogging()->getReport('html'), $this->output, new Summary\Html($validator, $bounds, $this->config->getTemplate())))
->push(new ReportWriter($this->config->getLogging()->getReport('json'), $this->output, new Details\Json($validator, $bounds)))
->push(new ReportWriter($this->config->getLogging()->getReport('xml'), $this->output, new Summary\Xml($validator, $bounds)))
->push(new ReportWriter($this->config->getLogging()->getReport('csv'), $this->output, new Details\Csv()))
->push(new ReportWriter($this->config->getLogging()->getViolation('xml'), $this->output, new Violations\Xml($validator, $bounds)))
->push(new ReportWriter($this->config->getLogging()->getChart('bubbles'), $this->output, new Chart\Bubbles($validator, $bounds)));
->push(new ReportRenderer(true, $this->output, new Summary\Cli($validator, $bounds, $this->output, $this->extensionsService)))
->push(new ReportRenderer($this->config->getLogging()->getReport('cli'), $this->output, new Details\Cli($validator, $bounds, $this->extensionsService)))
->push(new ReportWriter($this->config->getLogging()->getReport('html'), $this->output, new Summary\Html($validator, $bounds, $this->config->getTemplate(), $this->extensionsService)))
->push(new ReportWriter($this->config->getLogging()->getReport('json'), $this->output, new Details\Json(true, $this->extensionsService)))
->push(new ReportWriter($this->config->getLogging()->getReport('xml'), $this->output, new Summary\Xml($validator, $bounds, $this->extensionsService)))
->push(new ReportWriter($this->config->getLogging()->getReport('csv'), $this->output, new Details\Csv($this->extensionsService)))
->push(new ReportWriter($this->config->getLogging()->getViolation('xml'), $this->output, new Violations\Xml($validator, $bounds, $this->extensionsService)))
->push(new ReportWriter($this->config->getLogging()->getChart('bubbles'), $this->output, new Chart\Bubbles($validator, $bounds, $this->extensionsService)));

return $queue;
}
Expand Down
42 changes: 38 additions & 4 deletions src/Hal/Application/Command/RunMetricsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
*/

namespace Hal\Application\Command;
use Hal\Application\Command\Job\QueueAnalyzeFactory;
use Hal\Application\Command\Job\QueueFactory;
use Hal\Application\Command\Job\QueueReportFactory;
use Hal\Application\Config\ConfigFactory;
use Hal\Application\Extension\ExtensionService;
use Hal\Application\Extension\Repository;
use Hal\Component\Bounds\Bounds;
use Hal\Component\Evaluation\Evaluator;
use Hal\Component\File\Finder;
use Hal\Component\Result\ResultCollection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -88,6 +93,9 @@ protected function configure()
->addOption(
'offline', null, InputOption::VALUE_NONE, 'Include all JavaScript and CSS files in HTML. Generated report will be bigger'
)
->addOption(
'plugins', null, InputOption::VALUE_REQUIRED, 'Path of extensions to load, separated by comma (,)'
)
;
}

Expand Down Expand Up @@ -116,19 +124,45 @@ protected function execute(InputInterface $input, OutputInterface $output)
, $config->getPath()->isFollowSymlinks() ? Finder::FOLLOW_SYMLINKS : null
);

// prepare plugins
$repository = new Repository();
foreach($config->getExtensions()->getExtensions() as $filename) {
if(!file_exists($filename) ||!is_readable($filename)) {
$output->writeln(sprintf('<error>Plugin %s skipped: not found</error>', $filename));
continue;
}
$plugin = require_once($filename);
$repository->attach($plugin);
}
$extensionService = new ExtensionService($repository);

// prepare structures
$bounds = new Bounds();
$collection = new \Hal\Component\Result\ResultCollection();
$aggregatedResults = new \Hal\Component\Result\ResultCollection();
$collection = new ResultCollection();
$aggregatedResults = new ResultCollection();

// execute analyze
$queueFactory = new QueueFactory($input, $output, $config);
$queueFactory = new QueueAnalyzeFactory($input, $output, $config, $extensionService);
$queue = $queueFactory->factory($finder, $bounds);
gc_disable();
$queue->execute($collection, $aggregatedResults);
gc_enable();

$output->writeln('<info>done</info>');
$output->writeln('');

// provide data to extensions
if(($n = sizeof($repository->all())) > 0) {
$output->writeln(sprintf('%d %s. Executing analyzis', $n, ($n > 1 ? 'plugins are enabled' : 'plugin is enabled') ));
$extensionService->receive($config, $collection, $aggregatedResults, $bounds);
}

// generating reports
$output->writeln("Generating reports...");
$queueFactory = new QueueReportFactory($input, $output, $config, $extensionService);
$queue = $queueFactory->factory($finder, $bounds);
$queue->execute($collection, $aggregatedResults);

$output->writeln('<info>Done</info>');

// evaluation of success
$rule = $config->getFailureCondition();
Expand Down
1 change: 1 addition & 0 deletions src/Hal/Application/Config/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function factory(InputInterface $input) {
strlen($input->getOption('failure-condition')) > 0 && $config->setFailureCondition($input->getOption('failure-condition'));
strlen($input->getOption('template-title')) > 0 && $config->getTemplate()->setTitle($input->getOption('template-title'));
strlen($input->getOption('offline')) > 0 && $config->getTemplate()->setOffline($input->getOption('offline'));
strlen($input->getOption('plugins')) > 0 && $config->getExtensions()->setExtensions(explode(',', $input->getOption('plugins')));
strlen($input->getOption('ignore-errors')) > 0 && $config->setIgnoreErrors(true);

return $config;
Expand Down
23 changes: 23 additions & 0 deletions src/Hal/Application/Config/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class Configuration implements ConfigurationInterface
*/
private $ignoreErrors = false;

/**
* @var ExtensionsConfiguration
*/
private $extensions;

/**
* Constructor
*/
Expand All @@ -63,6 +68,7 @@ public function __construct() {
$this->path = new PathConfiguration();
$this->logging = new LoggingConfiguration();
$this->template = new TemplateConfiguration();
$this->extensions = new ExtensionsConfiguration();
}

/**
Expand Down Expand Up @@ -173,5 +179,22 @@ public function setIgnoreErrors($ignoreErrors)
return $this;
}

/**
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}

/**
* @param ExtensionsConfiguration $extensions
* @return Configuration
*/
public function setExtensions(ExtensionsConfiguration $extensions)
{
$this->extensions = $extensions;
return $this;
}

}
52 changes: 52 additions & 0 deletions src/Hal/Application/Config/ExtensionsConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* (c) Jean-Fran�ois L�pine <https://twitter.com/Halleck45>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Hal\Application\Config;

/**
* Extensions configuration
*
* @author Jean-François Lépine <https://twitter.com/Halleck45>
*/
class ExtensionsConfiguration
{
/**
* @var array
*/
private $extensions = array();


/**
* Constructor
*
* @param array $datas
*/
public function __construct(array $datas = array())
{
$this->extensions = $datas;
}

/**
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}

/**
* @param array $extensions
* @return ExtensionsConfiguration
*/
public function setExtensions($extensions)
{
$this->extensions = $extensions;
return $this;
}
}
5 changes: 4 additions & 1 deletion src/Hal/Application/Config/TreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getTree() {
->addDefaultsIfNotSet()
->children()
->scalarNode('directory')->defaultValue(null)->end()
->scalarNode('exclude')->defaultValue('Tests|tests|Features|features|\.svn|\.git|vendor')->end()
->scalarNode('exclude')->defaultValue('Tests|tests|Features|features|\.svn|\.git|vendor|node_modules|cache')->end()
->scalarNode('extensions')->defaultValue('php|inc')->end()
->booleanNode('symlinks')->defaultValue(false)->end()
->end()
Expand Down Expand Up @@ -82,6 +82,9 @@ public function getTree() {
->booleanNode('offline')->defaultValue(false)->end()
->end()
->end()
->arrayNode('plugins')
->prototype('scalar')
->end()
->end()
;

Expand Down
Loading