Skip to content

Commit c3cc3fe

Browse files
authored
refactor: fix and micro-optimize code in Format (#9583)
1 parent c65b238 commit c3cc3fe

File tree

9 files changed

+40
-88
lines changed

9 files changed

+40
-88
lines changed

system/Format/Exceptions/FormatException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function forInvalidFormatter(string $class)
3737
* Thrown in JSONFormatter when the json_encode produces
3838
* an error code other than JSON_ERROR_NONE and JSON_ERROR_RECURSION.
3939
*
40-
* @param string $error The error message
40+
* @param string|null $error The error message
4141
*
4242
* @return static
4343
*/

system/Format/Format.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,8 @@
2323
*/
2424
class Format
2525
{
26-
/**
27-
* Configuration instance
28-
*
29-
* @var FormatConfig
30-
*/
31-
protected $config;
32-
33-
/**
34-
* Constructor.
35-
*/
36-
public function __construct(FormatConfig $config)
26+
public function __construct(protected FormatConfig $config)
3727
{
38-
$this->config = $config;
3928
}
4029

4130
/**

system/Format/FormatterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ interface FormatterInterface
2121
/**
2222
* Takes the given data and formats it.
2323
*
24-
* @param array|object|string $data
24+
* @param array<array-key, mixed>|object|string $data
2525
*
26-
* @return false|string
26+
* @return false|non-empty-string
2727
*/
2828
public function format($data);
2929
}

system/Format/JSONFormatter.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class JSONFormatter implements FormatterInterface
2626
/**
2727
* Takes the given data and formats it.
2828
*
29-
* @param array|bool|float|int|object|string|null $data
29+
* @param array<array-key, mixed>|object|string $data
3030
*
31-
* @return false|string (JSON string | false)
31+
* @return false|non-empty-string
3232
*/
3333
public function format($data)
3434
{
@@ -37,7 +37,9 @@ public function format($data)
3737
$options = $config->formatterOptions['application/json'] ?? JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
3838
$options |= JSON_PARTIAL_OUTPUT_ON_ERROR;
3939

40-
$options = ENVIRONMENT === 'production' ? $options : $options | JSON_PRETTY_PRINT;
40+
if (ENVIRONMENT !== 'production') {
41+
$options |= JSON_PRETTY_PRINT;
42+
}
4143

4244
$result = json_encode($data, $options, 512);
4345

system/Format/XMLFormatter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class XMLFormatter implements FormatterInterface
2727
/**
2828
* Takes the given data and formats it.
2929
*
30-
* @param array|bool|float|int|object|string|null $data
30+
* @param array<array-key, mixed>|object|string $data
3131
*
32-
* @return false|string (XML string | false)
32+
* @return false|non-empty-string
3333
*/
3434
public function format($data)
3535
{
@@ -56,7 +56,8 @@ public function format($data)
5656
*
5757
* @see http://www.codexworld.com/convert-array-to-xml-in-php/
5858
*
59-
* @param SimpleXMLElement $output
59+
* @param array<array-key, mixed> $data
60+
* @param SimpleXMLElement $output
6061
*
6162
* @return void
6263
*/

tests/system/Format/JSONFormatterTest.php

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
namespace CodeIgniter\Format;
1515

16-
use CodeIgniter\Exceptions\RuntimeException;
16+
use CodeIgniter\Format\Exceptions\FormatException;
1717
use CodeIgniter\Test\CIUnitTestCase;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Group;
1920

2021
/**
@@ -31,51 +32,34 @@ protected function setUp(): void
3132
$this->jsonFormatter = new JSONFormatter();
3233
}
3334

34-
public function testBasicJSON(): void
35+
/**
36+
* @param array<string, string> $data
37+
*/
38+
#[DataProvider('provideFormattingToJson')]
39+
public function testFormattingToJson(array $data, string $expected): void
3540
{
36-
$data = [
37-
'foo' => 'bar',
38-
];
39-
40-
$expected = '{
41-
"foo": "bar"
42-
}';
43-
4441
$this->assertSame($expected, $this->jsonFormatter->format($data));
4542
}
4643

47-
public function testUnicodeOutput(): void
44+
/**
45+
* @return iterable<string, array{0: array<string, string>, 1: string}>
46+
*/
47+
public static function provideFormattingToJson(): iterable
4848
{
49-
$data = [
50-
'foo' => 'База данни грешка',
51-
];
49+
yield 'empty array' => [[], '[]'];
5250

53-
$expected = '{
54-
"foo": "База данни грешка"
55-
}';
51+
yield 'simple array' => [['foo' => 'bar'], "{\n \"foo\": \"bar\"\n}"];
5652

57-
$this->assertSame($expected, $this->jsonFormatter->format($data));
58-
}
53+
yield 'unicode array' => [['foo' => 'База данни грешка'], "{\n \"foo\": \"База данни грешка\"\n}"];
5954

60-
public function testKeepsURLs(): void
61-
{
62-
$data = [
63-
'foo' => 'https://www.example.com/foo/bar',
64-
];
65-
66-
$expected = '{
67-
"foo": "https://www.example.com/foo/bar"
68-
}';
69-
70-
$this->assertSame($expected, $this->jsonFormatter->format($data));
55+
yield 'url array' => [['foo' => 'https://www.example.com/foo/bar'], "{\n \"foo\": \"https://www.example.com/foo/bar\"\n}"];
7156
}
7257

