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

docs: Display/include tool info/version by default in commands and reports #7733

Merged
merged 11 commits into from
Jan 15, 2024
11 changes: 11 additions & 0 deletions doc/schemas/fix/xml.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
</xs:complexType>
</xs:element>

<xs:element name="about">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

<xs:element name="files">
<xs:complexType>
<xs:sequence>
Expand Down Expand Up @@ -73,6 +83,7 @@
<xs:element name="report">
<xs:complexType>
<xs:sequence>
<xs:element ref="about" maxOccurs="1" minOccurs="1"/>
<xs:element ref="files"/>
<xs:element ref="time" maxOccurs="1" minOccurs="0"/>
<xs:element ref="memory" maxOccurs="1" minOccurs="0"/>
Expand Down
39 changes: 34 additions & 5 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@
*/
final class Application extends BaseApplication
{
public const NAME = 'PHP CS Fixer';
public const VERSION = '3.46.1-DEV';
public const VERSION_CODENAME = 'Three Keys';

private ToolInfo $toolInfo;

public function __construct()
{
parent::__construct('PHP CS Fixer', self::VERSION);
parent::__construct(self::NAME, self::VERSION);

$this->toolInfo = new ToolInfo();

Expand Down Expand Up @@ -109,22 +110,50 @@ public function doRun(InputInterface $input, OutputInterface $output): int
return $result;
}

public function getLongVersion(): string
/**
* @internal
*/
public static function getAbout(bool $decorated = false): string
{
$longVersion = sprintf('%s <info>%s</info>', self::NAME, self::VERSION);

$commit = '@git-commit@';
$versionCommit = '';

if ('@'.'git-commit@' !== $commit) { /** @phpstan-ignore-line as `$commit` is replaced during phar building */
$versionCommit = substr($commit, 0, 7);
}

return implode('', [
parent::getLongVersion(),
$about = implode('', [
$longVersion,
$versionCommit ? sprintf(' <info>(%s)</info>', $versionCommit) : '', // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.`
self::VERSION_CODENAME ? sprintf(' <info>%s</info>', self::VERSION_CODENAME) : '', // @phpstan-ignore-line to avoid `Ternary operator condition is always true|false.`
' by <comment>Fabien Potencier</comment>, <comment>Dariusz Ruminski</comment> and <comment>contributors</comment>.',
"\nPHP runtime: <info>".PHP_VERSION.'</info>',
]);

if (false === $decorated) {
return strip_tags($about);
}

return $about;
}

/**
* @internal
*/
public static function getAboutWithRuntime(bool $decorated = false): string
{
$about = self::getAbout(true)."\nPHP runtime: <info>".PHP_VERSION.'</info>';
if (false === $decorated) {
return strip_tags($about);
}

return $about;
}

public function getLongVersion(): string
{
return self::getAboutWithRuntime(true);
}

protected function getDefaultCommands(): array
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Command/DescribeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace PhpCsFixer\Console\Command;

use PhpCsFixer\Config;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Console\ConfigurationResolver;
use PhpCsFixer\Differ\DiffConsoleFormatter;
use PhpCsFixer\Differ\FullDiffer;
Expand Down Expand Up @@ -95,9 +96,9 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity() && $output instanceof ConsoleOutputInterface) {
if ($output instanceof ConsoleOutputInterface) {
$stdErr = $output->getErrorOutput();
$stdErr->writeln($this->getApplication()->getLongVersion());
$stdErr->writeln(Application::getAboutWithRuntime(true));
}

$resolver = new ConfigurationResolver(
Expand Down
5 changes: 2 additions & 3 deletions src/Console/Command/FixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpCsFixer\Config;
use PhpCsFixer\ConfigInterface;
use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
use PhpCsFixer\Console\Application;
use PhpCsFixer\Console\ConfigurationResolver;
use PhpCsFixer\Console\Output\ErrorOutput;
use PhpCsFixer\Console\Output\OutputContext;
Expand Down Expand Up @@ -251,9 +252,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
: ('txt' === $reporter->getFormat() ? $output : null);

if (null !== $stdErr) {
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$stdErr->writeln($this->getApplication()->getLongVersion());
}
$stdErr->writeln(Application::getAboutWithRuntime(true));

$configFile = $resolver->getConfigFile();
$stdErr->writeln(sprintf('Loaded config <comment>%s</comment>%s.', $resolver->getConfig()->getName(), null === $configFile ? '' : ' from "'.$configFile.'"'));
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Command/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Console\Command;

use PhpCsFixer\Console\Application;
use PhpCsFixer\Console\SelfUpdate\NewVersionCheckerInterface;
use PhpCsFixer\PharCheckerInterface;
use PhpCsFixer\Preg;
Expand Down Expand Up @@ -81,9 +82,9 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity() && $output instanceof ConsoleOutputInterface) {
if ($output instanceof ConsoleOutputInterface) {
$stdErr = $output->getErrorOutput();
$stdErr->writeln($this->getApplication()->getLongVersion());
$stdErr->writeln(Application::getAboutWithRuntime(true));
}

if (!$this->toolInfo->isInstalledAsPhar()) {
Expand Down
4 changes: 4 additions & 0 deletions src/Console/Report/FixReport/CheckstyleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Console\Report\FixReport;

use PhpCsFixer\Console\Application;
use Symfony\Component\Console\Formatter\OutputFormatter;

/**
Expand All @@ -35,7 +36,10 @@ public function generate(ReportSummary $reportSummary): string
}

$dom = new \DOMDocument('1.0', 'UTF-8');

/** @var \DOMElement $checkstyles */
$checkstyles = $dom->appendChild($dom->createElement('checkstyle'));
$checkstyles->setAttribute('version', Application::getAbout());

foreach ($reportSummary->getChanged() as $filePath => $fixResult) {
/** @var \DOMElement $file */
Expand Down
7 changes: 5 additions & 2 deletions src/Console/Report/FixReport/GitlabReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Console\Report\FixReport;

use PhpCsFixer\Console\Application;
use SebastianBergmann\Diff\Chunk;
use SebastianBergmann\Diff\Parser;
use Symfony\Component\Console\Formatter\OutputFormatter;
Expand Down Expand Up @@ -46,15 +47,17 @@ public function getFormat(): string
*/
public function generate(ReportSummary $reportSummary): string
{
$about = Application::getAbout();

$report = [];
foreach ($reportSummary->getChanged() as $fileName => $change) {
$diffs = $this->diffParser->parse($change['diff']);
$firstChunk = isset($diffs[0]) ? $diffs[0]->getChunks() : [];
$firstChunk = array_shift($firstChunk);
foreach ($change['appliedFixers'] as $fixerName) {
$report[] = [
'check_name' => $fixerName,
'description' => $fixerName,
'check_name' => 'PHP-CS-Fixer.'.$fixerName,
'description' => 'PHP-CS-Fixer.'.$fixerName.' by '.$about,
'categories' => ['Style'],
'fingerprint' => md5($fileName.$fixerName),
'severity' => 'minor',
Expand Down
2 changes: 2 additions & 0 deletions src/Console/Report/FixReport/JsonReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Console\Report\FixReport;

use PhpCsFixer\Console\Application;
use Symfony\Component\Console\Formatter\OutputFormatter;

/**
Expand Down Expand Up @@ -47,6 +48,7 @@ public function generate(ReportSummary $reportSummary): string
}

$json = [
'about' => Application::getAbout(),
'files' => $jsonFiles,
'time' => [
'total' => round($reportSummary->getTime() / 1_000, 3),
Expand Down
8 changes: 8 additions & 0 deletions src/Console/Report/FixReport/JunitReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Console\Report\FixReport;

use PhpCsFixer\Console\Application;
use PhpCsFixer\Preg;
use Symfony\Component\Console\Formatter\OutputFormatter;

Expand Down Expand Up @@ -42,6 +43,13 @@ public function generate(ReportSummary $reportSummary): string
$testsuite = $testsuites->appendChild($dom->createElement('testsuite'));
$testsuite->setAttribute('name', 'PHP CS Fixer');

$properties = $dom->createElement('properties');
$property = $dom->createElement('property');
$property->setAttribute('name', 'about');
$property->setAttribute('value', Application::getAbout());
$properties->appendChild($property);
$testsuite->appendChild($properties);

if (\count($reportSummary->getChanged()) > 0) {
$this->createFailedTestCases($dom, $testsuite, $reportSummary);
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/Console/Report/FixReport/XmlReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Console\Report\FixReport;

use PhpCsFixer\Console\Application;
use Symfony\Component\Console\Formatter\OutputFormatter;

/**
Expand All @@ -39,6 +40,8 @@ public function generate(ReportSummary $reportSummary): string
$root = $dom->createElement('report');
$dom->appendChild($root);

$root->appendChild($this->createAboutElement($dom, Application::getAbout()));

$filesXML = $dom->createElement('files');
$root->appendChild($filesXML);

Expand Down Expand Up @@ -120,4 +123,12 @@ private function createMemoryElement(float $memory, \DOMDocument $dom): \DOMElem

return $memoryXML;
}

private function createAboutElement(\DOMDocument $dom, string $about): \DOMElement
{
$XML = $dom->createElement('about');
$XML->setAttribute('value', $about);

return $XML;
}
}
37 changes: 25 additions & 12 deletions tests/Console/Report/FixReport/CheckstyleReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace PhpCsFixer\Tests\Console\Report\FixReport;

use PhpCsFixer\Console\Application;
use PhpCsFixer\Console\Report\FixReport\CheckstyleReporter;
use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
use PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint\XmlMatchesXsd;
Expand Down Expand Up @@ -47,17 +48,21 @@ public static function tearDownAfterClass(): void

protected static function createNoErrorReport(): string
{
return <<<'XML'
$about = Application::getAbout();

return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle />
<checkstyle version="{$about}" />
XML;
}

protected static function createSimpleReport(): string
{
return <<<'XML'
$about = Application::getAbout();

return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<checkstyle version="{$about}">
<file name="someFile.php">
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
</file>
Expand All @@ -67,10 +72,12 @@ protected static function createSimpleReport(): string

protected static function createWithDiffReport(): string
{
$about = Application::getAbout();

// NOTE: checkstyle format does NOT include diffs
return <<<'XML'
return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<checkstyle version="{$about}">
<file name="someFile.php">
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
</file>
Expand All @@ -80,9 +87,11 @@ protected static function createWithDiffReport(): string

protected static function createWithAppliedFixersReport(): string
{
return <<<'XML'
$about = Application::getAbout();

return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<checkstyle version="{$about}">
<file name="someFile.php">
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_1" message="Found violation(s) of type: some_fixer_name_here_1" />
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_2" message="Found violation(s) of type: some_fixer_name_here_2" />
Expand All @@ -93,10 +102,12 @@ protected static function createWithAppliedFixersReport(): string

protected static function createWithTimeAndMemoryReport(): string
{
$about = Application::getAbout();

// NOTE: checkstyle format does NOT include time or memory
return <<<'XML'
return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<checkstyle version="{$about}">
<file name="someFile.php">
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
</file>
Expand All @@ -106,9 +117,11 @@ protected static function createWithTimeAndMemoryReport(): string

protected static function createComplexReport(): string
{
return <<<'XML'
$about = Application::getAbout();

return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<checkstyle version="{$about}">
<file name="someFile.php">
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_1" message="Found violation(s) of type: some_fixer_name_here_1" />
<error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_2" message="Found violation(s) of type: some_fixer_name_here_2" />
Expand Down