Skip to content

Commit fd2a0f7

Browse files
committed
Modernize code
1 parent 81bc9a0 commit fd2a0f7

File tree

7 files changed

+21
-33
lines changed

7 files changed

+21
-33
lines changed

SensioLabs/AnsiConverter/AnsiToHtmlConverter.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
*/
1919
class AnsiToHtmlConverter
2020
{
21-
protected $theme;
22-
protected $charset;
23-
protected $inlineStyles;
2421
protected $inlineColors;
2522
protected $colorNames;
2623

27-
public function __construct(?Theme $theme = null, $inlineStyles = true, $charset = 'UTF-8')
28-
{
29-
$this->theme = null === $theme ? new Theme() : $theme;
30-
$this->inlineStyles = $inlineStyles;
31-
$this->charset = $charset;
24+
public function __construct(
25+
protected Theme $theme = new Theme(),
26+
protected bool $inlineStyles = true,
27+
protected string $charset = 'UTF-8',
28+
) {
3229
$this->inlineColors = $this->theme->asArray();
3330
$this->colorNames = [
3431
'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
@@ -37,7 +34,7 @@ public function __construct(?Theme $theme = null, $inlineStyles = true, $charset
3734
];
3835
}
3936

40-
public function convert($text)
37+
public function convert(string $text): string
4138
{
4239
// remove cursor movement sequences
4340
$text = preg_replace('#\e\[(K|s|u|2J|2K|\d+(A|B|C|D|E|F|G|J|K|S|T)|\d+;\d+(H|f))#', '', $text);
@@ -86,12 +83,12 @@ public function convert($text)
8683
return $html;
8784
}
8885

89-
public function getTheme()
86+
public function getTheme(): Theme
9087
{
9188
return $this->theme;
9289
}
9390

94-
protected function convertAnsiToColor($ansi)
91+
protected function convertAnsiToColor(string $ansi): string
9592
{
9693
$bg = 0;
9794
$fg = 7;
@@ -173,7 +170,7 @@ protected function convertAnsiToColor($ansi)
173170
}
174171
}
175172

176-
protected function tokenize($text)
173+
protected function tokenize(string $text): array
177174
{
178175
$tokens = [];
179176
preg_match_all("/(?:\e\[(.*?)m|(\x08))/", $text, $matches, \PREG_OFFSET_CAPTURE);

SensioLabs/AnsiConverter/Bridge/Twig/AnsiExtension.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818

1919
class AnsiExtension extends AbstractExtension
2020
{
21-
/** @var AnsiToHtmlConverter */
22-
private $converter;
23-
24-
public function __construct(?AnsiToHtmlConverter $converter = null)
25-
{
26-
$this->converter = $converter ?: new AnsiToHtmlConverter();
21+
public function __construct(
22+
private AnsiToHtmlConverter $converter = new AnsiToHtmlConverter(),
23+
) {
2724
}
2825

2926
public function getFilters(): array
@@ -49,9 +46,4 @@ public function css(): string
4946
{
5047
return $this->converter->getTheme()->asCss();
5148
}
52-
53-
public function getName(): string
54-
{
55-
return 'sensiolabs_ansi';
56-
}
5749
}

SensioLabs/AnsiConverter/Tests/AnsiToHtmlConverterTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111

1212
namespace SensioLabs\AnsiConverter\Tests;
1313

14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
1516
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
1617

1718
class AnsiToHtmlConverterTest extends TestCase
1819
{
19-
/**
20-
* @dataProvider getConvertData
21-
*/
22-
public function testConvert($expected, $input)
20+
#[DataProvider('getConvertData')]
21+
public function testConvert(string $expected, string $input): void
2322
{
2423
$converter = new AnsiToHtmlConverter();
2524
$this->assertEquals($expected, $converter->convert($input));
2625
}
2726

28-
public static function getConvertData()
27+
public static function getConvertData(): array
2928
{
3029
return [
3130
// text is escaped

SensioLabs/AnsiConverter/Theme/AnsiTheme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class AnsiTheme extends Theme
1818
{
19-
public function asArray()
19+
public function asArray(): array
2020
{
2121
return [
2222
// normal

SensioLabs/AnsiConverter/Theme/SolarizedTheme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class SolarizedTheme extends Theme
2020
{
21-
public function asArray()
21+
public function asArray(): array
2222
{
2323
return [
2424
// normal

SensioLabs/AnsiConverter/Theme/SolarizedXTermTheme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class SolarizedXTermTheme extends Theme
2020
{
21-
public function asArray()
21+
public function asArray(): array
2222
{
2323
return [
2424
// normal

SensioLabs/AnsiConverter/Theme/Theme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class Theme
1818
{
19-
public function asCss($prefix = 'ansi_color')
19+
public function asCss(string $prefix = 'ansi_color'): string
2020
{
2121
$css = [];
2222
foreach ($this->asArray() as $name => $color) {
@@ -31,7 +31,7 @@ public function asCss($prefix = 'ansi_color')
3131
return implode("\n", $css);
3232
}
3333

34-
public function asArray()
34+
public function asArray(): array
3535
{
3636
return [
3737
'black' => 'black',

0 commit comments

Comments
 (0)