73-
public function testJSONError(): void
58+
public function testJSONFormatterThrowsError(): void
7459
{
75-
$this->expectException(RuntimeException::class);
60+
$this->expectException(FormatException::class);
61+
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
7662

77-
$data = ["\xB1\x31"];
78-
$expected = 'Boom';
79-
$this->assertSame($expected, $this->jsonFormatter->format($data));
63+
$this->assertSame('Boom', $this->jsonFormatter->format(["\xB1\x31"]));
8064
}
8165
}

tests/system/Format/XMLFormatterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public function testValidatingXmlTags(): void
104104
$this->assertSame($expected, $this->xmlFormatter->format($data));
105105
}
106106

107+
/**
108+
* @param array<string, string> $input
109+
*/
107110
#[DataProvider('provideValidatingInvalidTags')]
108111
public function testValidatingInvalidTags(string $expected, array $input): void
109112
{
@@ -116,6 +119,9 @@ public function testValidatingInvalidTags(string $expected, array $input): void
116119
$this->assertSame($expectedXML, $this->xmlFormatter->format($input));
117120
}
118121

122+
/**
123+
* @return iterable<int, array{0: string, 1: array<string, string>}>
124+
*/
119125
public static function provideValidatingInvalidTags(): iterable
120126
{
121127
return [

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3262 errors
1+
# total 3256 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/missingType.iterableValue.neon

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1571 errors
1+
# total 1565 errors
22

33
parameters:
44
ignoreErrors:
@@ -3052,26 +3052,6 @@ parameters:
30523052
count: 1
30533053
path: ../../system/Filters/PerformanceMetrics.php
30543054

3055-
-
3056-
message: '#^Method CodeIgniter\\Format\\FormatterInterface\:\:format\(\) has parameter \$data with no value type specified in iterable type array\.$#'
3057-
count: 1
3058-
path: ../../system/Format/FormatterInterface.php
3059-
3060-
-
3061-
message: '#^Method CodeIgniter\\Format\\JSONFormatter\:\:format\(\) has parameter \$data with no value type specified in iterable type array\.$#'
3062-
count: 1
3063-
path: ../../system/Format/JSONFormatter.php
3064-
3065-
-
3066-
message: '#^Method CodeIgniter\\Format\\XMLFormatter\:\:arrayToXML\(\) has parameter \$data with no value type specified in iterable type array\.$#'
3067-
count: 1
3068-
path: ../../system/Format/XMLFormatter.php
3069-
3070-
-
3071-
message: '#^Method CodeIgniter\\Format\\XMLFormatter\:\:format\(\) has parameter \$data with no value type specified in iterable type array\.$#'
3072-
count: 1
3073-
path: ../../system/Format/XMLFormatter.php
3074-
30753055
-
30763056
message: '#^Method CodeIgniter\\HTTP\\CLIRequest\:\:getArgs\(\) return type has no value type specified in iterable type array\.$#'
30773057
count: 1
@@ -6517,16 +6497,6 @@ parameters:
65176497
count: 1
65186498
path: ../../tests/system/Filters/InvalidCharsTest.php
65196499

6520-
-
6521-
message: '#^Method CodeIgniter\\Format\\XMLFormatterTest\:\:provideValidatingInvalidTags\(\) return type has no value type specified in iterable type iterable\.$#'
6522-
count: 1
6523-
path: ../../tests/system/Format/XMLFormatterTest.php
6524-
6525-
-
6526-
message: '#^Method CodeIgniter\\Format\\XMLFormatterTest\:\:testValidatingInvalidTags\(\) has parameter \$input with no value type specified in iterable type array\.$#'
6527-
count: 1
6528-
path: ../../tests/system/Format/XMLFormatterTest.php
6529-
65306500
-
65316501
message: '#^Method CodeIgniter\\HTTP\\IncomingRequestTest\:\:provideCanGrabGetRawInputVar\(\) return type has no value type specified in iterable type iterable\.$#'
65326502
count: 1

0 commit comments

Comments
 (0)