Skip to content

Commit d015e49

Browse files
committed
refactor: update CheckPhpIni code
1 parent bd4c41d commit d015e49

File tree

4 files changed

+81
-89
lines changed

4 files changed

+81
-89
lines changed

system/Security/CheckPhpIni.php

Lines changed: 48 additions & 31 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);
42-
43-
return null;
39+
return self::outputForCli($output, $thead);
4440
}
4541

46-
// Web
47-
return self::outputForWeb($output, $thead, $tbody);
42+
return self::outputForWeb($output, $thead);
4843
}
4944

50-
private static function outputForCli(array $output, array $thead, array $tbody): void
45+
/**
46+
* @param array<string, array{global: string, current: string, recommended: string, remark: string}> $output
47+
* @param array{string, string, string, string, string} $thead
48+
*
49+
* @return null
50+
*/
51+
private static function outputForCli(array $output, array $thead)
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,29 @@ 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);
81+
82+
return null;
7383
}
7484

75-
private static function outputForWeb(array $output, array $thead, array $tbody): string
85+
/**
86+
* @param array<string, array{global: string, current: string, recommended: string, remark: string}> $output
87+
* @param array{string, string, string, string, string} $thead
88+
*/
89+
private static function outputForWeb(array $output, array $thead): string
7690
{
91+
$tbody = [];
92+
7793
foreach ($output as $directive => $values) {
7894
$current = $values['current'];
7995
$notRecommended = false;
@@ -95,27 +111,27 @@ private static function outputForWeb(array $output, array $thead, array $tbody):
95111
$directive = $notRecommended
96112
? '<span style="color: red">' . $directive . '</span>'
97113
: $directive;
114+
98115
$tbody[] = [
99-
$directive, $values['global'], $current, $values['recommended'], $values['remark'],
116+
$directive,
117+
$values['global'],
118+
$current,
119+
$values['recommended'],
120+
$values['remark'],
100121
];
101122
}
102123

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

111128
return '<pre>' . $table->generate($tbody) . '</pre>';
112129
}
113130

114131
/**
115-
* @internal Used for testing purposes only.
116-
* @testTag
132+
* @return array<string, array{global: string, current: string, recommended: string, remark: string}>
117133
*/
118-
public static function checkIni(?string $argument = null): array
134+
private static function checkIni(?string $argument = null): array
119135
{
120136
// Default items
121137
$items = [
@@ -151,17 +167,19 @@ public static function checkIni(?string $argument = null): array
151167
'opcache.interned_strings_buffer' => ['recommended' => '16'],
152168
'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)'],
153169
'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'],
170+
'opcache.validate_timestamps' => ['recommended' => '0', 'remark' => 'When disabled, opcache will hold your code into shared memory. Restart webserver as needed'],
155171
'opcache.revalidate_freq' => [],
156172
'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'],
173+
'opcache.file_cache_only' => ['remark' => 'Opcode caching in shared memory, Disabled when you are using Windows'],
174+
'opcache.file_cache_fallback' => ['remark' => 'Enable when you are using Windows'],
175+
'opcache.save_comments' => ['recommended' => '0', 'remark' => 'Enable when your code requires to read docblock annotations at runtime'],
160176
];
161177
}
162178

163179
$output = [];
164-
$ini = ini_get_all();
180+
181+
$ini = ini_get_all();
182+
assert(is_array($ini));
165183

166184
foreach ($items as $key => $values) {
167185
$hasKeyInIni = array_key_exists($key, $ini);
@@ -173,7 +191,6 @@ public static function checkIni(?string $argument = null): array
173191
];
174192
}
175193

176-
// [directive => [current_value, recommended_value]]
177194
return $output;
178195
}
179196
}

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)