Skip to content

Commit

Permalink
Add strict types to index and other files
Browse files Browse the repository at this point in the history
  • Loading branch information
jdarwood007 committed Jan 20, 2024
1 parent e64a70d commit 8c5df79
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 70 deletions.
2 changes: 2 additions & 0 deletions SSI.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

if (!defined('SMF')) {
define('SMF', 'SSI');
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ class Board implements \ArrayAccess
* rather than Board::$board_id. The only exception to this rule is in code
* executed before Board::load() has been called.
*/
public static $board_id;
public static ?int $board_id;

/**
* @var self
*
* Instance of this class for board we are currently in.
*/
public static $info;
public static ?self $info;

/**
* @var array
Expand Down
2 changes: 2 additions & 0 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

// Don't do anything if SMF is already loaded.
if (defined('SMF')) {
return true;
Expand Down
2 changes: 2 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

/********************************************************
* Initialize things that are common to all entry points.
* (i.e. index.php, SSI.php, cron.php, proxy.php)
Expand Down
8 changes: 6 additions & 2 deletions other/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -1906,9 +1906,13 @@ function fixModSecurity()
}

return true;
} elseif (file_exists(Config::$boarddir . '/.htaccess')) {
}

if (file_exists(Config::$boarddir . '/.htaccess')) {
return strpos(implode('', file(Config::$boarddir . '/.htaccess')), '<IfModule mod_security.c>') !== false;
} elseif (is_writable(Config::$boarddir)) {
}

if (is_writable(Config::$boarddir)) {
if ($ht_handle = fopen(Config::$boarddir . '/.htaccess', 'w')) {
fwrite($ht_handle, $htaccess_addition);
fclose($ht_handle);
Expand Down
16 changes: 9 additions & 7 deletions other/update_unicode_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

// 1. Set a couple of variables that we'll need.
$boarddir = realpath(dirname(__DIR__));
$sourcedir = $boarddir . '/Sources';
Expand All @@ -41,23 +43,23 @@
// 3. Borrow a bit of stuff from index.php.
$index_php_start = file_get_contents($boarddir . '/index.php', false, null, 0, 4096);

foreach (array('SMF_VERSION', 'SMF_SOFTWARE_YEAR') as $const)
{
preg_match("/define\('$const', '([^)]+)'\);/", $index_php_start, $matches);
foreach (['SMF_VERSION', 'SMF_SOFTWARE_YEAR'] as $const) {
preg_match("/define\('{$const}', '([^)]+)'\);/", $index_php_start, $matches);

if (empty($matches[1]))
die("Could not find value for $const in index.php");
if (empty($matches[1])) {
die("Could not find value for {$const} in index.php");
}

define($const, $matches[1]);
}

// 4. Get some more stuff we need.
require_once($sourcedir . '/Autoloader.php');
require_once $sourcedir . '/Autoloader.php';
SMF\Config::$boarddir = $boarddir;
SMF\Config::$sourcedir = $sourcedir;

// 5. Do the job.
$unicode_updater = new SMF\Tasks\UpdateUnicode(array());
$unicode_updater = new SMF\Tasks\UpdateUnicode([]);
$unicode_updater->execute();

?>
92 changes: 46 additions & 46 deletions other/update_version_numbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

// Obviously, set this to the name of the release branch in question
$release_branch = 'release-3.0';

Expand All @@ -41,14 +43,17 @@
chdir($basedir);

// Do nothing if on wrong branch or working tree is dirty
if (trim((string) shell_exec('git status --porcelain')) !== '')
if (trim((string) shell_exec('git status --porcelain')) !== '') {
die('Could not continue. Dirty working tree.');
}

if (trim(shell_exec('git rev-parse --abbrev-ref HEAD')) !== $release_branch)
if (trim(shell_exec('git rev-parse --abbrev-ref HEAD')) !== $release_branch) {
shell_exec('git checkout "' . $release_branch . '"');
}

if (trim(shell_exec('git rev-parse --abbrev-ref HEAD')) !== $release_branch)
if (trim(shell_exec('git rev-parse --abbrev-ref HEAD')) !== $release_branch) {
die('Could not continue. Wrong branch is checked out.');
}

// Matches all standard SMF version strings
$version_regex = '\d+\.\d+[. ]?(?:(?:(?<= )(?>RC|Beta |Alpha ))?\d+)?';
Expand All @@ -58,23 +63,21 @@
$prev_version = ltrim(trim(shell_exec('git describe --tags --abbrev=0')), 'v');

// Was the new version passed as a command line argument?
if (!empty($argv[1]))
{
if (!empty($argv[1])) {
$new_version = trim($argv[1]);

if (!preg_match('~^' . $version_regex . '$~', $new_version))
if (!preg_match('~^' . $version_regex . '$~', $new_version)) {
die('Provided version string is invalid: ' . $new_version);
}
}
// Normal case: just increment the patch number
else
{
else {
$new_version = array_pad(explode('.', $prev_version), 3, 0);

$new_version[2]++;

if (!is_numeric($new_version[1]))
{
$new_version[1] = str_replace(array('-', 'ALPHA', 'BETA'), array(' ', 'Alpha', 'Beta'), strtoupper($new_version[1]));
if (!is_numeric($new_version[1])) {
$new_version[1] = str_replace(['-', 'ALPHA', 'BETA'], [' ', 'Alpha', 'Beta'], strtoupper($new_version[1]));

$new_version[1] .= (strpos($new_version[1], 'RC') !== false ? '' : ' ') . $new_version[2];

Expand All @@ -87,26 +90,27 @@
$year = date_format(date_create(), 'Y');

// These need to be updated for every new version, even if they have not otherwise changed
$always_update = array('index.php', 'cron.php', 'proxy.php', 'SSI.php', 'other/install.php', 'other/upgrade.php', 'other/upgrade-helper.php', 'other/Settings.php', 'other/Settings_bak.php');
$always_update = ['index.php', 'cron.php', 'proxy.php', 'SSI.php', 'other/install.php', 'other/upgrade.php', 'other/upgrade-helper.php', 'other/Settings.php', 'other/Settings_bak.php'];

// Checkout a new branch to work in.
$new_branch = 'update_version_numbers_to_' . preg_replace('/\s+/', '-', strtolower($new_version));

shell_exec('git checkout -b "' . $new_branch . '"');

if (trim(shell_exec('git rev-parse --abbrev-ref HEAD')) !== $new_branch)
if (trim(shell_exec('git rev-parse --abbrev-ref HEAD')) !== $new_branch) {
die('Failed to create branch "' . $new_branch . '"');
}

// Update SMF_VERSION and SMF_SOFTWARE_YEAR
foreach ($always_update as $file)
{
$content = $original_content = file_get_contents("$basedir/$file");
foreach ($always_update as $file) {
$content = $original_content = file_get_contents("{$basedir}/{$file}");

$content = preg_replace("~define\('SMF_VERSION', '" . $version_regex . "'\);~", "define('SMF_VERSION', '$new_version');", $content);
$content = preg_replace("~define\('SMF_SOFTWARE_YEAR', '\d{4}'\);~", "define('SMF_SOFTWARE_YEAR', '$year');", $content);
$content = preg_replace("~define\('SMF_VERSION', '" . $version_regex . "'\);~", "define('SMF_VERSION', '{$new_version}');", $content);
$content = preg_replace("~define\('SMF_SOFTWARE_YEAR', '\d{4}'\);~", "define('SMF_SOFTWARE_YEAR', '{$year}');", $content);

if ($content !== $original_content)
file_put_contents("$basedir/$file", $content);
if ($content !== $original_content) {
file_put_contents("{$basedir}/{$file}", $content);
}
}

// Update license blocks
Expand All @@ -121,50 +125,46 @@

$files = array_unique(array_merge($always_update, array_filter(
explode("\n", shell_exec('git diff --name-only v' . $prev_version . '...HEAD')),
function ($filename)
{
function ($filename) {
return file_exists($filename) && strpos(mime_content_type($filename), 'text/') === 0;
}
},
)));

foreach ($files as $file)
{
$content = $original_content = file_get_contents("$basedir/$file");
foreach ($files as $file) {
$content = $original_content = file_get_contents("{$basedir}/{$file}");

if (preg_match($license_pattern, $content))
{
if (preg_match($license_pattern, $content)) {
$content = preg_replace($license_pattern, '${1}' . $year . '${2}' . $new_version, $content);
}
elseif (preg_match($lang_pattern, $content))
{
} elseif (preg_match($lang_pattern, $content)) {
$content = preg_replace($lang_pattern, $new_version, $content);
}
else
{
} else {
continue;
}

if ($content !== $original_content)
file_put_contents("$basedir/$file", $content);
if ($content !== $original_content) {
file_put_contents("{$basedir}/{$file}", $content);
}
}

// Update SMF_LANG_VERSION
preg_match($lang_pattern, file_get_contents("$basedir/Themes/default/languages/index.english.php"), $matches);
preg_match($lang_pattern, file_get_contents("{$basedir}/Themes/default/languages/index.english.php"), $matches);

$lang_version = $matches[0];

$content = $original_content = file_get_contents("$basedir/other/upgrade.php");
$content = preg_replace("~define\('SMF_LANG_VERSION', '" . $version_regex . "'\);~", "define('SMF_LANG_VERSION', '$lang_version');", $content);
$content = $original_content = file_get_contents("{$basedir}/other/upgrade.php");
$content = preg_replace("~define\('SMF_LANG_VERSION', '" . $version_regex . "'\);~", "define('SMF_LANG_VERSION', '{$lang_version}');", $content);

if ($content !== $original_content)
file_put_contents("$basedir/other/upgrade.php", $content);
if ($content !== $original_content) {
file_put_contents("{$basedir}/other/upgrade.php", $content);
}

// Update LICENCE file
$content = $original_content = file_get_contents("$basedir/LICENSE");
$content = preg_replace("~Copyright © \d+ Simple Machines.~", "Copyright © $year Simple Machines.", $content);
$content = preg_replace("~http://www.simplemachines.org\b~", "https://www.simplemachines.org", $content);
$content = $original_content = file_get_contents("{$basedir}/LICENSE");
$content = preg_replace("~Copyright © \d+ Simple Machines.~", "Copyright © {$year} Simple Machines.", $content);
$content = preg_replace("~http://www.simplemachines.org\b~", 'https://www.simplemachines.org', $content);

if ($content !== $original_content)
file_put_contents("$basedir/LICENSE", $content);
if ($content !== $original_content) {
file_put_contents("{$basedir}/LICENSE", $content);
}

?>
2 changes: 1 addition & 1 deletion other/upgrade-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,4 @@ function upgradeCacheSettings()
$current_cache = !empty($GLOBALS['cache_accelerator']) ? $GLOBALS['cache_accelerator'] : 'smf';

return $cache_options[$current_cache];
}
}
21 changes: 10 additions & 11 deletions other/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ function ConvertUtf8()
];

// Default to ISO-8859-1 unless we detected another supported charset
$upcontext['charset_detected'] = (isset($lang_charsets[Config::$language], $charsets[strtr(strtolower($upcontext['charset_detected']),['utf' => 'UTF', 'iso' => 'ISO'])])) ? $lang_charsets[Config::$language] : 'ISO-8859-1';
$upcontext['charset_detected'] = (isset($lang_charsets[Config::$language], $charsets[strtr(strtolower($upcontext['charset_detected']), ['utf' => 'UTF', 'iso' => 'ISO'])])) ? $lang_charsets[Config::$language] : 'ISO-8859-1';

$upcontext['charset_list'] = array_keys($charsets);

Expand Down Expand Up @@ -4098,7 +4098,7 @@ function updateStepProgress(current, max, overall_weight)
<h2>', Lang::$txt['upgrade_progress'], '</h2>
<ul class="steps_list">';

foreach ($upcontext['steps'] as $num => $step) {
foreach ((array) $upcontext['steps'] as $num => $step) {
echo '
<li', $num == $upcontext['current_step'] ? ' class="stepcurrent"' : '', '>
', Lang::$txt['upgrade_step'], ' ', $step[0], ': ', Lang::$txt[$step[1]], '
Expand Down Expand Up @@ -4234,7 +4234,7 @@ function template_xml_above()
<smf>';

if (!empty($upcontext['get_data'])) {
foreach ($upcontext['get_data'] as $k => $v) {
foreach ((array) $upcontext['get_data'] as $k => $v) {
echo '
<get key="', $k, '">', $v, '</get>';
}
Expand Down Expand Up @@ -4529,7 +4529,7 @@ function template_backup_database()

// Dont any tables so far?
if (!empty($upcontext['previous_tables'])) {
foreach ($upcontext['previous_tables'] as $table) {
foreach ((array) $upcontext['previous_tables'] as $table) {
echo '
<br>', Lang::$txt['upgrade_completed_table'], ' &quot;', $table, '&quot;.';
}
Expand Down Expand Up @@ -4624,7 +4624,7 @@ function template_database_changes()

// No javascript looks rubbish!
if (!$support_js) {
foreach ($upcontext['actioned_items'] as $num => $item) {
foreach ((array) $upcontext['actioned_items'] as $num => $item) {
if ($num != 0) {
echo ' Successful!';
}
Expand Down Expand Up @@ -5024,7 +5024,7 @@ function template_convert_utf8()

// Done any tables so far?
if (!empty($upcontext['previous_tables'])) {
foreach ($upcontext['previous_tables'] as $table) {
foreach ((array) $upcontext['previous_tables'] as $table) {
echo '
<br>', Lang::$txt['upgrade_completed_table'], ' &quot;', $table, '&quot;.';
}
Expand Down Expand Up @@ -5128,7 +5128,7 @@ function template_serialize_json()

// Dont any tables so far?
if (!empty($upcontext['previous_tables'])) {
foreach ($upcontext['previous_tables'] as $table) {
foreach ((array) $upcontext['previous_tables'] as $table) {
echo '
<br>', Lang::$txt['upgrade_completed_table'], ' &quot;', $table, '&quot;.';
}
Expand Down Expand Up @@ -5270,9 +5270,8 @@ function doTheDelete(theCheck)
* @param string $newCol The new column to put data in
* @param int $limit The amount of entries to handle at once.
* @param int $setSize The amount of entries after which to update the database.
* @return bool
*/
function MySQLConvertOldIp($targetTable, $oldCol, $newCol, $limit = 50000, $setSize = 100)
function MySQLConvertOldIp($targetTable, $oldCol, $newCol, $limit = 50000, $setSize = 100): void
{
global $step_progress;

Expand Down Expand Up @@ -5410,15 +5409,15 @@ function MySQLConvertOldIp($targetTable, $oldCol, $newCol, $limit = 50000, $setS
*
* @return array Info on the table.
*/
function upgradeGetColumnInfo($targetTable, $column)
function upgradeGetColumnInfo($targetTable, $column): array
{
$columns = Db::$db->list_columns($targetTable, true);

if (isset($columns[$column])) {
return $columns[$column];
}


return [];
}

?>
2 changes: 2 additions & 0 deletions proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

if (!defined('SMF')) {
define('SMF', 'PROXY');
}
Expand Down
4 changes: 3 additions & 1 deletion subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

use SMF\Actions\Admin\ACP;
use SMF\Actions\Admin\Subscriptions;
use SMF\Config;
Expand Down Expand Up @@ -163,7 +165,7 @@
$subscription_act = time();
$status = 0;
} else {
Subscriptions::getAll();
Subscriptions::getSubs();
$subscription_act = $subscription_info['end_time'] - Subscriptions::$all[$subscription_id]['num_length'];
$status = 1;
}
Expand Down

0 comments on commit 8c5df79

Please sign in to comment.