Skip to content

Commit

Permalink
feature #53 Added a way to show/hide ignored violations (lyrixx)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.2.x-dev branch.

Discussion
----------

Added a way to show/hide ignored violations

Commits
-------

bd78b19 Added a way to show/hide ignored violations
e3a21a7 Fixed CS
  • Loading branch information
lyrixx committed May 27, 2016
2 parents 41e9c2e + bd78b19 commit d3a4935
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 26 deletions.
5 changes: 3 additions & 2 deletions Cli/Command/AnalysisCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function configure()
->addArgument('project-uuid', InputArgument::REQUIRED)
->addOption('format', null, InputOption::VALUE_REQUIRED, 'To output in other formats', 'txt')
->addOption('fail-condition', null, InputOption::VALUE_REQUIRED, '')
->addOption('show-ignored-violations', null, InputOption::VALUE_NONE, 'Show ignored violations')
->setDescription('Show the last project analysis')
;
}
Expand All @@ -44,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$helper = new DescriptorHelper($api->getSerializer());
$helper->describe($output, $analysis, $input->getOption('format'));
$helper->describe($output, $analysis, $input->getOption('format'), $input->getOption('show-ignored-violations'));

if ('txt' === $input->getOption('format') && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
$output->writeln('');
Expand Down Expand Up @@ -74,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
$vars = array(
'analysis' => $analysis,
'counts' => (object) $counts,
'counts' => (object) $counts,
);

if ($el->evaluate($expr, $vars)) {
Expand Down
5 changes: 3 additions & 2 deletions Cli/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function configure()
->addArgument('project-uuid', InputArgument::REQUIRED)
->addOption('format', null, InputOption::VALUE_REQUIRED, 'To output in other formats', 'txt')
->addOption('reference', null, InputOption::VALUE_REQUIRED, 'The git reference to analyze')
->addOption('show-ignored-violations', null, InputOption::VALUE_NONE, 'Show ignored violations')
->setDescription('Analyze a project')
;
}
Expand Down Expand Up @@ -54,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

usleep(200000);

$position++;
++$position;
}

$analysis = $api->getAnalysis($projectUuid, $analysis->getNumber());
Expand All @@ -65,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$helper = new DescriptorHelper($api->getSerializer());
$helper->describe($output, $analysis, $input->getOption('format'));
$helper->describe($output, $analysis, $input->getOption('format'), $input->getOption('show-ignored-violations'));

if ('txt' === $input->getOption('format') && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
$output->writeln('');
Expand Down
2 changes: 1 addition & 1 deletion Cli/Command/ProjectsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$nbPage = ceil($projectsResource->getTotal() / 10);
$page = 1;
while ($page < $nbPage) {
$page++;
++$page;
$projects = array_merge($projects, $api->getProjects($page)->getProjects());
}

Expand Down
2 changes: 1 addition & 1 deletion Cli/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private function getStoragePath()
}
}

