Skip to content

Commit

Permalink
new base code to implement feature request #208
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Jul 4, 2024
1 parent a00d2e5 commit 13e7229
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 108 deletions.
3 changes: 0 additions & 3 deletions .phplint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ exclude:
warning: true
memory-limit: -1
no-cache: false
log-json: false
log-junit: false
log-sarif: false
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ USER appuser
# Install Composer v2 then overtrue/phplint package
COPY --from=composer/composer:2-bin /composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN composer global require --no-progress overtrue/phplint 9.3.x-dev
RUN composer global require --no-progress overtrue/phplint 9.4.x-dev

# Following recommendation at https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions#workdir

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| Version | Status | Requirements |
|:--------|:------------------------------------------|:---------------|
| **9.x** | **Active development** | **PHP >= 8.1** |
| 9.4 | Active support | PHP >= 8.1 |
| 9.3 | Active support | PHP >= 8.1 |
| 9.2 | Active support | PHP >= 8.1 |
| 9.1 | Active support | PHP >= 8.1 |
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ You can also install `phplint` locally to your project with [Phive][phive] and c
```xml
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="overtrue/phplint" version="^9.3" copy="false" />
<phar name="overtrue/phplint" version="^9.4" copy="false" />
</phive>
```

Expand Down
30 changes: 30 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the overtrue/phplint package
*
* (c) overtrue
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\PHPLint;

use Symfony\Component\Console\Application;

