@@ -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