if (!is_dir($storagePath) && ! @mkdir($storagePath, 0777, true)) {
if (!is_dir($storagePath) && !@mkdir($storagePath, 0777, true)) {
throw new \RuntimeException(sprintf('The directory "%s" does not exist and could not be created.', $storagePath));
}

Expand Down
7 changes: 7 additions & 0 deletions Cli/Descriptor/AbstractDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
namespace SensioLabs\Insight\Cli\Descriptor;

use SensioLabs\Insight\Sdk\Model\Analysis;
use SensioLabs\Insight\Sdk\Model\Violation;

abstract class AbstractDescriptor
{
public function describe($object, array $options = array())
{
if ($object instanceof Analysis) {
if (!$options['show_ignored_violations']) {
$object->getViolations()->filter(function (Violation $v) {
return !$v->isIgnored();
});
}

return $this->describeAnalysis($object, $options);
}

Expand Down
16 changes: 8 additions & 8 deletions Cli/Descriptor/PmdDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

class PmdDescriptor extends AbstractDescriptor
{
const PHPMD_PRIORITY_HIGH = 1;
const PHPMD_PRIORITY_MEDIUM_HIGH = 2;
const PHPMD_PRIORITY_MEDIUM = 3;
const PHPMD_PRIORITY_MEDIUM_LOW = 4;
const PHPMD_PRIORITY_LOW = 5;
const PHPMD_PRIORITY_HIGH = 1;
const PHPMD_PRIORITY_MEDIUM_HIGH = 2;
const PHPMD_PRIORITY_MEDIUM = 3;
const PHPMD_PRIORITY_MEDIUM_LOW = 4;
const PHPMD_PRIORITY_LOW = 5;

protected function describeAnalysis(Analysis $analysis, array $options = array())
{
$output = $options['output'];

$xml = new \DOMDocument('1.0', 'UTF-8');
$xml = new \DOMDocument('1.0', 'UTF-8');
$xpath = new \DOMXPath($xml);

$xml->formatOutput = true;
$xml->formatOutput = true;
$xml->preserveWhiteSpace = true;

$pmd = $xml->createElement('pmd');
Expand All @@ -31,7 +31,7 @@ protected function describeAnalysis(Analysis $analysis, array $options = array()
$violations = $analysis->getViolations();
if ($violations) {
foreach ($violations as $violation) {
/**
/*
* @var $violation \SensioLabs\Insight\Sdk\Model\Violation
*/
$filename = $violation->getResource();
Expand Down
3 changes: 2 additions & 1 deletion Cli/Descriptor/TextDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function describeAnalysis(Analysis $analysis, array $options = array()
$template = <<<EOL
Resource: <comment>{{ resource }}:{{ line }}</comment>
Category: <comment>{{ category }}</comment> Severity: <comment>{{ severity }}</comment>
Title: <comment>{{ title }}</comment>
Title: <comment>{{ title }}</comment>{{ ignored }}
Message: <comment>{{ message }}</comment>
EOL;
Expand All @@ -74,6 +74,7 @@ protected function describeAnalysis(Analysis $analysis, array $options = array()
'{{ severity }}' => $violation->getSeverity(),
'{{ title }}' => $violation->getTitle(),
'{{ message }}' => $violation->getMessage(),
'{{ ignored }}' => $violation->isIgnored() ? ' (ignored)' : null,
)));
}
}
Expand Down
7 changes: 4 additions & 3 deletions Cli/Helper/DescriptorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public function __construct(Serializer $serializer)
;
}

public function describe(OutputInterface $output, $object, $format = null, $raw = false, $namespace = null)
public function describe(OutputInterface $output, $object, $format = null, $showIgnoredViolation = false)
{
$options = array(
'raw_text' => $raw,
'raw_text' => false,
'format' => $format ?: 'txt',
'output' => $output,
'show_ignored_violations' => $showIgnoredViolation,
);
$options['type'] = !$raw && 'txt' === $options['format'] ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW;
$options['type'] = 'txt' === $options['format'] ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW;

if (!isset($this->descriptors[$options['format']])) {
throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
Expand Down
4 changes: 2 additions & 2 deletions Sdk/Model/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

class Analysis
{
const STATUS_ORDERED = 'ordered';
const STATUS_RUNNING = 'running';
const STATUS_ORDERED = 'ordered';
const STATUS_RUNNING = 'running';
const STATUS_MEASURED = 'measured';
const STATUS_ANALYZED = 'analyzed';
const STATUS_FINISHED = 'finished';
Expand Down
8 changes: 4 additions & 4 deletions Sdk/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ class Project
public function toArray()
{
return array(
'name' => $this->name,
'public' => !$this->private,
'description' => $this->description,
'name' => $this->name,
'public' => !$this->private,
'description' => $this->description,
'repositoryUrl' => $this->repositoryUrl,
'type' => $this->type,
'type' => $this->type,
'configuration' => $this->configuration,
);
}
Expand Down
14 changes: 14 additions & 0 deletions Sdk/Model/Violation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Violation
*/
private $category;

/**
* @Type("boolean")
* @XmlAttribute
*/
private $ignored;

/**
* @return string
*/
Expand Down Expand Up @@ -87,4 +93,12 @@ public function getCategory()
{
return $this->category;
}

/**
* @return bool
*/
public function isIgnored()
{
return $this->ignored;
}
}
12 changes: 12 additions & 0 deletions Sdk/Model/Violations.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ public function getViolations()
{
return $this->violations;
}

/**
* @param callable $callback
*/
public function filter($callback)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('The callback is not callable.');
}

$this->violations = array_filter($this->violations, $callback);
}
}
1 change: 0 additions & 1 deletion Sdk/Tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,4 @@ private function createResponse($fixture, $statusCode = 200)
{
return new Response($statusCode, null, file_get_contents(sprintf('%s/fixtures/%s.xml', __DIR__, $fixture)));
}

}
2 changes: 1 addition & 1 deletion Sdk/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testParseErrors()
);

$this->assertInstanceOf('SensioLabs\Insight\Sdk\Model\Error', $error);
$this->assertSame($expectedFields , $error->getEntityBodyParameters());
$this->assertSame($expectedFields, $error->getEntityBodyParameters());
}

public function tearDown()
Expand Down

0 comments on commit d3a4935

Please sign in to comment.