Skip to content

Commit 09b69af

Browse files
authored
refactor: update CheckPhpIni code (#9672)
1 parent 343b6f7 commit 09b69af

File tree

4 files changed

+77
-87
lines changed

4 files changed

+77
-87
lines changed

system/Security/CheckPhpIni.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use CodeIgniter\View\Table;
1818

1919
/**
20-
* Checks php.ini settings
20+
* Checks php.ini settings in production environment.
2121
*
2222
* @used-by \CodeIgniter\Commands\Utilities\PhpIniCheck
2323
* @see \CodeIgniter\Security\CheckPhpIniTest
@@ -27,30 +27,33 @@ class CheckPhpIni
2727
/**
2828
* @param bool $isCli Set false if you run via Web
2929
*
30-
* @return string|null HTML string or void in CLI
30+
* @return ($isCli is true ? null : string)
3131
*/
3232
public static function run(bool $isCli = true, ?string $argument = null)
3333
{
34-
$output = static::checkIni($argument);
34+
$output = self::checkIni($argument);
3535

3636
$thead = ['Directive', 'Global', 'Current', 'Recommended', 'Remark'];
37-
$tbody = [];
3837

39-
// CLI
4038
if ($isCli) {
41-
self::outputForCli($output, $thead, $tbody);
39+
self::outputForCli($output, $thead);
4240

4341
return null;
4442
}
4543

46-
// Web
47-
return self::outputForWeb($output, $thead, $tbody);
44+
return self::outputForWeb($output, $thead);
4845
}
4946

50-
private static function outputForCli(array $output, array $thead, array $tbody): void
47+
/**
48+
* @param array<string, array{global: string, current: string, recommended: string, remark: string}> $output
49+
* @param array{string, string, string, string, string} $thead
50+
*/
51+
private static function outputForCli(array $output, array $thead): void
5152
{
53+
$tbody = [];
54+
5255
foreach ($output as $directive => $values) {
53-
$current = $values['current'] ?? '';
56+
$current = $values['current'];
5457
$notRecommended = false;
5558

5659
if ($values['recommended'] !== '') {
@@ -64,16 +67,27 @@ private static function outputForCli(array $output, array $thead, array $tbody):
6467
}
6568

6669
$directive = $notRecommended ? CLI::color($directive, 'red') : $directive;
67-
$tbody[] = [
68-
$directive, $values['global'], $current, $values['recommended'], $values['remark'],
70+
71+
$tbody[] = [
72+
$directive,
73+
$values['global'],
74+
$current,
75+
$values['recommended'],
76+
$values['remark'],
6977
];
7078
}
7179

7280
CLI::table($tbody, $thead);
7381
}
7482

75-
private static function outputForWeb(array $output, array $thead, array $tbody): string
83+
/**
84+
* @param array<string, array{global: string, current: string, recommended: string, remark: string}> $output
85+
* @param array{string, string, string, string, string} $thead
86+
*/
87+
private static function outputForWeb(array $output, array $thead): string
7688
{
89+
$tbody = [];
90+
7791
foreach ($output as $directive => $values) {
7892
$current = $values['current'];
7993
$notRecommended = false;
@@ -95,27 +109,27 @@ private static function outputForWeb(array $output, array $thead, array $tbody):
95109
$directive = $notRecommended
96110
? '<span style="color: red">' . $directive . '</span>'
97111
: $directive;
112+
98113
$tbody[] = [
99-
$directive, $values['global'], $current, $values['recommended'], $values['remark'],
114+
$directive,
115+
$values['global'],
116+
$current,
117+
$values['recommended'],
118+
$values['remark'],
100119
];
101120
}
102121

103-
$table = new Table();
104-
$template = [
105-
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
106-
];
107-
$table->setTemplate($template);
108-
122+
$table = new Table();
123+
$table->setTemplate(['table_open' => '<table border="1" cellpadding="4" cellspacing="0">']);
109124
$table->setHeading($thead);
110125

111126
return '<pre>' . $table->generate($tbody) . '</pre>';
112127
}
113128

114129
/**
115-
* @internal Used for testing purposes only.
116-
* @testTag
130+
* @return array<string, array{global: string, current: string, recommended: string, remark: string}>
117131
*/
118-
public static function checkIni(?string $argument = null): array
132+
private static function checkIni(?string $argument = null): array
119133
{
120134
// Default items
121135
$items = [
@@ -151,17 +165,19 @@ public static function checkIni(?string $argument = null): array
151165
'opcache.interned_strings_buffer' => ['recommended' => '16'],
152166
'opcache.max_accelerated_files' => ['remark' => 'Adjust based on the number of PHP files in your project (e.g.: find your_project/ -iname \'*.php\'|wc -l)'],
153167
'opcache.max_wasted_percentage' => ['recommended' => '10'],
154-
'opcache.validate_timestamps' => ['recommended' => '0', 'remark' => 'When you disabled, opcache hold your code into shared memory. Restart webserver needed'],
168+
'opcache.validate_timestamps' => ['recommended' => '0', 'remark' => 'When disabled, opcache will hold your code into shared memory. Restart webserver as needed'],
155169
'opcache.revalidate_freq' => [],
156170
'opcache.file_cache' => ['remark' => 'Location file caching, It should improve performance when SHM memory is full'],
157-
'opcache.file_cache_only' => ['remark' => 'Opcode caching in shared memory, Disabled when you using Windows'],
158-
'opcache.file_cache_fallback' => ['remark' => 'Set enable when you using Windows'],
159-
'opcache.save_comments' => ['recommended' => '0', 'remark' => 'Enable when you using package require docblock annotation'],
171+
'opcache.file_cache_only' => ['remark' => 'Opcode caching in shared memory, Disabled when you are using Windows'],
172+
'opcache.file_cache_fallback' => ['remark' => 'Enable when you are using Windows'],
173+
'opcache.save_comments' => ['recommended' => '0', 'remark' => 'Enable when your code requires to read docblock annotations at runtime'],
160174
];
161175
}
162176

163177
$output = [];
164-
$ini = ini_get_all();
178+
179+
$ini = ini_get_all();
180+
assert(is_array($ini));
165181

166182
foreach ($items as $key => $values) {
167183
$hasKeyInIni = array_key_exists($key, $ini);
@@ -173,7 +189,6 @@ public static function checkIni(?string $argument = null): array
173189
];
174190
}
175191

176-
// [directive => [current_value, recommended_value]]
177192
return $output;
178193
}
179194
}

