Skip to content

Commit c54551f

Browse files
committed
Apply native_function_invocation in PHPCS
Some built-in functions can be turned into specific opcodes at compile time when the compiler is sure that they will be used (i.e. no namespace fallback is needed). This can have a major performance impact when they appear in hot paths.
1 parent 9aa2a6f commit c54551f

File tree

207 files changed

+2240
-2236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+2240
-2236
lines changed

.github/workflows/php-cs-fixer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ jobs:
3131
- name: PHP-CS-Fixer
3232
uses: docker://oskarstark/php-cs-fixer-ga
3333
with:
34-
args: --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no ${{ env.PHPCS_EXTRA_ARGS }}
34+
args: --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no --allow-risky=yes ${{ env.PHPCS_EXTRA_ARGS }}

.php-cs-fixer.dist.php

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
'nullable_type_declaration' => ['syntax' => 'question_mark'],
9696

9797
// Namespace notation.
98+
'native_function_invocation' => [
99+
'include' => ['@compiler_optimized'],
100+
'scope' => 'namespaced',
101+
],
98102
'no_leading_namespace_whitespace' => true,
99103

100104
// Operator.

Sources/Actions/Admin/ACP.php

+33-33
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public function execute(): void
778778

779779
// Is it valid?
780780
if (!empty($call)) {
781-
call_user_func($call);
781+
\call_user_func($call);
782782
}
783783
}
784784

@@ -814,7 +814,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
814814

815815
foreach ($config_vars as $config_var) {
816816
// HR?
817-
if (!is_array($config_var)) {
817+
if (!\is_array($config_var)) {
818818
Utils::$context['config_vars'][] = $config_var;
819819
} else {
820820
// If it has no name it doesn't have any purpose!
@@ -878,10 +878,10 @@ public static function prepareDBSettingContext(array &$config_vars): void
878878
}
879879

880880
Utils::$context['config_vars'][$config_var[1]] = [
881-
'label' => $config_var['text_label'] ?? (Lang::$txt[$config_var[1]] ?? (isset($config_var[3]) && !is_array($config_var[3]) ? $config_var[3] : '')),
881+
'label' => $config_var['text_label'] ?? (Lang::$txt[$config_var[1]] ?? (isset($config_var[3]) && !\is_array($config_var[3]) ? $config_var[3] : '')),
882882
'help' => isset(Lang::$helptxt[$config_var[1]]) ? $config_var[1] : '',
883883
'type' => $config_var[0],
884-
'size' => !empty($config_var['size']) ? $config_var['size'] : (!empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], ['int', 'float']) ? 6 : 0)),
884+
'size' => !empty($config_var['size']) ? $config_var['size'] : (!empty($config_var[2]) && !\is_array($config_var[2]) ? $config_var[2] : (\in_array($config_var[0], ['int', 'float']) ? 6 : 0)),
885885
'data' => [],
886886
'name' => $config_var[1],
887887
'value' => $value,
@@ -912,7 +912,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
912912
}
913913

