Skip to content

Commit ceb7ee4

Browse files
committed
refactor: remove error suppression operator (@) and add proper error handling
Replace error suppression operator (@) with explicit file existence and readability checks. Add new helper functions in include/file.inc (file_get_size, file_get_mtime, file_get_contents_if_exists) to safely handle file operations. This improves code reliability by catching actual errors rather than silencing them, and makes error handling explicit and testable throughout the codebase.
1 parent ce33ea6 commit ceb7ee4

18 files changed

Lines changed: 92 additions & 40 deletions

bin/news2html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (count($_SERVER['argv']) < 2) {
1111
}
1212
$news_file = array_shift($_SERVER['argv']);
1313
$version = array_shift($_SERVER['argv']);
14-
$changelog = @array_shift($_SERVER['argv']);
14+
$changelog = array_shift($_SERVER['argv']);
1515

1616
// find NEWS entry
1717
$fp = fopen($news_file, "r");

cal.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,11 @@ function display_events_for_day($day, $events): void
253253
// Find a single event in the events file by ID
254254
function load_event($id)
255255
{
256+
$path = "backend/events.csv";
257+
if (!file_exists($path) || !is_readable($path)) { return false; }
258+
256259
// Open events CSV file, return on error
257-
$fp = @fopen("backend/events.csv",'r');
260+
$fp = fopen($path,'r');
258261
if (!$fp) { return false; }
259262

260263
// Read as we can, event by event
@@ -289,7 +292,10 @@ function load_events($from, $whole_month = false)
289292
$events = $seen = [];
290293

291294
// Try to open the events file for reading, return if unable to
292-
$fp = @fopen("backend/events.csv",'r');
295+
$path = "backend/events.csv";
296+
if (!file_exists($path) || !is_readable($path)) { return false; }
297+
298+
$fp = fopen($path,'r');
293299
if (!$fp) { return false; }
294300

295301
// For all events, read in the event and check it if fits our scope
@@ -357,7 +363,9 @@ function read_event($fp)
357363
] = $linearr;
358364

359365
// Get info on recurring event
360-
@[$recur, $recur_day] = explode(":", $recur, 2);
366+
$recurParts = explode(":", $recur, 2);
367+
$recur = $recurParts[0];
368+
$recur_day = $recurParts[1] ?? null;
361369

362370
// Return with SQL-resultset like array
363371
return [

download-docs.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@
114114
$link_to = "/distributions/manual/$filename";
115115

116116
// Try to get size and changed date
117-
$size = @filesize($filepath);
118-
$changed = @filemtime($filepath);
117+
$size = file_get_size($filepath);
118+
$changed = file_get_size($filepath);
119119

120120
// Size available, collect information
121121
if ($size !== false) {
@@ -138,8 +138,8 @@
138138
$actual_file = $_SERVER['DOCUMENT_ROOT'] . "/distributions/manual/php_manual_chm.zip";
139139
if (file_exists($actual_file)) {
140140
$link_to = "/get/php_manual_chm.zip/from/a/mirror";
141-
$size = @filesize($actual_file);
142-
$changed = @filemtime($actual_file);
141+
$size = file_get_size($actual_file);
142+
$changed = filemtime($actual_file);
143143
if ($size !== FALSE) {
144144
$files["en"]["zip"] = array(
145145
$link_to,

images/elephpants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
// read out photo metadata
4949
$path = __DIR__ . '/elephpants';
50-
$json = @file_get_contents($path . '/flickr.json');
50+
$json = file_get_contents_if_exists($path . '/flickr.json');
5151
$photos = json_decode($json, true);
5252

5353
// if no photo data, respond with an error.

include/do-download.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function get_actual_download_file($file)
1414
// Find out what is the exact file requested
1515
$found = false;
1616
foreach ($possible_files as $name => $log) {
17-
if (@file_exists($_SERVER['DOCUMENT_ROOT'] . '/distributions/' . $name)) {
17+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/distributions/' . $name)) {
1818
$found = $name;
1919
break;
2020
}

include/download-instructions/windows-downloads.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
$baseDownloads = 'https://downloads.php.net/~windows/releases/archives/';
33

4-
$dataStr = @file_get_contents(__DIR__ . '/../../backend/win-releases.json');
4+
$dataStr = file_get_contents_if_exists(__DIR__ . '/../../backend/win-releases.json');
55
$releases = $dataStr ? json_decode($dataStr, true) : null;
66

77
if (!is_array($releases)) {

include/file.inc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
*/
6+
7+
function file_get_size(string $filename): int|false
8+
{
9+
if (!file_exists($filename) || !is_readable($filename)) {
10+
return false;
11+
}
12+
13+
return filesize($filename);
14+
}
15+
16+
function file_get_mtime(string $filename): int|false
17+
{
18+
if (!file_exists($filename) || !is_readable($filename)) {
19+
return false;
20+
}
21+
22+
return filemtime($filename);
23+
}
24+
25+
function file_get_contents_if_exists(string $filename): string|false
26+
{
27+
if (!file_exists($filename) || !is_readable($filename)) {
28+
return false;
29+
}
30+
31+
return file_get_contents($filename);
32+
}
33+
{
34+
35+
}

include/footer.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ if (!empty($_SERVER['BASE_PAGE'])
101101
$jsfiles = ["ext/jquery-3.6.0.min.js", "ext/FuzzySearch.min.js", "ext/mousetrap.min.js", "ext/jquery.scrollTo.min.js", "search.js", "common.js"];
102102
foreach ($jsfiles as $filename) {
103103
$path = dirname(__DIR__) . '/js/' . $filename;
104-
echo '<script src="/cached.php?t=' . @filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
104+
echo '<script src="/cached.php?t=' . filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
105105
}
106106
?>
107107
<?php
108108
$jsfiles = ["interactive-examples.js"];
109109
foreach ($jsfiles as $filename) {
110110
$path = dirname(__DIR__) . '/js/' . $filename;
111-
echo '<script type="module" src="/cached.php?t=' . @filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
111+
echo '<script type="module" src="/cached.php?t=' . filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
112112
}
113113
?>
114114

include/get-download.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $site_config = [
1818
// Find out what is the exact file requested
1919
$file = false;
2020
foreach ($possible_files as $name) {
21-
if (@file_exists($_SERVER['DOCUMENT_ROOT'] . '/distributions/' . $name)) {
21+
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/distributions/' . $name)) {
2222
$file = $name;
2323
break;
2424
}
@@ -43,7 +43,7 @@ EOT;
4343
// Set local file name
4444
$local_file = $_SERVER['DOCUMENT_ROOT'] . '/distributions/' . $file;
4545
// Try to get filesize to display
46-
$size = @filesize($local_file);
46+
$size = file_get_size($local_file);
4747
}
4848
?>
4949
</div>

include/header.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ foreach($css_files as $filename) {
2020
$filename = "/styles/$filename";
2121
}
2222
$path = dirname(__DIR__) . $filename;
23-
$CSS[$filename] = @filemtime($path);
23+
$CSS[$filename] = filemtime($path);
2424
}
2525

2626
$JS = [];
2727
if (isset($config["js_files"])) {
2828
foreach($config['js_files'] as $filename) {
2929
$path = dirname(__DIR__) . '/' . $filename;
30-
$JS[$filename] = @filemtime($path);
30+
$JS[$filename] = filemtime($path);
3131
}
3232
}
3333

0 commit comments

Comments
 (0)