Skip to content

Commit 3a876db

Browse files
authored
Merge pull request #7 from Xoshbin/develop
fix: resolve PHPStan errors and Request deprecations
2 parents e4d4292 + 5c428d3 commit 3a876db

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

config/pertuk.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
],
4747

4848
// GitHub Repo & Branch for "Edit on GitHub" links
49-
'github_repo' => env('PERTUK_GITHUB_REPO', 'username/repo'),
50-
'github_branch' => env('PERTUK_GITHUB_BRANCH', 'main'),
49+
'github_repo' => env('PERTUK_GITHUB_REPO', 'username/repo'), // @phpstan-ignore-line
50+
'github_branch' => env('PERTUK_GITHUB_BRANCH', 'main'), // @phpstan-ignore-line
5151

5252
];

src/Http/Controllers/LocaleController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function setLocale(Request $request, string $locale): Response|JsonRespon
4242
}
4343

4444
// Standard redirection
45-
$redirectUrl = $request->get('redirect') ?: $request->header('referer');
45+
$redirectUrl = $request->input('redirect') ?: $request->header('referer');
4646

4747
if ($redirectUrl && is_string($redirectUrl) && $this->isDocsUrl($redirectUrl)) {
4848
return redirect($this->getLocaleEquivalentUrl($redirectUrl, $locale));

src/Services/DocumentationService.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ public function get(string $locale, string $slug): array
217217
}
218218

219219
$mtime = File::lastModified($path);
220-
$cacheKey = 'pertuk:docs:'.$locale.':'.md5($path.':'.$mtime);
220+
// Use realpath to ensure consistent cache keys regardless of symlinks/relative paths
221+
$realPath = realpath($path) ?: $path;
222+
$cacheKey = 'pertuk:docs:'.$locale.':'.md5($realPath.':'.$mtime);
221223

222224
// Get cached value and validate it
223225
$cached = Cache::get($cacheKey);
@@ -247,6 +249,12 @@ private function isValidCachedDocument(mixed $data): bool
247249
private function generateDocumentData(string $path, string $locale, string $slug, int $mtime): array
248250
{
249251
$raw = File::get($path);
252+
253+
// @phpstan-ignore-next-line
254+
if (! is_string($raw)) {
255+
throw new \RuntimeException("Failed to read file: {$path}");
256+
}
257+
250258
try {
251259
$front = YamlFrontMatter::parse($raw);
252260
$content = $front->body();
@@ -424,15 +432,16 @@ protected function buildAlternates(string $locale, string $slug): array
424432
$supported = (array) config('pertuk.supported_locales', ['en']);
425433

426434
$labels = (array) config('pertuk.locale_labels', []);
427-
$prefix = config('pertuk.route_prefix', 'docs');
435+
$prefix = (string) config('pertuk.route_prefix', 'docs');
428436

429437
$alternates = [];
430438
foreach ($supported as $loc) {
439+
$loc = (string) $loc;
431440
$path = $this->resolvePath($loc, $slug);
432441
if ($path) {
433442
$alternates[] = [
434443
'locale' => $loc,
435-
'label' => $labels[$loc] ?? strtoupper($loc),
444+
'label' => (string) ($labels[$loc] ?? strtoupper($loc)),
436445
'url' => url('/'.$prefix.'/'.($this->version ? $this->version.'/' : '').$loc.'/'.$slug),
437446
'active' => $loc === $locale,
438447
];

tests/Feature/CacheTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
// Verify cache key exists
3030
$path = $this->getTestDocsPath().'/en/test-cache.md';
3131
$mtime = File::lastModified($path);
32-
$cacheKey = 'pertuk:docs:en:'.md5($path.':'.$mtime);
32+
$realPath = realpath($path) ?: $path;
33+
$cacheKey = 'pertuk:docs:en:'.md5($realPath.':'.$mtime);
3334

3435
expect(Cache::has($cacheKey))->toBeTrue();
3536
});
@@ -75,7 +76,8 @@
7576
// Verify it's cached
7677
$path = $this->getTestDocsPath().'/en/test-ttl.md';
7778
$mtime = File::lastModified($path);
78-
$cacheKey = 'pertuk:docs:en:'.md5($path.':'.$mtime);
79+
$realPath = realpath($path) ?: $path;
80+
$cacheKey = 'pertuk:docs:en:'.md5($realPath.':'.$mtime);
7981
expect(Cache::has($cacheKey))->toBeTrue();
8082

8183
// Wait for TTL to expire
@@ -102,8 +104,11 @@
102104
$mtime1 = File::lastModified($path1);
103105
$mtime2 = File::lastModified($path2);
104106

105-
$cacheKey1 = 'pertuk:docs:en:'.md5($path1.':'.$mtime1);
106-
$cacheKey2 = 'pertuk:docs:en:'.md5($path2.':'.$mtime2);
107+
$realPath1 = realpath($path1) ?: $path1;
108+
$realPath2 = realpath($path2) ?: $path2;
109+
110+
$cacheKey1 = 'pertuk:docs:en:'.md5($realPath1.':'.$mtime1);
111+
$cacheKey2 = 'pertuk:docs:en:'.md5($realPath2.':'.$mtime2);
107112

108113
expect($cacheKey1)->not->toBe($cacheKey2);
109114
expect(Cache::has($cacheKey1))->toBeTrue();
@@ -146,7 +151,8 @@
146151
// Manually corrupt the cache
147152
$path = $this->getTestDocsPath().'/en/cache-corruption.md';
148153
$mtime = File::lastModified($path);
149-
$cacheKey = 'pertuk:docs:en:'.md5($path.':'.$mtime);
154+
$realPath = realpath($path) ?: $path;
155+
$cacheKey = 'pertuk:docs:en:'.md5($realPath.':'.$mtime);
150156

151157
Cache::put($cacheKey, 'corrupted-data', 60);
152158

tests/Feature/FileReadingTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
$filePath = $this->createTestMarkdownFile('test-permissions.md', "# Test\n\nContent");
157157

158158
// Make file unreadable (this might not work on all systems)
159-
if (chmod($filePath, 0000)) {
159+
if (chmod($filePath, 0000) && ! is_readable($filePath)) {
160160
$service = DocumentationService::make();
161161

162162
// Should handle the error gracefully
@@ -166,8 +166,8 @@
166166
// Restore permissions for cleanup
167167
chmod($filePath, 0644);
168168
} else {
169-
// Skip test if we can't change permissions
170-
$this->markTestSkipped('Cannot change file permissions on this system');
169+
// Skip test if we can't change permissions or it's still readable (e.g. root)
170+
$this->markTestSkipped('Cannot make file unreadable on this system');
171171
}
172172
});
173173

0 commit comments

Comments
 (0)