Skip to content

Commit

Permalink
Apply native_function_invocation in PHPCS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
live627 committed Jul 23, 2024
1 parent 9aa2a6f commit c54551f
Show file tree
Hide file tree
Showing 207 changed files with 2,240 additions and 2,236 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no ${{ env.PHPCS_EXTRA_ARGS }}
args: --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no --allow-risky=yes ${{ env.PHPCS_EXTRA_ARGS }}
4 changes: 4 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
'nullable_type_declaration' => ['syntax' => 'question_mark'],

// Namespace notation.
'native_function_invocation' => [
'include' => ['@compiler_optimized'],
'scope' => 'namespaced',
],
'no_leading_namespace_whitespace' => true,

// Operator.
Expand Down
66 changes: 33 additions & 33 deletions Sources/Actions/Admin/ACP.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function execute(): void

// Is it valid?
if (!empty($call)) {
call_user_func($call);
\call_user_func($call);
}
}

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

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

Utils::$context['config_vars'][$config_var[1]] = [
'label' => $config_var['text_label'] ?? (Lang::$txt[$config_var[1]] ?? (isset($config_var[3]) && !is_array($config_var[3]) ? $config_var[3] : '')),
'label' => $config_var['text_label'] ?? (Lang::$txt[$config_var[1]] ?? (isset($config_var[3]) && !\is_array($config_var[3]) ? $config_var[3] : '')),
'help' => isset(Lang::$helptxt[$config_var[1]]) ? $config_var[1] : '',
'type' => $config_var[0],
'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)),
'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)),
'data' => [],
'name' => $config_var[1],
'value' => $value,
Expand Down Expand Up @@ -912,7 +912,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
}

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

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

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

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

$totalTags = count($sectionTags);
$totalTags = \count($sectionTags);
$tagsPerColumn = ceil($totalTags / $numColumns);

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

foreach ($settings_defs as $var => $def) {
if (!is_string($var)) {
if (!\is_string($var)) {
continue;
}

if (!empty($def['is_password'])) {
$config_passwords[] = $var;
} else {
// Special handling if multiple types are allowed.
if (is_array($def['type'])) {
if (\is_array($def['type'])) {
// Obviously, we don't need null here.
$def['type'] = array_filter(
$def['type'],
Expand All @@ -1098,7 +1098,7 @@ function ($type) {
},
);

$type = count($def['type']) == 1 ? reset($def['type']) : 'multiple';
$type = \count($def['type']) == 1 ? reset($def['type']) : 'multiple';
} else {
$type = $def['type'];
}
Expand All @@ -1115,7 +1115,7 @@ function ($type) {
case 'integer':
// Some things saved as integers are presented as booleans
foreach ($config_vars as $config_var) {
if (is_array($config_var) && $config_var[0] == $var) {
if (\is_array($config_var) && $config_var[0] == $var) {
if ($config_var[3] == 'check') {
$config_bools[] = $var;
break 2;
Expand All @@ -1141,7 +1141,7 @@ function ($type) {

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

Expand All @@ -1164,15 +1164,15 @@ function ($type) {
}
}

if (!in_array($var_name, $config_bools) && !isset($_POST[$var_name])) {
if (!\in_array($var_name, $config_bools) && !isset($_POST[$var_name])) {
continue;
}

if (in_array($var_name, $config_passwords)) {
if (\in_array($var_name, $config_passwords)) {
if (isset($_POST[$var_name][1]) && $_POST[$var_name][0] == $_POST[$var_name][1]) {
$new_settings[$var_name] = $_POST[$var_name][0];
}
} elseif (in_array($var_name, $config_nums)) {
} elseif (\in_array($var_name, $config_nums)) {
$new_settings[$var_name] = (int) $_POST[$var_name];

// 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...
Expand All @@ -1183,7 +1183,7 @@ function ($type) {
if (isset($config_var['max'])) {
$new_settings[$var_name] = min($config_var['max'], $new_settings[$var_name]);
}
} elseif (in_array($var_name, $config_bools)) {
} elseif (\in_array($var_name, $config_bools)) {
$new_settings[$var_name] = !empty($_POST[$var_name]);
} elseif (isset($config_multis[$var_name])) {
$is_acceptable_type = false;
Expand Down Expand Up @@ -1215,7 +1215,7 @@ function ($type) {

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

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

foreach ($_POST[$var[1]] as $invar) {
if (in_array($invar, array_keys($var[2]))) {
if (\in_array($invar, array_keys($var[2]))) {
$lOptions[] = $invar;
}
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@ public static function saveDBSettings(array &$config_vars): void
}
}
// Text!
elseif (in_array($var[0], ['text', 'large_text', 'color', 'date', 'datetime', 'datetime-local', 'email', 'month', 'time'])) {
elseif (\in_array($var[0], ['text', 'large_text', 'color', 'date', 'datetime', 'datetime-local', 'email', 'month', 'time'])) {
$setArray[$var[1]] = $_POST[$var[1]];
}
// Passwords!
Expand All @@ -1357,7 +1357,7 @@ public static function saveDBSettings(array &$config_vars): void

if (!isset($_POST[$var[1] . '_enabledTags'])) {
$_POST[$var[1] . '_enabledTags'] = [];
} elseif (!is_array($_POST[$var[1] . '_enabledTags'])) {
} elseif (!\is_array($_POST[$var[1] . '_enabledTags'])) {
$_POST[$var[1] . '_enabledTags'] = [$_POST[$var[1] . '_enabledTags']];
}

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

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

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

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

if (in_array($class_name_txt_key, $checkFor)) {
if (\in_array($class_name_txt_key, $checkFor)) {
$versions[$class_name_txt_key] = [
'title' => Lang::$txt[$class_name_txt_key . '_cache'] ?? $class_name,
'version' => $cache_api->getVersion(),
];
}
}

if (in_array('php', $checkFor)) {
if (\in_array('php', $checkFor)) {
$versions['php'] = [
'title' => 'PHP',
'version' => PHP_VERSION,
'more' => '?action=admin;area=serversettings;sa=phpinfo',
];
}

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

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

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

if (is_string($value)) {
if (\is_string($value)) {
$value = strtr($value, [
'{scripturl}' => Config::$scripturl,
'{boardurl}' => Config::$boardurl,
Expand Down Expand Up @@ -1943,7 +1943,7 @@ function (&$value, $key) {
*/
protected static function adminLogin_outputPostVars(string $k, string|array $v): string
{
if (!is_array($v)) {
if (!\is_array($v)) {
return "\n" . '<input type="hidden" name="' . Utils::htmlspecialchars($k) . '" value="' . strtr($v, ['"' => '&quot;', '<' => '&lt;', '>' => '&gt;']) . '">';
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Actions/Admin/AntiSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function execute(): void

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

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

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

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

$config_vars = [
['check', 'reg_verification'],
Expand Down
Loading

0 comments on commit c54551f

Please sign in to comment.