Skip to content

remove manual wrapping for error table #4035

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from

Conversation

schlndh
Copy link
Contributor

@schlndh schlndh commented May 30, 2025

The two issues mentioned in the comments were fixed years ago. Now it seems that Symfony's wrapping is superior to the PHPStan's.

Motivation:

  • PHPStan's wrapping uses wordwrap, which doesn't work if there are no whitespaces (typically errors involving long unions).
  • I wanted to try creating a custom version of TableErrorFormatter which would put the editor links on the line numbers, instead of showing them separately (I find that distracting). But with the current ErrorConsoleStyle it causes the table to become very narrow, because the hyperlink is included in the computed width of the Line column.

Results:

Before After
error_old error_new
links_old links_new

Here are the test files I used to generate the screenshots:

<?php

namespace Foo;

class Bar
{
	public const CONST_1 = 'aaaa';
	public const CONST_2 = 'bbbb';
	public const CONST_3 = 'cccc';
	public const CONST_4 = 'dddd';
	public const CONST_5 = 'eeee';
	public const CONST_6 = 'ffff';
	public const CONST_7 = 'gggg';
	public const CONST_8 = 'hhhh';
	public const CONST_9 = 'iiii';
	public const CONST_10 = 'jjjj';
	public const CONST_11 = 'kkkk';
	public const CONST_12 = 'llll';
	public const CONST_13 = 'mmmm';
	public const CONST_14 = 'nnnn';
	public const CONST_15 = 'oooo';
	public const CONST_16 = 'pppp';
	public const CONST_17 = 'qqqq';
	public const CONST_18 = 'rrrr';
	public const CONST_19 = 'ssss';
	public const CONST_20 = 'tttt';
}

/** @phpstan-param Bar::CONST_* $i */
function printNum(string $i): void
{
	echo $i;
}

printNum((string) random_int(0, 100));
<?php declare(strict_types = 1);

use PHPStan\Command\ErrorsConsoleStyle;
use PHPStan\Command\Symfony\SymfonyOutput;
use PHPStan\Command\Symfony\SymfonyStyle;

require_once __DIR__ . '/vendor/autoload.php';

$output = new \Symfony\Component\Console\Output\ConsoleOutput();
$input = new \Symfony\Component\Console\Input\ArgvInput();
$stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output)));
$stdOutput->getStyle()->table(
	['Line', 'path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php (in context of class aksdjlajdljasdlkjaldkjaldajdlkaj\alksjdalksdjlasdlajk\asldjkaskldj)'],
	[
		[
			'<href="editor://open?file=foo/bar/baz.php&line=12">12</>',
			"Long error message asdalksdj lkajsdl aksjd lakjsdl jasld kjalksd jlaskjd lakjd lkasjd lkajsd lkajsd lkaj dlkajsd klaj dlkaj d"
			. "\n✏️ <href=\"editor://open?=file=path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php&line=12\">"
			. "path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php</>",
		],
		[
			'<href="editor://open?file=foo/bar/baz.php&line=12">12</>',
			"Same link, but shorter title."
			. "\n✏️ <href=\"editor://open?=file=path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php&line=12\">"
			. "short title</>",
		],
	],
);

$stdOutput->writeLineFormatted('');
$stdOutput->writeLineFormatted('Table without Line links');
$stdOutput->writeLineFormatted('');

$stdOutput->getStyle()->table(
	['Line', 'path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php (in context of class aksdjlajdljasdlkjaldkjaldajdlkaj\alksjdalksdjlasdlajk\asldjkaskldj)'],
	[
		[
			'12',
			"Long error message asdalksdj lkajsdl aksjd lakjsdl jasld kjalksd jlaskjd lakjd lkasjd lkajsd lkajsd lkaj dlkajsd klaj dlkaj d"
			. "\n✏️ <href=\"editor://open?=file=path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php&line=12\">"
			. "path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php</>",
		],
		[
			'123456789',
			"Same link, but shorter title."
			. "\n✏️ <href=\"editor://open?=file=path/foo/bar/baz/adlkjhasdkjhkajhsdkjad/adkljalskdjalkd/asdlkjaldjk/adasdasd/alkjdlkajsdlkjalkdjalkjdslkja/asdasdadasdasd/qeqwqeqasdasdada/Foo.php&line=12\">"
			. "short title</>",
		],
	],
);

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.

1 participant