1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
From: William Desportes <williamdes@wdes.fr>
Date: Fri, 2 May 2025 21:56:19 +0200
Subject: Upgrade to PHP 8
Add types everywhere and ensure they are checked
I ran this into phpstan level max to find all the missed parts
Globals are needed to access the config variables
Origin: vendor
Forwarded: no
Bug-Debian: https://bugs.debian.org/1104085
---
phpwebcounter.php | 27 ++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/phpwebcounter.php b/phpwebcounter.php
index d04bdb8..245f955 100644
--- a/phpwebcounter.php
+++ b/phpwebcounter.php
@@ -1,4 +1,7 @@
<?php
+
+declare(strict_types=1);
+
#####################################################################
# PHP Web Counter, version 1.0 #
# #
@@ -28,8 +31,10 @@ require("/etc/phpwebcounter/phpwebcounter.config.php");
##########################
##########################
-function hits_webcounter($webcounter_id=0, $hits_file=null, $vis=null, $path_images=null, $counter_pad, $step)
+function hits_webcounter(int $webcounter_id=0, string|null $hits_file=null, bool|null $vis=null, string|null $path_images=null, int $counter_pad=6, int $step=1): void
{
+ global $path_hits_file, $visible;
+
if (is_null($hits_file))
$hits_file = $path_hits_file;
if (is_null($vis))
@@ -37,7 +42,7 @@ function hits_webcounter($webcounter_id=0, $hits_file=null, $vis=null, $path_ima
$counter = read_webcounter($webcounter_id, $hits_file);
increment_webcounter($webcounter_id, $counter, $step, $hits_file);
-
+
if (!$vis) return;
if (is_null($path_images)) {
echo $counter;
@@ -46,10 +51,10 @@ function hits_webcounter($webcounter_id=0, $hits_file=null, $vis=null, $path_ima
}
}
-function show_webcounter($counter, $path_images, $counter_pad=6)
+function show_webcounter(int $counter, string $path_images, int $counter_pad=6): void
{
echo "<SPAN>";
- $counter = str_pad($counter, $counter_pad, '0', STR_PAD_LEFT);
+ $counter = str_pad((string) $counter, $counter_pad, '0', STR_PAD_LEFT);
for ($i=0; $i< strlen($counter) ; $i++) {
$digit=$counter[$i];
echo "<IMG src='$path_images/$digit.png' alt='$digit' />";
@@ -57,26 +62,30 @@ function show_webcounter($counter, $path_images, $counter_pad=6)
echo "</SPAN>";
}
-function read_webcounter($webcounter_id, $hits_file=null)
+function read_webcounter(int $webcounter_id, string|null $hits_file=null): int
{
+ global $path_hits_file;
+
if (is_null($hits_file))
$hits_file = $path_hits_file;
$file="$hits_file/hits_webcounter.$webcounter_id";
$fp = fopen($file, 'a+');
- $counter= fread($fp,1024);
+ $counter = (int) fread($fp,1024);
fclose($fp);
return $counter;
}
-function increment_webcounter($webcounter_id, $counter, $step=1, $hits_file=null)
+function increment_webcounter(int $webcounter_id, int $counter, int $step=1, string|null $hits_file=null): void
{
+ global $path_hits_file;
+
if (is_null($hits_file))
$hits_file = $path_hits_file;
$file="$hits_file/hits_webcounter.$webcounter_id";
$fp = fopen($file, 'w');
- fwrite($fp,$counter+$step);
+ fwrite($fp,(string) ($counter + $step));
fclose($fp);
}
@@ -88,7 +97,7 @@ function increment_webcounter($webcounter_id, $counter, $step=1, $hits_file=null
hits_webcounter($webcounter_id,$path_hits_file,$visible,$path_images,$counter_pad, $step);
-if ($banner == '1') {
+if ($banner == '1') {
echo '<br><a href="http://phpwebcounter.sourceforge.net"><font size=1px>PHP Web Counter</a></font size><br>';
}
?>
|