Skip to content

Commit 8a95878

Browse files
committed
fix phpstan errors
1 parent 05d56d2 commit 8a95878

2 files changed

Lines changed: 40 additions & 20 deletions

File tree

public_html/lib/module/rpcn/games.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@
12891289
},
12901290
"NPWR00628_00": {
12911291
"title": ["無双OROCHI Z"],
1292-
"id": ["BLJM60139"]
1292+
"id": ["BLJM60139", "BLAS50130"]
12931293
},
12941294
"NPWR00629_00": {
12951295
"title": ["Age of Booty"],
@@ -4257,7 +4257,7 @@
42574257
},
42584258
"NPWR01730_00": {
42594259
"title": ["inFAMOUS 2"],
4260-
"id": ["BCES01143", "BCES01144", "BCES01229", "BCJS30075", "BCUS98125", "NPEA00318"]
4260+
"id": ["BCES01143", "BCES01144", "BCES01229", "BCJS30075", "BCUS98125", "NPEA00318", "BCAS20181"]
42614261
},
42624262
"NPWR01731_00": {
42634263
"title": ["Mercury Hg"],
@@ -6337,7 +6337,7 @@
63376337
},
63386338
"NPWR03466_00": {
63396339
"title": ["アーシャのアトリエ ~黄昏の大地の錬金術士~"],
6340-
"id": ["BLES01793", "BLJM60486", "BLUS31152"]
6340+
"id": ["BLES01793", "BLJM60486", "BLUS31152", "BLAS50502"]
63416341
},
63426342
"NPWR03468_00": {
63436343
"title": ["Far Cry 3"],
@@ -9025,7 +9025,7 @@
90259025
},
90269026
"NPWR06754_00": {
90279027
"title": ["MOBILE SUIT GUNDAM SIDE STORIES"],
9028-
"id": []
9028+
"id": ["BLAS50710"]
90299029
},
90309030
"NPWR06757_00": {
90319031
"title": ["Disney Infinity [2.0]"],
@@ -9341,7 +9341,7 @@
93419341
},
93429342
"NPWR07534_00": {
93439343
"title": ["The Evil Within"],
9344-
"id": []
9344+
"id": ["BCAS20333"]
93459345
},
93469346
"NPWR07535_00": {
93479347
"title": ["The Evil Within"],

public_html/lib/module/rpcn/inc-rpcn-stats.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ class RPCNStats {
33
private string $games_json;
44
private string $log_file;
55
private string $api_url;
6-
public array $title_regions = [];
76

87
public int $total_users = 0;
8+
9+
/** @var array<string, array<int, string>> */
10+
public array $title_regions = [];
11+
912
/** @var array<string, int> */
1013
public array $title_player_counts = [];
14+
15+
/** @var array<string, array<string>> */
16+
public array $title_ids = [];
17+
1118
public bool $has_error = false;
1219

1320
public function __construct(string $games_json, string $log_file, string $api_url) {
@@ -59,6 +66,9 @@ private function processStats(): void {
5966
}
6067

6168
$json_content = file_get_contents($this->games_json);
69+
if ($json_content === false) {
70+
throw new Exception("Unable to read {$this->games_json}");
71+
}
6272
$game_mappings = json_decode($json_content, true);
6373

6474
if (json_last_error() !== JSON_ERROR_NONE) {
@@ -67,15 +77,14 @@ private function processStats(): void {
6777

6878
// cURL
6979
$ch = curl_init();
80+
assert($this->api_url !== '');
7081
curl_setopt($ch, CURLOPT_URL, $this->api_url);
7182
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
7283
curl_setopt($ch, CURLOPT_HEADER, true);
7384
curl_setopt($ch, CURLOPT_HTTPHEADER, [
7485
'Accept: application/json',
7586
]);
7687

77-
$response = curl_exec($ch);
78-
7988
if (curl_errno($ch)) {
8089
$error_message = 'cURL error: ' . curl_error($ch);
8190
$this->log_error($error_message);
@@ -90,18 +99,19 @@ private function processStats(): void {
9099
throw new Exception($error_message);
91100
}
92101

93-
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
94-
$api_data = substr($response, $header_size);
95-
96-
curl_close($ch);
97-
98-
if ($api_data === false) {
99-
$last_error = error_get_last();
100-
$error_message = $last_error ? $last_error['message'] : 'Unknown error occurred while fetching API data';
102+
$response = curl_exec($ch);
103+
if ($response === false) {
104+
$error_message = 'cURL error: ' . curl_error($ch);
101105
$this->log_error($error_message);
102106
throw new Exception($error_message);
103107
}
104108

109+
/** @var string $response */
110+
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
111+
$api_data = substr($response, $header_size);
112+
113+
curl_close($ch);
114+
105115
$data = json_decode($api_data, true);
106116
if (json_last_error() !== JSON_ERROR_NONE) {
107117
$error_message = json_last_error_msg();
@@ -114,7 +124,7 @@ private function processStats(): void {
114124
// Merge Player Counts from API Data
115125
foreach ($game_mappings as $comm_id => $info) {
116126
$titles = $info['title'] ?? ["Unknown Game"];
117-
$game_title = $titles[0];
127+
$game_title = $titles[0] ?? 'Unknown Game';
118128

119129
// Initialize counts and ID array if not already set
120130
if (!isset($this->title_player_counts[$game_title])) {
@@ -131,18 +141,28 @@ private function processStats(): void {
131141

132142
foreach ($ids as $entry_id) {
133143
$region = $this->get_region_from_id($entry_id);
134-
if (!in_array($region, $this->title_regions[$game_title])) {
144+
if (!array_key_exists($game_title, $this->title_regions)) {
145+
$this->title_regions[$game_title] = [];
146+
}
147+
if (!in_array($region, $this->title_regions[$game_title], true)) {
135148
$this->title_regions[$game_title][] = $region;
136149
}
137150
}
138151

139-
sort($this->title_regions[$game_title], SORT_STRING);
152+
if (!empty($this->title_regions[$game_title])) {
153+
sort($this->title_regions[$game_title], SORT_STRING);
154+
}
140155
$comm_id_player_count = 0;
141156

142157
// First try psn_games (comm_id)
143158
if (isset($data['psn_games'][$comm_id])) {
144159
$value = $data['psn_games'][$comm_id];
145-
$comm_id_player_count += is_array($value) ? (int)$value[0] : (int)$value;
160+
161+
if (is_array($value) && isset($value[0])) {
162+
$comm_id_player_count += (int) $value[0];
163+
} elseif (is_int($value)) {
164+
$comm_id_player_count += $value;
165+
}
146166
}
147167

148168
// If we got a comm_id match, skip ticket_games

0 commit comments

Comments
 (0)