914914
// If this is a select box handle any data.
915-
if (!empty($config_var[2]) && is_array($config_var[2])) {
915+
if (!empty($config_var[2]) && \is_array($config_var[2])) {
916916
// If we allow multiple selections, we need to adjust a few things.
917917
if ($config_var[0] == 'select' && !empty($config_var['multiple'])) {
918918
Utils::$context['config_vars'][$config_var[1]]['name'] .= '[]';
@@ -921,7 +921,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
921921
}
922922

923923
// If it's associative
924-
if (isset($config_var[2][0]) && is_array($config_var[2][0])) {
924+
if (isset($config_var[2][0]) && \is_array($config_var[2][0])) {
925925
Utils::$context['config_vars'][$config_var[1]]['data'] = $config_var[2];
926926
} else {
927927
foreach ($config_var[2] as $key => $item) {
@@ -930,7 +930,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
930930
}
931931

932932
if (empty($config_var['size']) && !empty($config_var['multiple'])) {
933-
Utils::$context['config_vars'][$config_var[1]]['size'] = max(4, count($config_var[2]));
933+
Utils::$context['config_vars'][$config_var[1]]['size'] = max(4, \count($config_var[2]));
934934
}
935935
}
936936

@@ -1007,7 +1007,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
10071007
$sectionTags = array_diff($bbcTags, Utils::$context['legacy_bbc']);
10081008
}
10091009

1010-
$totalTags = count($sectionTags);
1010+
$totalTags = \count($sectionTags);
10111011
$tagsPerColumn = ceil($totalTags / $numColumns);
10121012

10131013
$col = 0;
@@ -1081,15 +1081,15 @@ public static function saveSettings(array &$config_vars): void
10811081
$settings_defs = Config::getSettingsDefs();
10821082

10831083
foreach ($settings_defs as $var => $def) {
1084-
if (!is_string($var)) {
1084+
if (!\is_string($var)) {
10851085
continue;
10861086
}
10871087

10881088
if (!empty($def['is_password'])) {
10891089
$config_passwords[] = $var;
10901090
} else {
10911091
// Special handling if multiple types are allowed.
1092-
if (is_array($def['type'])) {
1092+
if (\is_array($def['type'])) {
10931093
// Obviously, we don't need null here.
10941094
$def['type'] = array_filter(
10951095
$def['type'],
@@ -1098,7 +1098,7 @@ function ($type) {
10981098
},
10991099
);
11001100

1101-
$type = count($def['type']) == 1 ? reset($def['type']) : 'multiple';
1101+
$type = \count($def['type']) == 1 ? reset($def['type']) : 'multiple';
11021102
} else {
11031103
$type = $def['type'];
11041104
}
@@ -1115,7 +1115,7 @@ function ($type) {
11151115
case 'integer':
11161116
// Some things saved as integers are presented as booleans
11171117
foreach ($config_vars as $config_var) {
1118-
if (is_array($config_var) && $config_var[0] == $var) {
1118+
if (\is_array($config_var) && $config_var[0] == $var) {
11191119
if ($config_var[3] == 'check') {
11201120
$config_bools[] = $var;
11211121
break 2;
@@ -1141,7 +1141,7 @@ function ($type) {
11411141

11421142
// Figure out which config vars we're saving here...
11431143
foreach ($config_vars as $config_var) {
1144-
if (!is_array($config_var) || $config_var[2] != 'file') {
1144+
if (!\is_array($config_var) || $config_var[2] != 'file') {
11451145
continue;
11461146
}
11471147

@@ -1164,15 +1164,15 @@ function ($type) {
11641164
}
11651165
}
11661166

1167-
if (!in_array($var_name, $config_bools) && !isset($_POST[$var_name])) {
1167+
if (!\in_array($var_name, $config_bools) && !isset($_POST[$var_name])) {
11681168
continue;
11691169
}
11701170

1171-
if (in_array($var_name, $config_passwords)) {
1171+
if (\in_array($var_name, $config_passwords)) {
11721172
if (isset($_POST[$var_name][1]) && $_POST[$var_name][0] == $_POST[$var_name][1]) {
11731173
$new_settings[$var_name] = $_POST[$var_name][0];
11741174
}
1175-
} elseif (in_array($var_name, $config_nums)) {
1175+
} elseif (\in_array($var_name, $config_nums)) {
11761176
$new_settings[$var_name] = (int) $_POST[$var_name];
11771177

11781178
// If no min is specified, assume 0. This is done to avoid having to specify 'min => 0' for all settings where 0 is the min...
@@ -1183,7 +1183,7 @@ function ($type) {
11831183
if (isset($config_var['max'])) {
11841184
$new_settings[$var_name] = min($config_var['max'], $new_settings[$var_name]);
11851185
}
1186-
} elseif (in_array($var_name, $config_bools)) {
1186+
} elseif (\in_array($var_name, $config_bools)) {
11871187
$new_settings[$var_name] = !empty($_POST[$var_name]);
11881188
} elseif (isset($config_multis[$var_name])) {
11891189
$is_acceptable_type = false;
@@ -1215,7 +1215,7 @@ function ($type) {
12151215

12161216
foreach ($config_vars as $config_var) {
12171217
// We just saved the file-based settings, so skip their definitions.
1218-
if (!is_array($config_var) || $config_var[2] == 'file') {
1218+
if (!\is_array($config_var) || $config_var[2] == 'file') {
12191219
continue;
12201220
}
12211221

@@ -1268,14 +1268,14 @@ public static function saveDBSettings(array &$config_vars): void
12681268
$setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0';
12691269
}
12701270
// Select boxes!
1271-
elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2]))) {
1271+
elseif ($var[0] == 'select' && \in_array($_POST[$var[1]], array_keys($var[2]))) {
12721272
$setArray[$var[1]] = $_POST[$var[1]];
12731273
} elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != []) {
12741274
// For security purposes we validate this line by line.
12751275
$lOptions = [];
12761276

12771277
foreach ($_POST[$var[1]] as $invar) {
1278-
if (in_array($invar, array_keys($var[2]))) {
1278+
if (\in_array($invar, array_keys($var[2]))) {
12791279
$lOptions[] = $invar;
12801280
}
12811281
}
@@ -1338,7 +1338,7 @@ public static function saveDBSettings(array &$config_vars): void
13381338
}
13391339
}
13401340
// Text!
1341-
elseif (in_array($var[0], ['text', 'large_text', 'color', 'date', 'datetime', 'datetime-local', 'email', 'month', 'time'])) {
1341+
elseif (\in_array($var[0], ['text', 'large_text', 'color', 'date', 'datetime', 'datetime-local', 'email', 'month', 'time'])) {
13421342
$setArray[$var[1]] = $_POST[$var[1]];
13431343
}
13441344
// Passwords!
@@ -1357,7 +1357,7 @@ public static function saveDBSettings(array &$config_vars): void
13571357

13581358
if (!isset($_POST[$var[1] . '_enabledTags'])) {
13591359
$_POST[$var[1] . '_enabledTags'] = [];
1360-
} elseif (!is_array($_POST[$var[1] . '_enabledTags'])) {
1360+
} elseif (!\is_array($_POST[$var[1] . '_enabledTags'])) {
13611361
$_POST[$var[1] . '_enabledTags'] = [$_POST[$var[1] . '_enabledTags']];
13621362
}
13631363

@@ -1393,13 +1393,13 @@ public static function getServerVersions(array $checkFor): array
13931393
$versions = [];
13941394

13951395
// Is GD available? If it is, we should show version information for it too.
1396-
if (in_array('gd', $checkFor) && function_exists('gd_info')) {
1396+
if (\in_array('gd', $checkFor) && \function_exists('gd_info')) {
13971397
$temp = gd_info();
13981398
$versions['gd'] = ['title' => Lang::$txt['support_versions_gd'], 'version' => $temp['GD Version']];
13991399
}
14001400

14011401
// Why not have a look at ImageMagick? If it's installed, we should show version information for it too.
1402-
if (in_array('imagemagick', $checkFor) && class_exists('Imagick')) {
1402+
if (\in_array('imagemagick', $checkFor) && class_exists('Imagick')) {
14031403
$temp = new \Imagick();
14041404
$temp2 = $temp->getVersion();
14051405
$im_version = $temp2['versionString'];
@@ -1412,7 +1412,7 @@ public static function getServerVersions(array $checkFor): array
14121412
}
14131413

14141414
// Now lets check for the Database.
1415-
if (in_array('db_server', $checkFor)) {
1415+
if (\in_array('db_server', $checkFor)) {
14161416
if (!isset(Db::$db_connection) || Db::$db_connection === false) {
14171417
Lang::load('Errors');
14181418
trigger_error(Lang::$txt['get_server_versions_no_database'], E_USER_NOTICE);
@@ -1433,23 +1433,23 @@ public static function getServerVersions(array $checkFor): array
14331433
foreach (CacheApi::detect() as $class_name => $cache_api) {
14341434
$class_name_txt_key = strtolower($cache_api->getImplementationClassKeyName());
14351435

1436-
if (in_array($class_name_txt_key, $checkFor)) {
1436+
if (\in_array($class_name_txt_key, $checkFor)) {
14371437
$versions[$class_name_txt_key] = [
14381438
'title' => Lang::$txt[$class_name_txt_key . '_cache'] ?? $class_name,
14391439
'version' => $cache_api->getVersion(),
14401440
];
14411441
}
14421442
}
14431443

1444-
if (in_array('php', $checkFor)) {
1444+
if (\in_array('php', $checkFor)) {
14451445
$versions['php'] = [
14461446
'title' => 'PHP',
14471447
'version' => PHP_VERSION,
14481448
'more' => '?action=admin;area=serversettings;sa=phpinfo',
14491449
];
14501450
}
14511451

1452-
if (in_array('server', $checkFor)) {
1452+
if (\in_array('server', $checkFor)) {
14531453
$versions['server'] = [
14541454
'title' => Lang::$txt['support_versions_server'],
14551455
'version' => $_SERVER['SERVER_SOFTWARE'],
@@ -1746,7 +1746,7 @@ public static function emailAdmins(string $template, array $replacements = [], a
17461746
// Any additional users we must email this to?
17471747
if (!empty($additional_recipients)) {
17481748
foreach ($additional_recipients as $recipient) {
1749-
if (in_array($recipient['email'], $emails_sent)) {
1749+
if (\in_array($recipient['email'], $emails_sent)) {
17501750
continue;
17511751
}
17521752

@@ -1779,7 +1779,7 @@ public static function adminLogin(string $type = 'admin'): void
17791779
// Validate what type of session check this is.
17801780
$types = [];
17811781
IntegrationHook::call('integrate_validateSession', [&$types]);
1782-
$type = in_array($type, $types) || $type == 'moderate' ? $type : 'admin';
1782+
$type = \in_array($type, $types) || $type == 'moderate' ? $type : 'admin';
17831783

17841784
// They used a wrong password, log it and unset that.
17851785
if (isset($_POST[$type . '_hash_pass']) || isset($_POST[$type . '_pass'])) {
@@ -1878,11 +1878,11 @@ protected function setAdminAreas(): void
18781878
array_walk_recursive(
18791879
$this->admin_areas,
18801880
function (&$value, $key) {
1881-
if (in_array($key, ['title', 'label'])) {
1881+
if (\in_array($key, ['title', 'label'])) {
18821882
$value = Lang::$txt[$value] ?? $value;
18831883
}
18841884

1885-
if (is_string($value)) {
1885+
if (\is_string($value)) {
18861886
$value = strtr($value, [
18871887
'{scripturl}' => Config::$scripturl,
18881888
'{boardurl}' => Config::$boardurl,
@@ -1943,7 +1943,7 @@ function (&$value, $key) {
19431943
*/
19441944
protected static function adminLogin_outputPostVars(string $k, string|array $v): string
19451945
{
1946-
if (!is_array($v)) {
1946+
if (!\is_array($v)) {
19471947
return "\n" . '<input type="hidden" name="' . Utils::htmlspecialchars($k) . '" value="' . strtr($v, ['"' => '&quot;', '<' => '&lt;', '>' => '&gt;']) . '">';
19481948
}
19491949

Sources/Actions/Admin/AntiSpam.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function execute(): void
153153

154154
foreach (Utils::$context['qa_languages'] as $lang_id => $dummy) {
155155
// If we had some questions for this language before, but don't now, delete everything from that language.
156-
if ((!isset($_POST['question'][$lang_id]) || !is_array($_POST['question'][$lang_id])) && !empty(Utils::$context['qa_by_lang'][$lang_id])) {
156+
if ((!isset($_POST['question'][$lang_id]) || !\is_array($_POST['question'][$lang_id])) && !empty(Utils::$context['qa_by_lang'][$lang_id])) {
157157
$changes['delete'] = array_merge($changes['delete'], Utils::$context['qa_by_lang'][$lang_id]);
158158
}
159159

@@ -188,7 +188,7 @@ public function execute(): void
188188
$question = Utils::htmlspecialchars(trim($question));
189189

190190
// Get the answers. Firstly check there actually might be some.
191-
if (!isset($_POST['answer'][$lang_id][$q_id]) || !is_array($_POST['answer'][$lang_id][$q_id])) {
191+
if (!isset($_POST['answer'][$lang_id][$q_id]) || !\is_array($_POST['answer'][$lang_id][$q_id])) {
192192
if (isset(Utils::$context['question_answers'][$q_id])) {
193193
$changes['delete'][] = $q_id;
194194
}
@@ -361,7 +361,7 @@ public static function getConfigVars(): array
361361
Lang::load('ManageSettings');
362362

363363
// Generate a sample registration image.
364-
Utils::$context['use_graphic_library'] = in_array('gd', get_loaded_extensions());
364+
Utils::$context['use_graphic_library'] = \in_array('gd', get_loaded_extensions());
365365

366366
$config_vars = [
367367
['check', 'reg_verification'],

0 commit comments

Comments
 (0)