-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: Clickable rule names in TextReporter
pointing to official documentation
#8003
Changes from 8 commits
773b0b0
c846f61
d0920ee
b0516bd
3ccb518
8919afb
175e8a2
48b82ca
0199bab
18091f5
87734b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
namespace PhpCsFixer\Console\Report\FixReport; | ||
|
||
use PhpCsFixer\Differ\DiffConsoleFormatter; | ||
use Symfony\Component\Console\Formatter\OutputFormatter; | ||
|
||
/** | ||
* @author Boris Gorbylev <[email protected]> | ||
|
@@ -41,6 +42,7 @@ public function generate(ReportSummary $reportSummary): string | |
$output .= $this->getAppliedFixers( | ||
$reportSummary->isDecoratedOutput(), | ||
$fixResult['appliedFixers'], | ||
$fixResult['extraInfoFixers'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really wonder if we need to change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will let you think again then ... |
||
); | ||
} | ||
|
||
|
@@ -58,13 +60,32 @@ public function generate(ReportSummary $reportSummary): string | |
} | ||
|
||
/** | ||
* @param list<string> $appliedFixers | ||
* @param list<string> $appliedFixers | ||
* @param array{helpUri: array<string, string>|array{}}|array{} $extraInfoFixers | ||
*/ | ||
private function getAppliedFixers(bool $isDecoratedOutput, array $appliedFixers): string | ||
private function getAppliedFixers(bool $isDecoratedOutput, array $appliedFixers, array $extraInfoFixers = []): string | ||
{ | ||
if (!isset($extraInfoFixers['helpUri'])) { | ||
return sprintf( | ||
$isDecoratedOutput ? ' (<comment>%s</comment>)' : ' (%s)', | ||
implode(', ', $appliedFixers) | ||
); | ||
} | ||
|
||
$fixers = []; | ||
|
||
foreach ($appliedFixers as $appliedFixer) { | ||
$url = $extraInfoFixers['helpUri'][$appliedFixer] ?? ''; | ||
if ($isDecoratedOutput && '' !== $url) { | ||
$fixers[] = sprintf('<href=%s;fg=yellow>%s</>', OutputFormatter::escape($url), $appliedFixer); | ||
} else { | ||
$fixers[] = $appliedFixer; | ||
} | ||
} | ||
|
||
return sprintf( | ||
$isDecoratedOutput ? ' (<comment>%s</comment>)' : ' (%s)', | ||
implode(', ', $appliedFixers) | ||
' (<comment>%s</comment>)', | ||
implode(', ', $fixers) | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,12 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void; | |
*/ | ||
public function getDefinition(): FixerDefinitionInterface; | ||
|
||
/** | ||
* Returns the absolute URI [RFC3986](http://www.rfc-editor.org/info/rfc3986) | ||
* of the primary documentation for the fixer. | ||
*/ | ||
public function getHelpUri(): string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've recently released https://github.com/llaville/sarif-php-sdk/releases/tag/1.2.0 that will add SARIF support to most common PHP linters. I've prepared a All, to said that SARIF used an helpUri property to enforce online documentation. This is also my goal with PHP-CS-Fixer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that, but it does not change the fact it's a breaking change and requires major release, which we don't want now, as it would require massive amount of work to clean the codebase from deprecations Having said that, we could make it part of a contract, but using composition - we can introduce a new interface, dedicated only for that, implement it internally to satisfy the reporter's needs, but at the same time let people implement it at their own pace. That would require checking if the fixer implements this interface while preparing the output and linking only these rules that provide a doc URL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with you to do it, but I'm sorry I won't have enough free time today ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a problem, everyone's busy. Let's continue as fast as possible, but without any rush There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Wirone I'll work now on your idea to split interfaces to have something like : <?php
namespace PhpCsFixer\Fixer;
interface FixerReportInterface
{
public function getHelpUri(): string;
public function getExtraInfoFixers(): array;
} The This interface will be usefull, in state only for |
||
|
||
/** | ||
* Returns the name of the fixer. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,7 @@ | |
} | ||
|
||
/** | ||
* @return array<string, array{appliedFixers: list<string>, diff: string}> | ||
* @return array<string, array{appliedFixers: list<string>, diff: string, extraInfoFixers: array{helpUri: array<string, string>}|array{}}> | ||
*/ | ||
public function fix(): array | ||
{ | ||
|
@@ -130,7 +130,7 @@ | |
} | ||
|
||
/** | ||
* @return null|array{appliedFixers: list<string>, diff: string} | ||
* @return null|array{appliedFixers: list<string>, diff: string, extraInfoFixers: array{helpUri: array<string, string>}|array{}} | ||
*/ | ||
private function fixFile(\SplFileInfo $file, LintingResultInterface $lintingResult): ?array | ||
{ | ||
|
@@ -158,8 +158,9 @@ | |
$newHash = $oldHash; | ||
|
||
$appliedFixers = []; | ||
$extraInfoFixers = []; | ||
|
||
try { | ||
Check warning on line 163 in src/Runner/Runner.php
|
||
foreach ($this->fixers as $fixer) { | ||
// for custom fixers we don't know is it safe to run `->fix()` without checking `->supports()` and `->isCandidate()`, | ||
// thus we need to check it and conditionally skip fixing | ||
|
@@ -176,6 +177,10 @@ | |
$tokens->clearEmptyTokens(); | ||
$tokens->clearChanged(); | ||
$appliedFixers[] = $fixer->getName(); | ||
|
||
if (!isset($extraInfoFixers['helpUri'][$fixer->getName()])) { | ||
$extraInfoFixers['helpUri'][$fixer->getName()] = $fixer->getHelpUri(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only place There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kubawerlos are you familiar with this comment? Because info about URIs would be used with another, dedicated reporter too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I did not verify it against that concept, so maybe you're right it would also work for it 🙂. Just wanted to make sure you have the context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw it, and as in https://github.com/llaville/sarif-php-sdk/blob/1.2.0/src/Converter/PhpStanConverter.php the URLs are in there, not coming from PHPStan, so my understanding is that both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll give more explains later, especially in context of SARIF reports. |
||
} | ||
} | ||
} | ||
} catch (\ParseError $e) { | ||
|
@@ -208,6 +213,7 @@ | |
$fixInfo = [ | ||
'appliedFixers' => $appliedFixers, | ||
'diff' => $this->differ->diff($old, $new, $file), | ||
'extraInfoFixers' => $extraInfoFixers, | ||
]; | ||
|
||
try { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not:
or
The first one make the
helpUri
nullable, the second one makes it optional. In both cases we can detect if there's info with URIs or not. I also findextraInfoFixers
superfluous, I would rather add one level field likehelpUris
(plural, as it's a collection).In fact, I would like to do
appliedFixers: list<array{name: string, helpUri: string}>
, butRunner
also is not internal so we can't change the format of the returned array. Adding new field should be OK, though.@keradus please let me know what you think 🙂.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Wirone Stop wasting your time with this current PR. Even if code is almost ready, I should change the
extraInfoFixers
.I'll give more explains when my new version will be ready. I hope to propose something in next hours (now, I'm back)