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

[Feat] Add formatting support #201

Merged
merged 19 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Feat: Added support for formatting the result to json.
  • Loading branch information
olivernybroe committed Jun 25, 2019
commit 3435463d502a829464f5da51f733c1475b664f77
31 changes: 16 additions & 15 deletions src/Application/Console/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace NunoMaduro\PhpInsights\Application\Console;

use NunoMaduro\PhpInsights\Application\Console\Contracts\Formatter;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollectionFactory;
use NunoMaduro\PhpInsights\Domain\MetricsFinder;
use NunoMaduro\PhpInsights\Domain\Results;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
Expand All @@ -31,28 +33,27 @@ public function __construct(InsightCollectionFactory $insightCollectionFactory)
/**
* Analyse the given dirs.
*
* @param \NunoMaduro\PhpInsights\Application\Console\Style $style
* @param array<string, array> $config
* @param string $dir
* @param Formatter $format
* @param array<string, array> $config
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
* @param string $dir
* @param OutputInterface $consoleOutput
*
* @return \NunoMaduro\PhpInsights\Domain\Results
*/
public function analyse(Style $style, array $config, string $dir): Results
public function analyse(
Formatter $format,
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
array $config,
string $dir,
OutputInterface $consoleOutput
): Results
{
$metrics = MetricsFinder::find();

$insightCollection = $this->insightCollectionFactory->get($metrics, $config, $dir);
$insightCollection = $this->insightCollectionFactory
->get($metrics, $config, $dir, $consoleOutput);

$results = $insightCollection->results();
$format->format($insightCollection, $dir, $metrics);

$style->header($results, $dir)
->code($insightCollection, $results)
->complexity($insightCollection, $results)
->architecture($insightCollection, $results)
->misc($results);

$style->issues($insightCollection, $metrics, $dir);

return $results;
return $insightCollection->results();
}
}
33 changes: 25 additions & 8 deletions src/Application/Console/Commands/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Application\Console\Analyser;
use NunoMaduro\PhpInsights\Application\Console\Formatters\FormatResolver;
use NunoMaduro\PhpInsights\Application\Console\OutputDecorator;
use NunoMaduro\PhpInsights\Application\Console\Style;
use NunoMaduro\PhpInsights\Domain\Contracts\Repositories\FilesRepository;
use NunoMaduro\PhpInsights\Domain\Kernel;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;

/**
* @internal
Expand Down Expand Up @@ -54,7 +58,16 @@ public function __construct(Analyser $analyser, FilesRepository $filesRepository
*/
public function __invoke(InputInterface $input, OutputInterface $output): int
{
$style = new Style($input, OutputDecorator::decorate($output));
$consoleOutput = $output;
if ($consoleOutput instanceof ConsoleOutputInterface) {
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
$consoleOutput = $consoleOutput->getErrorOutput();
$consoleOutput->setDecorated(true);
}
$consoleStyle = new Style($input, $consoleOutput);

$output = OutputDecorator::decorate($output);

$format = FormatResolver::resolve($input, $output);

$directory = $this->getDirectory($input);

Expand All @@ -70,35 +83,39 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
if (! $isRootAnalyse) {
$config = $this->excludeGlobalInsights($config);
}
$results = $this->analyser->analyse($style, $config, $directory);
$results = $this->analyser->analyse(
$format,
$config,
$directory,
$consoleOutput
);

$hasError = false;
if ($input->getOption('min-quality') > $results->getCodeQuality()) {
$style->error('The code quality score is too low');
$consoleStyle->error('The code quality score is too low');
$hasError = true;
}

if ($input->getOption('min-complexity') > $results->getComplexity()) {
$style->error('The complexity score is too low');
$consoleStyle->error('The complexity score is too low');
$hasError = true;
}

if ($input->getOption('min-architecture') > $results->getStructure()) {
$style->error('The architecture score is too low');
$consoleStyle->error('The architecture score is too low');
$hasError = true;
}

if ($input->getOption('min-style') > $results->getStyle()) {
$style->error('The style score is too low');
$consoleStyle->error('The style score is too low');
$hasError = true;
}

if (! (bool) $input->getOption('disable-security-check') && $results->getTotalSecurityIssues() > 0) {
$hasError = true;
}

$style->newLine();
$style->writeln('✨ See something that needs to be improved? <bold>Create an issue</> or send us a <bold>pull request</>: <title>https://github.com/nunomaduro/phpinsights</title>');
$consoleStyle->writeln('✨ See something that needs to be improved? <bold>Create an issue</bold> or send us a <bold>pull request</bold>: <title>https://github.com/nunomaduro/phpinsights</title>');

return (int) $hasError;
}
Expand Down
27 changes: 27 additions & 0 deletions src/Application/Console/Contracts/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\PhpInsights\Application\Console\Contracts;

use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use NunoMaduro\PhpInsights\Domain\Results;

/**
* This interface is used to define the format of the result.
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
*/
interface Formatter
{
/**
* Format the result to the desired format.
*
* @param InsightCollection $insightCollection
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
* @param string $dir
* @param array $metrics
*/
public function format(
InsightCollection $insightCollection,
string $dir,
array $metrics
): void;
}
7 changes: 7 additions & 0 deletions src/Application/Console/Definitions/AnalyseDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public static function get(): array
InputOption::VALUE_NONE,
'Disable Security issues check to not throw error if vulnerability is found'
),
new InputOption(
'format',
null,
InputOption::VALUE_REQUIRED,
'Format to output the result in',
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
"console"
),
];
}
}
Loading