Skip to content

Commit 44f73c0

Browse files
committed
feature #36 Modernize code base (fabpot)
This PR was squashed before being merged into the master branch. Discussion ---------- Modernize code base Commits ------- 5d8d863 Fix fabbot 9be41b4 Add support for PHPStan fd2a0f7 Modernize code 81bc9a0 Fix CS e631b09 Add Github CI support 0f12cbd Upgrade PHPUnit 98cc4f7 Remove unsupported Twig versions b124f51 Bump min PHP version 3775600 Fix license headers
2 parents f9a7bea + 5d8d863 commit 44f73c0

File tree

14 files changed

+184
-122
lines changed

14 files changed

+184
-122
lines changed

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: "CI"
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
tests:
12+
name: "PHP ${{ matrix.php-version }}"
13+
14+
runs-on: 'ubuntu-latest'
15+
16+
strategy:
17+
matrix:
18+
php-version:
19+
- '8.3'
20+
- '8.4'
21+
22+
steps:
23+
- name: "Checkout code"
24+
uses: actions/checkout@v4
25+
26+
- name: "Install PHP with extensions"
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
coverage: "none"
30+
php-version: ${{ matrix.php-version }}
31+
ini-values: memory_limit=-1
32+
33+
- name: "Add PHPUnit matcher"
34+
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
35+
36+
- run: composer install
37+
38+
- name: "Run tests"
39+
run: vendor/bin/phpunit

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
composer.lock
22
/vendor/
3+
.phpunit.cache

.php-cs-fixer.dist.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return (new PhpCsFixer\Config())
4+
->setRules([
5+
'@PHP71Migration' => true,
6+
'@PHPUnit75Migration:risky' => true,
7+
'@Symfony' => true,
8+
'@Symfony:risky' => true,
9+
'protected_to_private' => false,
10+
])
11+
->setRiskyAllowed(true)
12+
->setFinder(
13+
(new PhpCsFixer\Finder())
14+
->in(__DIR__.'/SensioLabs')
15+
->append([__FILE__])
16+
)
17+
->setCacheFile('.php-cs-fixer.cache')
18+
;

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013 Fabien Potencier
1+
Copyright (c) 2013-present Fabien Potencier
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

SensioLabs/AnsiConverter/AnsiToHtmlConverter.php

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of ansi-to-html.
55
*
6-
* (c) 2013 Fabien Potencier
6+
* (c) Fabien Potencier
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -18,33 +18,30 @@
1818
*/
1919
class AnsiToHtmlConverter
2020
{
21-
protected $theme;
22-
protected $charset;
23-
protected $inlineStyles;
24-
protected $inlineColors;
25-
protected $colorNames;
26-
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;
21+
protected array $inlineColors;
22+
protected array $colorNames;
23+
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();
33-
$this->colorNames = array(
30+
$this->colorNames = [
3431
'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
3532
'', '',
3633
'brblack', 'brred', 'brgreen', 'bryellow', 'brblue', 'brmagenta', 'brcyan', 'brwhite',
37-
);
34+
];
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);
4441
// remove character set sequences
4542
$text = preg_replace('#\e(\(|\))(A|B|[0-2])#', '', $text);
4643

47-
$text = htmlspecialchars($text, PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES, $this->charset);
44+
$text = htmlspecialchars($text, \PHP_VERSION_ID >= 50400 ? \ENT_QUOTES | \ENT_SUBSTITUTE : \ENT_QUOTES, $this->charset);
4845

