8000 Use PHPStan's `editorUrl` and add example on how to use Symfony's Console Hyperlinks by ruudk · Pull Request #18 · grifart/phpstan-oneline · GitHub
[go: up one dir, main page]

Skip to content

Use PHPStan's editorUrl and add example on how to use Symfony's Console Hyperlinks #18

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

Closed
wants to merge 2 commits into from

Conversation

ruudk
Copy link
@ruudk ruudk commented Dec 12, 2021

Thanks for this simple but amazing error formatter.

I noticed that clicking the file path in my terminal (iTerm2) would open my default editor (VS Code) instead of my configured editorUrl PHPStorm.

In this PR I added the editorUrl so that it's available inside the formatter. The file and line are replaced and then the editorUrl variable is available in the formatter.

It uses the same replace format as PHPStan does, see: https://github.com/phpstan/phpstan-src/blob/0f2fbfff2d861e17345981f2c02ab7696a2298e3/src/Command/ErrorFormatter/TableErrorFormatter.php#L86

Together with Symfony's Console Hyperlinks you can now easily make the paths clickable for almost all terminals.

parameters:
	editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'
	compact:
		format: "<href={editorUrl}>{path}:{line}</>\n ↳ {error}" # default

Screenshot 2021-12-12 at 12 10 27@2x

@ruudk
Copy link
Author
ruudk commented Dec 18, 2021

@jiripudil What do you think?

)
{
$this->relativePathHelper = $relativePathHelper;
$this->format = $format;
$this->editorUrl = $editorUrl ?? '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this falls back to empty string? I presume symfony prints the escape sequence with an empty href – are terminals ok with it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume, that null variant cannot happen, therefore it should be removed. Because there should be a reasonable default. In case it is missing, maybe write a notice into stderr output?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine as it is. If you want to use the editorUrl then you already know that you need to set it (as per readme). Also, if you are using a custom error formatter, you kind of know what you are doing and also test it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the error message to phpstan output & reasonable default when editorUrl is null.

@jiripudil
Copy link
Collaborator

Hi, lgtm! @jkuchar what do you think?

@jiripudil jiripudil requested a review from jkuchar December 20, 2021 08:14
@@ -48,7 +53,8 @@ public function formatErrors(
$this->format,
[
'{absolutePath}' => $absolutePath,
'{path}' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()),
'{editorUrl}' => str_replace(['%file%', '%line%'], [$absolutePath, $fileSpecificError->getLine() ?? ''], $this->editorUrl),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract into closure/function as parametr expansion can be separated:

$formatEditorUrl = fn(string $path, string $line) => str_replace(['%file%', '%line%'], [$path, $line], $this->editorUrl);

'{editorUrl}' => $formatEditorUrl($absolutePath, (string) ($fileSpecificError->getLine() ?? ''));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to not use this package anymore, and use my own formatter because I wanted to do more modifications. Therefore I have no interest in updating the PR anymore. Should I close it? Or do we merge this as is?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't agree with that the proposed change is a better one tbh.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are those modifications? Feel free to open another PR for them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Color highlighting phpstan/phpstan-src#841 (comment)
Very opinionated and not in the scope of this library I would say.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no problem with keeping this the way it is. I idea here is to keep the formatting logic separated. No big deal.

Copy link
Member
@jkuchar jkuchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jiripudil Could you please fix the null param & merge this?

)
{
$this->relativePathHelper = $relativePathHelper;
$this->format = $format;
$this->editorUrl = $editorUrl ?? '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the error message to phpstan output & reasonable default when editorUrl is null.

@@ -48,7 +53,8 @@ public function formatErrors(
$this->format,
[
'{absolutePath}' => $absolutePath,
'{path}' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()),
'{editorUrl}' => str_replace(['%file%', '%line%'], [$absolutePath, $fileSpecificError->getLine() ?? ''], $this->editorUrl),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are those modifications? Feel free to open another PR for them.

@ruudk ruudk closed this Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0