tests/system/Security/CheckPhpIniTest.php

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

1414
namespace CodeIgniter\Security;
1515

16-
use CodeIgniter\CLI\CLI;
1716
use CodeIgniter\Test\CIUnitTestCase;
18-
use CodeIgniter\Test\Mock\MockInputOutput;
17+
use CodeIgniter\Test\StreamFilterTrait;
1918
use PHPUnit\Framework\Attributes\Group;
2019

2120
/**
@@ -24,53 +23,64 @@
2423
#[Group('Others')]
2524
final class CheckPhpIniTest extends CIUnitTestCase
2625
{
26+
use StreamFilterTrait;
27+
2728
public function testCheckIni(): void
2829
{
29-
$output = CheckPhpIni::checkIni();
30+
$output = self::getPrivateMethodInvoker(CheckPhpIni::class, 'checkIni')();
3031

3132
$expected = [
32-
'global' => '',
33-
'current' => '1',
34-
'recommended' => '0',
33+
'global' => 'UTF-8',
34+
'current' => 'UTF-8',
35+
'recommended' => 'UTF-8',
3536
'remark' => '',
3637
];
37-
$this->assertSame($expected, $output['display_errors']);
38+
$this->assertSame($expected, $output['default_charset']);
3839
}
3940

4041
public function testCheckIniOpcache(): void
4142
{
42-
$output = CheckPhpIni::checkIni('opcache');
43+
$output = self::getPrivateMethodInvoker(CheckPhpIni::class, 'checkIni')('opcache');
4344

4445
$expected = [
4546
'global' => '1',
4647
'current' => '1',
4748
'recommended' => '0',
48-
'remark' => 'Enable when you using package require docblock annotation',
49+
'remark' => 'Enable when your code requires to read docblock annotations at runtime',
4950
];
5051
$this->assertSame($expected, $output['opcache.save_comments']);
5152
}
5253

5354
public function testRunCli(): void
5455
{
55-
// Set MockInputOutput to CLI.
56-
$io = new MockInputOutput();
57-
CLI::setInputOutput($io);
58-
5956
CheckPhpIni::run(true);
6057

61-
// Get the whole output string.
62-
$output = $io->getOutput();
63-
64-
$this->assertStringContainsString('display_errors', $output);
65-
66-
// Remove MockInputOutput.
67-
CLI::resetInputOutput();
58+
$this->assertMatchesRegularExpression(
59+
'/\| Directive\s+\| Global\s+\| Current\s+\| Recommended\s+\| Remark\s+\|/',
60+
$this->getStreamFilterBuffer(),
61+
);
62+
$this->assertMatchesRegularExpression(
63+
'/\| default_charset\s+\| UTF-8\s+\| UTF-8\s+\| UTF-8\s+\| \s+\|/',
64+
$this->getStreamFilterBuffer(),
65+
);
6866
}
6967

7068
public function testRunWeb(): void
7169
{
7270
$output = CheckPhpIni::run(false);
7371

74-
$this->assertStringContainsString('display_errors', (string) $output);
72+
$this->assertIsString($output);
73+
$this->assertMatchesRegularExpression(
74+
'/<table border="1" cellpadding="4" cellspacing="0">/',
75+
$output,
76+
);
77+
$this->assertMatchesRegularExpression(
78+
'/<th>Directive<\/th><th>Global<\/th><th>Current<\/th><th>Recommended<\/th><th>Remark<\/th>/',
79+
$output,
80+
);
81+
$this->assertMatchesRegularExpression(
82+
'/<td>default_charset<\/td><td>UTF-8<\/td><td>UTF-8<\/td><td>UTF-8<\/td><td><\/td>/',
83+
$output,
84+
);
7585
}
7686
}

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 2844 errors
1+
# total 2837 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/missingType.iterableValue.neon

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1416 errors
1+
# total 1409 errors
22

33
parameters:
44
ignoreErrors:
@@ -4552,41 +4552,6 @@ parameters:
45524552
count: 1
45534553
path: ../../system/Router/RouterInterface.php
45544554

4555-
-
4556-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:checkIni\(\) return type has no value type specified in iterable type array\.$#'
4557-
count: 1
4558-
path: ../../system/Security/CheckPhpIni.php
4559-
4560-
-
4561-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:outputForCli\(\) has parameter \$output with no value type specified in iterable type array\.$#'
4562-
count: 1
4563-
path: ../../system/Security/CheckPhpIni.php
4564-
4565-
-
4566-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:outputForCli\(\) has parameter \$tbody with no value type specified in iterable type array\.$#'
4567-
count: 1
4568-
path: ../../system/Security/CheckPhpIni.php
4569-
4570-
-
4571-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:outputForCli\(\) has parameter \$thead with no value type specified in iterable type array\.$#'
4572-
count: 1
4573-
path: ../../system/Security/CheckPhpIni.php
4574-
4575-
-
4576-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:outputForWeb\(\) has parameter \$output with no value type specified in iterable type array\.$#'
4577-
count: 1
4578-
path: ../../system/Security/CheckPhpIni.php
4579-
4580-
-
4581-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:outputForWeb\(\) has parameter \$tbody with no value type specified in iterable type array\.$#'
4582-
count: 1
4583-
path: ../../system/Security/CheckPhpIni.php
4584-
4585-
-
4586-
message: '#^Method CodeIgniter\\Security\\CheckPhpIni\:\:outputForWeb\(\) has parameter \$thead with no value type specified in iterable type array\.$#'
4587-
count: 1
4588-
path: ../../system/Security/CheckPhpIni.php
4589-
45904555
-
45914556
message: '#^Property CodeIgniter\\Session\\Handlers\\BaseHandler\:\:\$savePath type has no value type specified in iterable type array\.$#'
45924557
count: 1

0 commit comments

Comments
 (0)