Browse Source

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

Dariusz Rumiński 1 year ago
parent
commit
98ccec2af4

+ 11 - 0
doc/schemas/fix/xml.xsd

@@ -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>
@@ -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"/>

+ 34 - 5
src/Console/Application.php

@@ -40,6 +40,7 @@ use Symfony\Component\Console\Output\OutputInterface;
  */
 final class Application extends BaseApplication
 {
+    public const NAME = 'PHP CS Fixer';
     public const VERSION = '3.46.1-DEV';
     public const VERSION_CODENAME = 'Three Keys';
 
@@ -47,7 +48,7 @@ final class Application extends BaseApplication
 
     public function __construct()
     {
-        parent::__construct('PHP CS Fixer', self::VERSION);
+        parent::__construct(self::NAME, self::VERSION);
 
         $this->toolInfo = new ToolInfo();
 
@@ -109,8 +110,13 @@ final class Application extends BaseApplication
         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 = '';
 
@@ -118,13 +124,36 @@ final class Application extends BaseApplication
             $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

+ 3 - 2
src/Console/Command/DescribeCommand.php

@@ -15,6 +15,7 @@ declare(strict_types=1);
 namespace PhpCsFixer\Console\Command;
 
 use PhpCsFixer\Config;
+use PhpCsFixer\Console\Application;
 use PhpCsFixer\Console\ConfigurationResolver;
 use PhpCsFixer\Differ\DiffConsoleFormatter;
 use PhpCsFixer\Differ\FullDiffer;
@@ -95,9 +96,9 @@ final class DescribeCommand extends Command
 
     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(

+ 2 - 3
src/Console/Command/FixCommand.php

@@ -17,6 +17,7 @@ namespace PhpCsFixer\Console\Command;
 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;
@@ -251,9 +252,7 @@ use Symfony\Component\Stopwatch\Stopwatch;
             : ('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.'"'));

+ 3 - 2
src/Console/Command/SelfUpdateCommand.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Console\Command;
 
+use PhpCsFixer\Console\Application;
 use PhpCsFixer\Console\SelfUpdate\NewVersionCheckerInterface;
 use PhpCsFixer\PharCheckerInterface;
 use PhpCsFixer\Preg;
@@ -81,9 +82,9 @@ final class SelfUpdateCommand extends Command
 
     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()) {

+ 4 - 0
src/Console/Report/FixReport/CheckstyleReporter.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Console\Report\FixReport;
 
+use PhpCsFixer\Console\Application;
 use Symfony\Component\Console\Formatter\OutputFormatter;
 
 /**
@@ -35,7 +36,10 @@ final class CheckstyleReporter implements ReporterInterface
         }
 
         $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 */

+ 5 - 2
src/Console/Report/FixReport/GitlabReporter.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Console\Report\FixReport;
 
+use PhpCsFixer\Console\Application;
 use SebastianBergmann\Diff\Chunk;
 use SebastianBergmann\Diff\Parser;
 use Symfony\Component\Console\Formatter\OutputFormatter;
@@ -46,6 +47,8 @@ final class GitlabReporter implements ReporterInterface
      */
     public function generate(ReportSummary $reportSummary): string
     {
+        $about = Application::getAbout();
+
         $report = [];
         foreach ($reportSummary->getChanged() as $fileName => $change) {
             $diffs = $this->diffParser->parse($change['diff']);
@@ -53,8 +56,8 @@ final class GitlabReporter implements ReporterInterface
             $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',

+ 2 - 0
src/Console/Report/FixReport/JsonReporter.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Console\Report\FixReport;
 
+use PhpCsFixer\Console\Application;
 use Symfony\Component\Console\Formatter\OutputFormatter;
 
 /**
@@ -47,6 +48,7 @@ final class JsonReporter implements ReporterInterface
         }
 
         $json = [
+            'about' => Application::getAbout(),
             'files' => $jsonFiles,
             'time' => [
                 'total' => round($reportSummary->getTime() / 1_000, 3),

+ 8 - 0
src/Console/Report/FixReport/JunitReporter.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Console\Report\FixReport;
 
+use PhpCsFixer\Console\Application;
 use PhpCsFixer\Preg;
 use Symfony\Component\Console\Formatter\OutputFormatter;
 
@@ -42,6 +43,13 @@ final class JunitReporter implements ReporterInterface
         $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 {

+ 11 - 0
src/Console/Report/FixReport/XmlReporter.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Console\Report\FixReport;
 
+use PhpCsFixer\Console\Application;
 use Symfony\Component\Console\Formatter\OutputFormatter;
 
 /**
@@ -39,6 +40,8 @@ final class XmlReporter implements ReporterInterface
         $root = $dom->createElement('report');
         $dom->appendChild($root);
 
+        $root->appendChild($this->createAboutElement($dom, Application::getAbout()));
+
         $filesXML = $dom->createElement('files');
         $root->appendChild($filesXML);
 
@@ -120,4 +123,12 @@ final class XmlReporter implements ReporterInterface
 
         return $memoryXML;
     }
+
+    private function createAboutElement(\DOMDocument $dom, string $about): \DOMElement
+    {
+        $XML = $dom->createElement('about');
+        $XML->setAttribute('value', $about);
+
+        return $XML;
+    }
 }

Some files were not shown because too many files changed in this diff