/**
* @author Laurent Laville
* @since Release 9.4.0
*/
class Client
{
public function __construct(private readonly Application $application)
{
}

public function getApplication(): Application
{
return $this->application;
}
}
26 changes: 7 additions & 19 deletions src/Command/ConfigureCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,16 @@ protected function configureCommand(Command $command): void
'Hide the progress output'
)
->addOption(
'log-json',
null,
InputOption::VALUE_OPTIONAL,
'Log scan results in JSON format to file (<comment>default: ' . OptionDefinition::DEFAULT_STANDARD_OUTPUT_LABEL . '</comment>)'
)
->addOption(
'log-junit',
null,
InputOption::VALUE_OPTIONAL,
'Log scan results in JUnit XML format to file (<comment>default: ' . OptionDefinition::DEFAULT_STANDARD_OUTPUT_LABEL . '</comment>)'
)
->addOption(
'log-sarif',
null,
InputOption::VALUE_OPTIONAL,
'Log scan results in SARIF format to file (<comment>default: ' . OptionDefinition::DEFAULT_STANDARD_OUTPUT_LABEL . '</comment>)'
'output',
'o',
InputOption::VALUE_REQUIRED,
'Generate an output to the specified path (<comment>default: ' . OptionDefinition::DEFAULT_STANDARD_OUTPUT_LABEL . '</comment>)'
)
->addOption(
'sarif-converter',
'format',
null,
InputOption::VALUE_OPTIONAL,
'SARIF class converter (<comment>default: ' . OptionDefinition::DEFAULT_SARIF_CONVERTER_CLASS . '</comment>)'
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Format of requested reports'
)
->addOption(
'warning',
Expand Down
11 changes: 8 additions & 3 deletions src/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Overtrue\PHPLint\Command;

use Overtrue\PHPLint\Client;
use Overtrue\PHPLint\Configuration\ConsoleOptionsResolver;
use Overtrue\PHPLint\Configuration\FileOptionsResolver;
use Overtrue\PHPLint\Configuration\OptionDefinition;
Expand Down Expand Up @@ -86,9 +87,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$finder = (new Finder($configResolver))->getFiles();
/** @var Application $app */
$app = $this->getApplication();
$linter = new Linter($configResolver, $this->dispatcher, $app->getLongVersion(), $this->getHelperSet(), $output);
$linter = new Linter(
$configResolver,
$this->dispatcher,
new Client($this->getApplication()),
$this->getHelperSet(),
$output
);
$this->results = $linter->lintFiles($finder, $startTime);

$data = $this->results->getFailures();
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/ConsoleOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author Laurent Laville
* @since Release 9.0.0
*/
class ConsoleOptionsResolver extends AbstractOptionsResolver
final class ConsoleOptionsResolver extends AbstractOptionsResolver
{
public function factory(): Options
{
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/FileOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author Laurent Laville
* @since Release 9.0.0
*/
class FileOptionsResolver extends AbstractOptionsResolver
final class FileOptionsResolver extends AbstractOptionsResolver
{
public function __construct(InputInterface $input)
{
Expand Down
24 changes: 4 additions & 20 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@
use Overtrue\PHPLint\Helper\DebugFormatterHelper;
use Overtrue\PHPLint\Helper\ProcessHelper;
use Overtrue\PHPLint\Output\ConsoleOutput;
use Phar;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function array_keys;
use function in_array;

use const STDOUT;

/**
* @author Overtrue
* @author Laurent Laville (since v9.0)
*/
final class Application extends BaseApplication
{
public const NAME = 'phplint';
public const VERSION = '9.3.1';
public const VERSION = '9.4.0-dev';

public function __construct()
{
Expand All @@ -45,27 +45,11 @@ public function __construct()

public function run(InputInterface $input = null, OutputInterface $output = null): int
{
$output ??= new ConsoleOutput();
$output ??= new ConsoleOutput(STDOUT);

return parent::run($input, $output);
}

protected function configureIO(InputInterface $input, OutputInterface $output): void
{
if (Phar::running()) {
$inputDefinition = $this->getDefinition();
$inputDefinition->addOption(
new InputOption(
'manifest',
null,
InputOption::VALUE_NONE,
'Show which versions of dependencies are bundled'
)
);
}
parent::configureIO($input, $output);
}

protected function getDefaultCommands(): array
{
return [new HelpCommand(), new ListCommand()];
Expand Down
38 changes: 14 additions & 24 deletions src/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Overtrue\PHPLint\Event\BeforeCheckingEvent;
use Overtrue\PHPLint\Event\BeforeLintFileEvent;
use Overtrue\PHPLint\Helper\ProcessHelper;
use Overtrue\PHPLint\Output\ConsoleOutputInterface;
use Overtrue\PHPLint\Output\LinterOutput;
use Overtrue\PHPLint\Process\LintProcess;
use Psr\Cache\InvalidArgumentException;
Expand All @@ -40,6 +39,7 @@
use function md5_file;
use function microtime;
use function phpversion;
use function strip_tags;
use function version_compare;

/**
Expand All @@ -57,20 +57,16 @@ final class Linter
private int $processLimit;
private string $memoryLimit;
private bool $warning;
private array $options;
private string $appLongVersion;

public function __construct(
Resolver $configResolver,
EventDispatcherInterface $dispatcher,
string $appVersion = '9.1.x-dev',
HelperSet $helperSet = null,
OutputInterface $output = null
private readonly ?Client $client = null,
?HelperSet $helperSet = null,
?OutputInterface $output = null,
) {
$this->configResolver = $configResolver;
$this->dispatcher = $dispatcher;
$this->appLongVersion = $appVersion;
$this->options = $configResolver->getOptions();
$this->processLimit = $configResolver->getOption(OptionDefinition::JOBS);
$this->memoryLimit = (string) $configResolver->getOption(OptionDefinition::OPTION_MEMORY_LIMIT);
$this->warning = $configResolver->getOption(OptionDefinition::WARNING);
Expand Down Expand Up @@ -108,21 +104,7 @@ public function lintFiles(Finder $finder, ?float $startTime = null): LinterOutpu
$fileCount = 0;
}

if ($this->output instanceof ConsoleOutputInterface) {
$configFile = $this->options['no-configuration']
? ''
: $this->options['configuration']
;
$this->output->headerBlock($this->appLongVersion, $configFile);
$this->output->configBlock($this->options);
}

$this->dispatcher->dispatch(
new BeforeCheckingEvent(
$this,
['fileCount' => $fileCount, 'appVersion' => $this->appLongVersion, 'options' => $this->options]
)
);
$this->dispatcher->dispatch(new BeforeCheckingEvent($this, ['fileCount' => $fileCount]));

$processCount = 0;
if ($fileCount > 0) {
Expand All @@ -131,8 +113,16 @@ public function lintFiles(Finder $finder, ?float $startTime = null): LinterOutpu
$results = [];
}

if (null !== $this->client) {
$default = [
'application_version' => [
'long' => $this->client->getApplication()->getLongVersion(),
'short' => $this->client->getApplication()->getVersion(),
]
];
}
$finalResults = new LinterOutput($results, $finder);
$finalResults->setContext($this->configResolver, $startTime, $processCount);
$finalResults->setContext($this->configResolver, $startTime, $processCount, $default ?? []);

$this->dispatcher->dispatch(new AfterCheckingEvent($this, ['results' => $finalResults]));

Expand Down
13 changes: 13 additions & 0 deletions src/Output/ChainOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use InvalidArgumentException;

use function count;
use function fclose;
use function get_debug_type;
use function sprintf;

Expand Down Expand Up @@ -45,12 +46,24 @@ public function __construct(array $handlers)
}
}

public function getName(): string
{
return 'chain';
}

public function format(LinterOutput $results): void
{
$i = count($this->outputHandlers);

if ($i === 0) {
return;
}

while ($i--) {
$this->outputHandlers[$i]->format($results);
}

// close stream only once all formatters do their job
fclose($this->outputHandlers[0]->getStream());
}
}
28 changes: 21 additions & 7 deletions src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput as BaseConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutput as SymfonyConsoleOutput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -56,21 +57,30 @@
* @author Laurent Laville
* @since Release 9.0.0
*/
class ConsoleOutput extends BaseConsoleOutput implements ConsoleOutputInterface
final class ConsoleOutput extends StreamOutput implements OutputInterface, ConsoleOutputInterface
{
public const MAX_LINE_LENGTH = 120;

private ?ProgressBar $progressBar = null;

private int $lineLength;

public function __construct(int $verbosity = parent::VERBOSITY_NORMAL, bool $decorated = null, OutputFormatterInterface $formatter = null)
{
parent::__construct($verbosity, $decorated, $formatter);
public function __construct(
$stream,
int $verbosity = parent::VERBOSITY_NORMAL,
?bool $decorated = null,
?OutputFormatterInterface $formatter = null
) {
parent::__construct($stream, $verbosity, $decorated, $formatter);
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
$this->lineLength = min($width - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
}

public function getName(): string
{
return 'console';
}

public function format(LinterOutput $results): void
{
$data = $results->getFailures();
Expand All @@ -83,6 +93,11 @@ public function format(LinterOutput $results): void
return;
}

$options = $context['options_used'] ?? [];
$configFile = $options['no-configuration'] ? '' : $options['configuration'];
$this->headerBlock($context['application_version']['long'], $configFile);
$this->configBlock($options);

$this->consumeBlock($context['time_usage'], $context['memory_usage'], $context['cache_usage'], $context['process_count']);

if ($errCount > 0) {
Expand Down Expand Up @@ -262,8 +277,7 @@ public function configBlock(array $options): void
}
}

$section = $this->section();
$table = new Table($section);
$table = new Table($this);
$table
->setHeaders($headers)
->setRows($rows)
Expand Down
Loading

0 comments on commit 13e7229

Please sign in to comment.