4946
// carriage return
5047
$text = preg_replace('#^.*\r(?!\n)#m', '', $text);
@@ -56,7 +53,7 @@ public function convert($text)
5653
if ('backspace' == $token[0]) {
5754
$j = $i;
5855
while (--$j >= 0) {
59-
if ('text' == $tokens[$j][0] && strlen($tokens[$j][1]) > 0) {
56+
if ('text' == $tokens[$j][0] && '' !== $tokens[$j][1]) {
6057
$tokens[$j][1] = substr($tokens[$j][1], 0, -1);
6158

6259
break;
@@ -75,9 +72,9 @@ public function convert($text)
7572
}
7673

7774
if ($this->inlineStyles) {
78-
$html = sprintf('<span style="background-color: %s; color: %s">%s</span>', $this->inlineColors['black'], $this->inlineColors['white'], $html);
75+
$html = \sprintf('<span style="background-color: %s; color: %s">%s</span>', $this->inlineColors['black'], $this->inlineColors['white'], $html);
7976
} else {
80-
$html = sprintf('<span class="ansi_color_bg_black ansi_color_fg_white">%s</span>', $html);
77+
$html = \sprintf('<span class="ansi_color_bg_black ansi_color_fg_white">%s</span>', $html);
8178
}
8279

8380
// remove empty span
@@ -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;
@@ -102,6 +99,7 @@ protected function convertAnsiToColor($ansi)
10299
$options = explode(';', $ansi);
103100

104101
foreach ($options as $key => $option) {
102+
$option = (int) $option;
105103
if ($option >= 30 && $option < 38) {
106104
$fg = $option - 30;
107105
} elseif ($option >= 40 && $option < 48) {
@@ -135,75 +133,75 @@ protected function convertAnsiToColor($ansi)
135133
}
136134

137135
// options: bold => 1, dim => 2, italic => 3, underscore => 4, blink => 5, rapid blink => 6, reverse => 7, conceal => 8, strikethrough => 9
138-
if (in_array(1, $options) || $hi) { // high intensity equals regular bold
136+
if (\in_array(1, $options) || $hi) { // high intensity equals regular bold
139137
$fg += 10;
140138
// bold does not affect background color
141139
}
142140

143-
if (in_array(2, $options)) { // dim
141+
if (\in_array(2, $options)) { // dim
144142
$fg = ($fg >= 10) ? $fg - 10 : $fg;
145143
}
146144

147-
if (in_array(3, $options)) {
145+
if (\in_array(3, $options)) {
148146
$as .= '; font-style: italic';
149147
$cs .= ' ansi_color_italic';
150148
}
151149

152-
if (in_array(4, $options)) {
150+
if (\in_array(4, $options)) {
153151
$as .= '; text-decoration: underline';
154152
$cs .= ' ansi_color_underline';
155153
}
156154

157-
if (in_array(9, $options)) {
155+
if (\in_array(9, $options)) {
158156
$as .= '; text-decoration: line-through';
159157
$cs .= ' ansi_color_strikethrough';
160158
}
161159

162-
if (in_array(7, $options)) {
160+
if (\in_array(7, $options)) {
163161
$tmp = $fg;
164162
$fg = $bg;
165163
$bg = $tmp;
166164
}
167165
}
168166

169167
if ($this->inlineStyles) {
170-
return sprintf('</span><span style="background-color: %s; color: %s%s">', $this->inlineColors[$this->colorNames[$bg]], $this->inlineColors[$this->colorNames[$fg]], $as);
168+
return \sprintf('</span><span style="background-color: %s; color: %s%s">', $this->inlineColors[$this->colorNames[$bg]], $this->inlineColors[$this->colorNames[$fg]], $as);
171169
} else {
172-
return sprintf('</span><span class="ansi_color_bg_%s ansi_color_fg_%s%s">', $this->colorNames[$bg], $this->colorNames[$fg], $cs);
170+
return \sprintf('</span><span class="ansi_color_bg_%s ansi_color_fg_%s%s">', $this->colorNames[$bg], $this->colorNames[$fg], $cs);
173171
}
174172
}
175173

176-
protected function tokenize($text)
174+
protected function tokenize(string $text): array
177175
{
178-
$tokens = array();
179-
preg_match_all("/(?:\e\[(.*?)m|(\x08))/", $text, $matches, PREG_OFFSET_CAPTURE);
176+
$tokens = [];
177+
preg_match_all("/(?:\e\[(.*?)m|(\x08))/", $text, $matches, \PREG_OFFSET_CAPTURE);
180178

181-
$codes = array();
179+
$codes = [];
182180
$offset = 0;
183181
foreach ($matches[0] as $i => $match) {
184182
if ($match[1] - $offset > 0) {
185-
$tokens[] = array('text', substr($text, $offset, $match[1] - $offset));
183+
$tokens[] = ['text', substr($text, $offset, $match[1] - $offset)];
186184
}
187185

188186
foreach (explode(';', $matches[1][$i][0]) as $code) {
189187
if ('0' == $code || '' == $code) {
190-
$codes = array();
188+
$codes = [];
191189
} else {
192190
// remove existing occurrence to avoid processing duplicate styles
193-
if (in_array($code, $codes) && ($key = array_search($code, $codes)) !== false) {
191+
if (\in_array($code, $codes) && ($key = array_search($code, $codes)) !== false) {
194192
unset($codes[$key]);
195193
}
196194
}
197195

198196
$codes[] = $code;
199197
}
200198

201-
$tokens[] = array("\x08" == $match[0] ? 'backspace' : 'color', implode(';', $codes));
202-
$offset = $match[1] + strlen($match[0]);
199+
$tokens[] = ["\x08" == $match[0] ? 'backspace' : 'color', implode(';', $codes)];
200+
$offset = $match[1] + \strlen($match[0]);
203201
}
204202

205-
if ($offset < strlen($text)) {
206-
$tokens[] = array('text', substr($text, $offset));
203+
if ($offset < \strlen($text)) {
204+
$tokens[] = ['text', substr($text, $offset)];
207205
}
208206

209207
return $tokens;

SensioLabs/AnsiConverter/Bridge/Twig/AnsiExtension.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of ansi-to-html.
5+
*
6+
* (c) Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace SensioLabs\AnsiConverter\Bridge\Twig;
413

514
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
@@ -9,12 +18,9 @@
918

1019
class AnsiExtension extends AbstractExtension
1120
{
12-
/** @var AnsiToHtmlConverter */
13-
private $converter;
14-
15-
public function __construct(?AnsiToHtmlConverter $converter = null)
16-
{
17-
$this->converter = $converter ?: new AnsiToHtmlConverter();
21+
public function __construct(
22+
private AnsiToHtmlConverter $converter = new AnsiToHtmlConverter(),
23+
) {
1824
}
1925

2026
public function getFilters(): array
@@ -40,9 +46,4 @@ public function css(): string
4046
{
4147
return $this->converter->getTheme()->asCss();
4248
}
43-
44-
public function getName(): string
45-
{
46-
return 'sensiolabs_ansi';
47-
}
4849
}

0 commit comments

Comments
 (0)