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 all 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
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
12 changes: 8 additions & 4 deletions docs/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

The project is under development. As such, any help is welcome!

1. [Create a new insight from scratch](#create-a-new-insight)
2. [Add a new insight from PHP CS Sniff](#add-a-new-insight-from-php-cs-sniff)
3. [Create or improve create a preset for your favorite framework](#create-or-improve-create-a-preset-for-your-favorite-framework)
4. [Create the test suite](#create-the-test-suite)
[[TOC]]
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved

## Create a new `Insight`

Expand Down Expand Up @@ -129,3 +126,10 @@ final class Preset implements PresetContract
}
}
```

## Create a new `Formatter`

The package has support for formatting the result.
All formats implements the contract `src/Application/Console/Contracts/Formatter`.

You are welcome to contribute with new formats or improve on the ones we already have.
25 changes: 25 additions & 0 deletions docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ In laravel, launch command as usual with your path:
php artisan insights path/to/analyse
```

## Formatting the output

For changing the output format you can add the `format` flag. The following formats are supported:

- console
- json

```bash
./vendor/bin/phpinsights analyse --format=json
```

## Saving output to file

You can pipe the result to a file or to anywhere you like.
A common use case is parsing the output formatted as json to a json file.

```bash
./vendor/bin/phpinsights analyse --format=json > test.json
```

When piping the result remember to add the no interaction flag `-n`, as the part where you need to interact is also getting piped. (the json format does not have any interaction)
While piping data, if you want the progress bar to refresh itself instead of printing a new one, add the `--ansi` flag.



## Allowed memory size of X bytes exhausted

If you encounter the error `Allowed memory size of XXXXX bytes exhausted`, the current workaround is to increase the memory limit:
Expand Down
13 changes: 13 additions & 0 deletions phpinsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

use NunoMaduro\PhpInsights\Domain\Sniffs\ForbiddenSetterSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
use SlevomatCodingStandard\Sniffs\TypeHints\DisallowMixedTypeHintSniff;

return [

Expand Down Expand Up @@ -47,6 +49,17 @@
'lineLimit' => 80,
'absoluteLineLimit' => 120,
],
DisallowMixedTypeHintSniff::class => [
'exclude' => [
'src/Domain/Reflection.php',
],
],
ForbiddenSetterSniff::class => [
'exclude' => [
'src/Domain/Reflection.php',
'src/Domain/Details.php',
],
],
],

];
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
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
101 changes: 101 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"readOnly": true,
"title": "The JSON format for phpinsights",
"required": [
"summary",
"Code",
"Complexity",
"Architecture",
"Style",
"Security"
],
"definitions": {
"percentage": {
"type": "number",
"minimum": 0,
"maximum": 100
},
"insight": {
"type": "object",
"required": [
"title",
"insightClass"
],
"properties": {
"title": {
"type": "string"
},
"insightClass": {
"type": "string"
},
"file": {
"type": "string"
},
"line": {
"type": "integer",
"minimum": 1
},
"message": {
"type": "string"
}
}
}
},
"properties": {
"summary": {
"$id": "#/properties/summary",
"type": "object",
"title": "The Summary Schema",
"properties": {
"code": {
"$ref": "#/definitions/percentage"
},
"complexity": {
"$ref": "#/definitions/percentage"
},
"architecture": {
"$ref": "#/definitions/percentage"
},
"style": {
"$ref": "#/definitions/percentage"
},
"security issues": {
"type": "integer",
"minimum": 0
}
}
},
"Code": {
"type": "array",
"items": {
"$ref": "#/definitions/insight"
}
},
"Complexity": {
"type": "array",
"items": {
"$ref": "#/definitions/insight"
}
},
"Architecture": {
"type": "array",
"items": {
"$ref": "#/definitions/insight"
}
},
"Style": {
"type": "array",
"items": {
"$ref": "#/definitions/insight"
}
},
"Security": {
"type": "array",
"items": {
"$ref": "#/definitions/insight"
}
}
}
}
33 changes: 17 additions & 16 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 @@ -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 $formatter
* @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 $formatter,
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();
$formatter->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
Original file line number Diff line number Diff line change
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);

$formatter = FormatResolver::resolve($input, $output, $consoleOutput);

$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(
$formatter,
$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
Loading