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 8 commits
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Expand Up @@ -17,3 +17,11 @@ trim_trailing_whitespace = false

[*.yml]
indent_size = 2

[*.php]
ij_php_align_phpdoc_comments = false
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
ij_php_align_phpdoc_param_names = false
ij_php_phpdoc_use_fqcn = true
ij_php_space_after_unary_not = true
ij_php_force_short_declaration_array_style = true
ij_php_comma_after_last_array_element = true
1 change: 1 addition & 0 deletions phpstan.neon.dist
Expand Up @@ -23,6 +23,7 @@ parameters:
- '#Language construct isset\(\) should not be used#'
- '#Access to an undefined property PHP_CodeSniffer\\Config::\$standards.#'
- '#Access to an undefined property PHP_CodeSniffer\\Sniffs\\Sniff::\$#'
- '#NunoMaduro\\PhpInsights\\Application\\Console\\Formatters\\Json has an unused parameter \$input#'
autoload_files:
- %rootDir%/../../squizlabs/php_codesniffer/autoload.php
reportUnmatchedIgnoredErrors: false
Expand Down
33 changes: 17 additions & 16 deletions src/Application/Console/Analyser.php
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 @@ -21,7 +23,7 @@ final class Analyser
/**
* Analyser constructor.
*
* @param \NunoMaduro\PhpInsights\Domain\Insights\InsightCollectionFactory $insightCollectionFactory
* @param \NunoMaduro\PhpInsights\Domain\Insights\InsightCollectionFactory $insightCollectionFactory
*/
public function __construct(InsightCollectionFactory $insightCollectionFactory)
{
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();
}
}
46 changes: 31 additions & 15 deletions src/Application/Console/Commands/AnalyseCommand.php
Expand Up @@ -6,11 +6,13 @@

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\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -35,8 +37,8 @@ final class AnalyseCommand
/**
* Creates a new instance of the Analyse Command.
*
* @param \NunoMaduro\PhpInsights\Application\Console\Analyser $analyser
* @param \NunoMaduro\PhpInsights\Domain\Contracts\Repositories\FilesRepository $filesRepository
* @param \NunoMaduro\PhpInsights\Application\Console\Analyser $analyser
* @param \NunoMaduro\PhpInsights\Domain\Contracts\Repositories\FilesRepository $filesRepository
*/
public function __construct(Analyser $analyser, FilesRepository $filesRepository)
{
Expand All @@ -47,14 +49,23 @@ public function __construct(Analyser $analyser, FilesRepository $filesRepository
/**
* Handle the given input.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int
*/
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($output->isDecorated());
}
$consoleStyle = new Style($input, $consoleOutput);

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

$format = FormatResolver::resolve($input, $output, $consoleOutput);
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -70,44 +81,49 @@ 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->newLine();
$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;
}

/**
* Gets the config from the given input.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param string $directory
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param string $directory
*
* @return array<string, array>
*/
Expand All @@ -126,7 +142,7 @@ private function getConfig(InputInterface $input, string $directory): array
/**
* Gets the directory from the given input.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Input\InputInterface $input
*
* @return string
*/
Expand Down
28 changes: 28 additions & 0 deletions src/Application/Console/Contracts/Formatter.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\PhpInsights\Application\Console\Contracts;

use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;

/**
* This interface is used to define the format of the result.
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
*
* @internal
*/
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<string> $metrics
olivernybroe marked this conversation as resolved.
Show resolved Hide resolved
*/
public function format(
InsightCollection $insightCollection,
string $dir,
array $metrics
): void;
}
7 changes: 7 additions & 0 deletions src/Application/Console/Definitions/AnalyseDefinition.php
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 [console, json]',
"console"
),
];
}
}