Skip to content

Commit e3c573a

Browse files
author
rahimi
committed
🐛 Fix phpstan errors
🐛 Fix phpstan errors 🐛 Fix phpstan errors
1 parent a0a12b5 commit e3c573a

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/FileCache.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CachingProxy;
44

5+
use RuntimeException;
6+
57
class FileCache
68
{
79
public function __construct()
@@ -14,13 +16,22 @@ public function getCacheKey(string $url): string
1416
return md5($url);
1517
}
1618

19+
/**
20+
* @return array<mixed>|null
21+
*/
1722
public function get(string $url): ?array
1823
{
1924
$fileName = $this->getCacheKey($url);
2025
$cacheFile = cache_path($fileName);
2126

22-
if (file_exists($cacheFile)) {
23-
$data = unserialize(file_get_contents($cacheFile));
27+
if (file_exists($cacheFile) && is_readable($cacheFile)) {
28+
$cacheFileContent = file_get_contents($cacheFile);
29+
30+
if ($cacheFileContent === false) {
31+
throw new RuntimeException("Failed to read Cache file `{$cacheFile}`");
32+
}
33+
34+
$data = unserialize($cacheFileContent);
2435

2536
// Check cache time to live
2637
if (time() - filemtime($cacheFile) < config('cache', 'ttl')) {
@@ -34,6 +45,9 @@ public function get(string $url): ?array
3445
return null;
3546
}
3647

48+
/**
49+
* @param array<mixed> $content
50+
*/
3751
public function set(string $url, array $content): bool
3852
{
3953
$fileName = $this->getCacheKey($url);

src/ProxyServer.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace CachingProxy;
44

5+
use RuntimeException;
6+
57
class ProxyServer
68
{
79
public function __construct(
810
private FileCache $cache = new FileCache()
911
) {
1012
}
1113

12-
public function handleRequest()
14+
public function handleRequest(): void
1315
{
1416
header('Access-Control-Allow-Origin: *');
1517
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
@@ -63,7 +65,13 @@ private function getTargetUrl(): ?string
6365
return urldecode($_GET['url']);
6466
}
6567

66-
$input = json_decode(file_get_contents('php://input'), true);
68+
$requestBody = file_get_contents('php://input');
69+
70+
if ($requestBody === false) {
71+
throw new RuntimeException("Failed to read `POST` request body");
72+
}
73+
74+
$input = json_decode($requestBody, true);
6775

6876
if ($input['url'] !== null) {
6977
return $input['url'];
@@ -88,7 +96,10 @@ private function checkFilterUrl(string $url): bool
8896
return true;
8997
}
9098

91-
private function makeRequest(string $url)
99+
/**
100+
* @return array<mixed>
101+
*/
102+
private function makeRequest(string $url): array
92103
{
93104
$ch = curl_init();
94105

@@ -107,7 +118,14 @@ private function makeRequest(string $url)
107118
// Handle POST requests
108119
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
109120
curl_setopt($ch, CURLOPT_POST, true);
110-
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
121+
122+
$requestBody = file_get_contents('php://input');
123+
124+
if ($requestBody === false) {
125+
throw new RuntimeException("Failed to read `POST` request body");
126+
}
127+
128+
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
111129
}
112130

113131
// Forward headers (excluding some sensitive headers)
@@ -159,7 +177,10 @@ private function makeRequest(string $url)
159177
];
160178
}
161179

162-
private function cacheResponse($url, $response)
180+
/**
181+
* @param array<mixed> $response
182+
*/
183+
private function cacheResponse(string $url, array $response): void
163184
{
164185
if ($response['status_code'] === 200) {
165186
header('X-Proxy-Cache: MISS');
@@ -168,13 +189,19 @@ private function cacheResponse($url, $response)
168189
}
169190
}
170191

171-
private function sendCachedResponse($cachedResponse)
192+
/**
193+
* @param array<mixed> $cachedResponse
194+
*/
195+
private function sendCachedResponse(array $cachedResponse): void
172196
{
173197
header('X-Proxy-Cache: HIT');
174198
$this->sendResponse($cachedResponse);
175199
}
176200

177-
private function sendResponse(array $response)
201+
/**
202+
* @param array<mixed> $response
203+
*/
204+
private function sendResponse(array $response): void
178205
{
179206
http_response_code($response['status_code']);
180207

@@ -190,7 +217,7 @@ private function sendResponse(array $response)
190217
echo $response['body'];
191218
}
192219

193-
private function sendError($message, $statusCode = 500)
220+
private function sendError(string $message, int $statusCode = 500): void
194221
{
195222
http_response_code($statusCode);
196223
header('Content-Type: application/json');

src/helpers/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function config_path(string $file): string
2222
}
2323

2424
if (! function_exists('dd')) {
25-
function dd(...$vars): void
25+
function dd(mixed ...$vars): void
2626
{
2727
echo '<pre>';
2828
var_dump(...$vars);
@@ -32,7 +32,7 @@ function dd(...$vars): void
3232
}
3333

3434
if (! function_exists('config')) {
35-
function config(string $fileName, string $index)
35+
function config(string $fileName, string $index): mixed
3636
{
3737
$fileName = $fileName . '.php';
3838

0 commit comments

Comments
 (0)