Skip to content

Commit 49d5aac

Browse files
committed
Change the UserPreferences to an instance class
1 parent 0fbf599 commit 49d5aac

File tree

8 files changed

+64
-61
lines changed

8 files changed

+64
-61
lines changed

error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@
725725
// ============================================================================
726726
// If no match was found till this point, the last action is to start a
727727
// search with the URI the user typed in
728-
$fallback = (UserPreferences::$searchType === UserPreferences::URL_MANUAL ? "404manual" : "404quickref");
728+
$fallback = ($userPreferences->searchType === UserPreferences::URL_MANUAL ? "404manual" : "404quickref");
729729
mirror_redirect(
730730
'/search.php?show=' . $fallback . '&lang=' . urlencode($LANG) .
731731
'&pattern=' . substr($_SERVER['REQUEST_URI'], 1),

include/langchooser.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030

3131
use phpweb\LangChooser;
32-
use phpweb\UserPreferences;
3332

3433
require_once __DIR__ . '/../src/autoload.php';
3534

@@ -38,7 +37,7 @@ $_SERVER['STRIPPED_URI'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES,
3837

3938
// The code is encapsulated in a function,
4039
// so the variable namespace is not polluted
41-
list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, UserPreferences::$languageCode, default_language() ?: ''))->chooseCode(
40+
list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, $userPreferences->languageCode, default_language() ?: ''))->chooseCode(
4241
$_REQUEST['lang'] ?? null,
4342
$_SERVER['REQUEST_URI'],
4443
$_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? null,

include/layout.inc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use phpweb\Navigation\NavItem;
4-
use phpweb\UserPreferences;
54

65
$_SERVER['STATIC_ROOT'] = $MYSITE;
76
$_SERVER['MYSITE'] = $MYSITE;
@@ -426,13 +425,13 @@ EOT;
426425

427426
function site_header(string $title = 'Hypertext Preprocessor', array $config = []): void
428427
{
429-
global $MYSITE;
428+
global $MYSITE, $LANG;
430429

431430
$meta_image_path = $MYSITE . 'images/meta-image.png';
432431
$meta_description = "PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.";
433432

434433
$defaults = [
435-
"lang" => UserPreferences::$languageCode,
434+
"lang" => $LANG,
436435
"current" => "",
437436
"meta-navigation" => [],
438437
'classes' => '',

include/prepend.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ unset($COUNTRY);
7575
unset($ONLOAD);
7676
unset($LAST_UPDATED);
7777

78+
$userPreferences = new UserPreferences();
79+
7880
// Load the My PHP.net settings before any includes
79-
UserPreferences::load();
81+
$userPreferences->load();
8082

8183
// Site details (mirror site information)
8284
include __DIR__ . '/site.inc';

my.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
if (isset($_POST['my_lang'], $langs[$_POST['my_lang']])) {
1717

1818
// Set the language preference
19-
UserPreferences::$languageCode = $_POST['my_lang'];
19+
$userPreferences->languageCode = $_POST['my_lang'];
2020

2121
// Add this as first option, selected
2222
$options[] = '<option value="' . $_POST['my_lang'] . '" selected>' .
@@ -27,14 +27,14 @@
2727
}
2828

2929
// We have received a cookie and it is an available language
30-
elseif (isset($langs[UserPreferences::$languageCode])) {
30+
elseif (isset($langs[$userPreferences->languageCode])) {
3131

3232
// Add this as first option, selected
33-
$options[] = '<option value="' . UserPreferences::$languageCode . '" selected>' .
34-
$langs[UserPreferences::$languageCode] . "</option>\n";
33+
$options[] = '<option value="' . $userPreferences->languageCode . '" selected>' .
34+
$langs[$userPreferences->languageCode] . "</option>\n";
3535

3636
// Remove, so it is not listed two times
37-
unset($langs[UserPreferences::$languageCode]);
37+
unset($langs[$userPreferences->languageCode]);
3838
}
3939

4040
// We have no cookie and no form submitted
@@ -54,18 +54,18 @@
5454

5555
// Save URL shortcut fallback setting
5656
if (isset($_POST['urlsearch'])) {
57-
UserPreferences::setUrlSearchType($_POST['urlsearch']);
57+
$userPreferences->setUrlSearchType($_POST['urlsearch']);
5858
}
5959

6060
if (isset($_POST["showug"])) {
61-
UserPreferences::setIsUserGroupTipsEnabled($_POST["showug"] === "enable");
61+
$userPreferences->setIsUserGroupTipsEnabled($_POST["showug"] === "enable");
6262
}
6363

6464
// Prepare mirror array
6565
$mirror_sites = $MIRRORS;
6666
$mirror_sites["NONE"] = [7 => MIRROR_OK];
6767

68-
UserPreferences::save();
68+
$userPreferences->save();
6969

7070
site_header("My PHP.net", ["current" => "community"]);
7171
?>
@@ -177,7 +177,7 @@
177177
<div class="indent">
178178
Your setting: <input id="form-urlsearch-quickref" type="radio" name="urlsearch" value="quickref"
179179
<?php
180-
$type = UserPreferences::$searchType;
180+
$type = $userPreferences->searchType;
181181
if ($type === UserPreferences::URL_NONE || $type === UserPreferences::URL_FUNC) {
182182
echo ' checked="checked"';
183183
}
@@ -196,8 +196,8 @@
196196
We are experimenting with listing nearby user groups. This feature is highly experimental
197197
and will very likely change a lot and be broken at times.
198198
</p>
199-
<label for="showugenable">Enable UG tips</label> <input type="radio" name="showug" id="showugenable" value="enable" <?php echo UserPreferences::$isUserGroupTipsEnabled ? "checked=checked" : "" ?>><br>
200-
<label for="showugdisable">Disable UG tips</label> <input type="radio" name="showug" id="showugdisable" value="disable" <?php echo UserPreferences::$isUserGroupTipsEnabled ? "" : "checked=checked" ?>>
199+
<label for="showugenable">Enable UG tips</label> <input type="radio" name="showug" id="showugenable" value="enable" <?php echo $userPreferences->isUserGroupTipsEnabled ? "checked=checked" : "" ?>><br>
200+
<label for="showugdisable">Disable UG tips</label> <input type="radio" name="showug" id="showugdisable" value="disable" <?php echo $userPreferences->isUserGroupTipsEnabled ? "" : "checked=checked" ?>>
201201

202202
<p class="center">
203203
<input type="submit" value="Set All Preferences">

src/UserPreferences.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace phpweb;
46

7+
use function explode;
8+
use function in_array;
9+
use function is_string;
10+
use function join;
11+
use function mirror_setcookie;
12+
513
/**
614
* Handles the "My PHP.net" preferences.
715
*/
@@ -13,22 +21,19 @@ final class UserPreferences
1321

1422
public const URL_MANUAL = 'manual';
1523

16-
public static string $languageCode = '';
17-
18-
/**
19-
* URL search fallback
20-
*
21-
* @var 'manual'|'quickref'|false
22-
*/
23-
public static string|false $searchType = self::URL_NONE;
24-
25-
public static bool $isUserGroupTipsEnabled = false;
24+
/** @param self::URL_* $searchType URL search fallback */
25+
public function __construct(
26+
public string $languageCode = '',
27+
public string|false $searchType = self::URL_NONE,
28+
public bool $isUserGroupTipsEnabled = false,
29+
) {
30+
}
2631

27-
public static function load(): void
32+
public function load(): void
2833
{
29-
self::$languageCode = '';
30-
self::$searchType = self::URL_NONE;
31-
self::$isUserGroupTipsEnabled = false;
34+
$this->languageCode = '';
35+
$this->searchType = self::URL_NONE;
36+
$this->isUserGroupTipsEnabled = false;
3237

3338
if (!isset($_COOKIE['MYPHPNET']) || !is_string($_COOKIE['MYPHPNET']) || $_COOKIE['MYPHPNET'] === '') {
3439
return;
@@ -42,30 +47,30 @@ public static function load(): void
4247
* 4 - Documentation developmental server (removed)
4348
*/
4449
$preferences = explode(",", $_COOKIE['MYPHPNET']);
45-
self::$languageCode = $preferences[0] ?? '';
46-
self::setUrlSearchType($preferences[1] ?? self::URL_NONE);
47-
self::$isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3];
50+
$this->languageCode = $preferences[0] ?? '';
51+
$this->setUrlSearchType($preferences[1] ?? self::URL_NONE);
52+
$this->isUserGroupTipsEnabled = isset($preferences[3]) && $preferences[3];
4853
}
4954

50-
public static function setUrlSearchType(mixed $type): void
55+
public function setUrlSearchType(mixed $type): void
5156
{
5257
if (!in_array($type, [self::URL_FUNC, self::URL_MANUAL, self::URL_NONE], true)) {
5358
return;
5459
}
5560

56-
self::$searchType = $type;
61+
$this->searchType = $type;
5762
}
5863

59-
public static function setIsUserGroupTipsEnabled(bool $enable): void {
64+
public function setIsUserGroupTipsEnabled(bool $enable): void {
6065
// Show the ug tips to lucky few, depending on time.
6166
if ($_SERVER["REQUEST_TIME"] % 10) {
6267
$enable = true;
6368
}
6469

65-
self::$isUserGroupTipsEnabled = $enable;
70+
$this->isUserGroupTipsEnabled = $enable;
6671
}
6772

68-
public static function save(): void
73+
public function save(): void
6974
{
7075
/**
7176
* 0 - Language code
@@ -74,7 +79,7 @@ public static function save(): void
7479
* 3 - User Group tips
7580
* 4 - Documentation developmental server (removed)
7681
*/
77-
$preferences = [self::$languageCode, self::$searchType, '', self::$isUserGroupTipsEnabled];
82+
$preferences = [$this->languageCode, $this->searchType, '', $this->isUserGroupTipsEnabled];
7883

7984
// Set all the preferred values for a year
8085
mirror_setcookie("MYPHPNET", join(",", $preferences), 60 * 60 * 24 * 365);

tests/Unit/LangChooserTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use phpweb\LangChooser;
88
use PHPUnit\Framework;
9-
use phpweb\UserPreferences;
109

1110
#[Framework\Attributes\CoversClass(LangChooser::class)]
1211
class LangChooserTest extends Framework\TestCase
@@ -107,9 +106,7 @@ public function testChooseCodeWithLangParameterAndManualPath(): void
107106

108107
public function testChooseCodeWithManualPathAndUserPreference(): void
109108
{
110-
UserPreferences::$languageCode = 'en';
111-
112-
$langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], '', 'en');
109+
$langChooser = new LangChooser(self::DEFAULT_LANGUAGE_LIST, [], 'en', 'en');
113110
$result = $langChooser->chooseCode('', '/manual/de', null);
114111

115112
self::assertSame(['de', 'de'], $result);

tests/Unit/UserPreferencesTest.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ public function testLoad(
2222
): void {
2323
$_COOKIE = $cookie;
2424

25-
UserPreferences::load();
25+
$userPreferences = new UserPreferences();
26+
$userPreferences->load();
2627

27-
self::assertSame($languageCode, UserPreferences::$languageCode);
28-
self::assertSame($searchType, UserPreferences::$searchType);
29-
self::assertSame($isUserGroupTipsEnabled, UserPreferences::$isUserGroupTipsEnabled);
28+
self::assertSame($languageCode, $userPreferences->languageCode);
29+
self::assertSame($searchType, $userPreferences->searchType);
30+
self::assertSame($isUserGroupTipsEnabled, $userPreferences->isUserGroupTipsEnabled);
3031
}
3132

3233
/** @return array<int, array{array<string, mixed>, string, string|false, bool}> */
@@ -51,9 +52,9 @@ public static function loadCookiesProvider(): array
5152
#[DataProvider('urlSearchTypeProvider')]
5253
public function testSetUrlSearchType(mixed $type, string|false $expected): void
5354
{
54-
UserPreferences::$searchType = UserPreferences::URL_NONE;
55-
UserPreferences::setUrlSearchType($type);
56-
self::assertSame($expected, UserPreferences::$searchType);
55+
$userPreferences = new UserPreferences(searchType: UserPreferences::URL_NONE);
56+
$userPreferences->setUrlSearchType($type);
57+
self::assertSame($expected, $userPreferences->searchType);
5758
}
5859

5960
/** @return array<int, array{mixed, string|false}> */
@@ -74,19 +75,19 @@ public function testSetIsUserGroupTipsEnabled(): void
7475
$timeBackup = $_SERVER['REQUEST_TIME'];
7576
$_SERVER['REQUEST_TIME'] = 1726600070;
7677

77-
UserPreferences::$isUserGroupTipsEnabled = false;
78-
UserPreferences::setIsUserGroupTipsEnabled(true);
79-
self::assertTrue(UserPreferences::$isUserGroupTipsEnabled);
78+
$userPreferences = new UserPreferences(isUserGroupTipsEnabled: false);
79+
$userPreferences->setIsUserGroupTipsEnabled(true);
80+
self::assertTrue($userPreferences->isUserGroupTipsEnabled);
8081

81-
UserPreferences::$isUserGroupTipsEnabled = true;
82-
UserPreferences::setIsUserGroupTipsEnabled(false);
83-
self::assertFalse(UserPreferences::$isUserGroupTipsEnabled);
82+
$userPreferences = new UserPreferences(isUserGroupTipsEnabled: true);
83+
$userPreferences->setIsUserGroupTipsEnabled(false);
84+
self::assertFalse($userPreferences->isUserGroupTipsEnabled);
8485

8586
$_SERVER['REQUEST_TIME'] = 1726600066;
8687

87-
UserPreferences::$isUserGroupTipsEnabled = false;
88-
UserPreferences::setIsUserGroupTipsEnabled(false);
89-
self::assertTrue(UserPreferences::$isUserGroupTipsEnabled);
88+
$userPreferences = new UserPreferences(isUserGroupTipsEnabled: false);
89+
$userPreferences->setIsUserGroupTipsEnabled(false);
90+
self::assertTrue($userPreferences->isUserGroupTipsEnabled);
9091

9192
$_SERVER['REQUEST_TIME'] = $timeBackup;
9293
}

0 commit comments

Comments
 (0)