Skip to content

Commit f17e1a1

Browse files
committed
Refactor codebase to adhere to strict coding standards
- Split DocumentationService into MarkdownRenderer and ContentProcessor for better separation of concerns - Apply strict types (strict_types=1) to Controllers and Services - Optimize routes/web.php by removing closures and utilizing controller groups - Clean up response handling in DocumentController and LocaleController"
1 parent 6934814 commit f17e1a1

File tree

6 files changed

+424
-376
lines changed

6 files changed

+424
-376
lines changed

routes/web.php

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,41 @@
44
use Xoshbin\Pertuk\Http\Controllers\DocumentController;
55
use Xoshbin\Pertuk\Http\Controllers\LocaleController;
66

7-
// Locale switching route (global, not prefixed) - supports both GET and POST
7+
// Locale switching route
88
Route::match(['GET', 'POST'], '/locale/{locale}', [LocaleController::class, 'setLocale'])
99
->name('pertuk.locale.set')
1010
->where('locale', implode('|', config('pertuk.supported_locales', ['en'])));
1111

12-
$routePrefix = config('pertuk.route_prefix', 'docs');
13-
$middleware = config('pertuk.middleware', []);
14-
15-
Route::prefix($routePrefix)
16-
->middleware($middleware)
12+
Route::prefix(config('pertuk.route_prefix', 'docs'))
13+
->middleware(config('pertuk.middleware', []))
1714
->name('pertuk.docs.')
1815
->group(function () {
19-
$controller = DocumentController::class;
20-
21-
// Redirect root /docs to default locale
22-
Route::get('/', function () {
23-
$default = config('pertuk.default_locale', 'en');
24-
// If default version is set, we might want to include it, but let's stick to locale/slug for now
25-
// or redirect to the most "canonical" starting point.
26-
// For now, mirroring existing behavior but cleaner.
27-
28-
return redirect()->route(config('pertuk.route_prefix', 'docs').'.show', [
29-
'locale' => $default,
30-
'slug' => 'index',
31-
]);
32-
})->name('index');
33-
34-
$supportedLocales = (array) config('pertuk.supported_locales', ['en']);
35-
$localeConstraint = implode('|', $supportedLocales);
36-
$versionConstraint = '(?!('.$localeConstraint.')$)[a-zA-Z0-9\.]+';
37-
38-
// Search routes
39-
Route::controller($controller)->group(function () use ($localeConstraint, $versionConstraint) {
16+
$locales = implode('|', (array) config('pertuk.supported_locales', ['en']));
17+
$version = '(?!('.$locales.')$)[a-zA-Z0-9\.]+';
18+
19+
Route::controller(DocumentController::class)->group(function () use ($locales, $version) {
20+
// Root redirect
21+
Route::get('/', 'root')->name('index');
22+
23+
// Search index
4024
Route::get('/{locale}/index.json', 'searchIndex')
41-
->where('locale', $localeConstraint)
25+
->where('locale', $locales)
4226
->name('search.json');
4327

4428
Route::get('/{version}/{locale}/index.json', 'searchIndex')
45-
->where('version', $versionConstraint)
46-
->where('locale', $localeConstraint)
29+
->where('version', $version)
30+
->where('locale', $locales)
4731
->name('version.search.json');
4832

49-
// Documentation routes
50-
// Note: Order matters. Versioned route first.
33+
// Documentation pages
5134
Route::get('/{version}/{locale}/{slug?}', 'show')
52-
->where('version', $versionConstraint)
53-
->where('locale', $localeConstraint)
35+
->where('version', $version)
36+
->where('locale', $locales)
5437
->where('slug', '.*')
5538
->name('version.show');
5639

5740
Route::get('/{locale}/{slug?}', 'show')
58-
->where('locale', $localeConstraint)
41+
->where('locale', $locales)
5942
->where('slug', '.*')
6043
->name('show');
6144
});

src/Http/Controllers/DocumentController.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Xoshbin\Pertuk\Http\Controllers;
46

7+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8+
use Illuminate\Contracts\View\View as ViewContract;
9+
use Illuminate\Http\JsonResponse;
10+
use Illuminate\Http\RedirectResponse;
511
use Illuminate\Http\Request;
12+
use Illuminate\Http\Response as HttpResponse;
613
use Illuminate\Routing\Controller;
714
use Illuminate\Support\Facades\App;
815
use Illuminate\Support\Facades\Session;
@@ -11,7 +18,24 @@
1118

1219
class DocumentController extends Controller
1320
{
14-
public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
21+
/**
22+
* Redirect root to default locale.
23+
*/
24+
public function root(): RedirectResponse
25+
{
26+
$default = config('pertuk.default_locale', 'en');
27+
$prefix = config('pertuk.route_prefix', 'docs');
28+
29+
return redirect()->route($prefix.'.show', [
30+
'locale' => $default,
31+
'slug' => 'index',
32+
]);
33+
}
34+
35+
/**
36+
* Show a documentation page.
37+
*/
38+
public function show(Request $request): HttpResponse|ViewContract|RedirectResponse
1539
{
1640
// Session starting should ideally be in middleware, leaving it for now if middleware is minimal.
1741
if (! Session::isStarted()) {
@@ -22,8 +46,13 @@ public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Co
2246
$locale = $request->route('locale');
2347
$slug = $request->route('slug') ?? 'index';
2448

49+
$locale = is_string($locale) ? $locale : 'en';
50+
$slug = is_string($slug) ? $slug : 'index';
51+
$version = is_string($version) ? $version : null;
52+
2553
// Validation - 404 if locale not supported
26-
abort_unless(in_array($locale, config('pertuk.supported_locales', ['en'])), 404);
54+
$supportedLocales = (array) config('pertuk.supported_locales', ['en']);
55+
abort_unless(in_array($locale, $supportedLocales, true), 404);
2756

2857
// Set application locale state
2958
App::setLocale($locale);
@@ -34,7 +63,7 @@ public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Co
3463
// Attempt to retrieve the document
3564
try {
3665
$data = $docs->get($locale, $slug);
37-
} catch (\Illuminate\Contracts\Filesystem\FileNotFoundException $e) {
66+
} catch (FileNotFoundException) {
3867
// Fallback: If 'index' is requested but no physical index.md exists,
3968
// we render the index view with a list of all documents.
4069
if ($slug === 'index') {
@@ -52,15 +81,15 @@ public function show(Request $request): \Illuminate\Http\Response|\Illuminate\Co
5281

5382
return $this->responseWithCacheHeaders(
5483
response()->view('pertuk::show', $viewData),
55-
$data['mtime'],
56-
$data['etag']
84+
(int) $data['mtime'],
85+
(string) $data['etag']
5786
);
5887
}
5988

6089
/**
61-
* Render the index page with grouped documentation items.
90+
* Helper to show the index page when index.md is missing.
6291
*/
63-
protected function showIndex(DocumentationService $docs, string $locale): \Illuminate\Contracts\View\View
92+
protected function showIndex(DocumentationService $docs, string $locale): ViewContract
6493
{
6594
$items = $docs->list($locale);
6695

@@ -81,7 +110,7 @@ protected function showIndex(DocumentationService $docs, string $locale): \Illum
81110
/**
82111
* Attach HTTP caching headers to the response.
83112
*/
84-
protected function responseWithCacheHeaders(\Illuminate\Http\Response $response, int $mtime, string $etag): \Illuminate\Http\Response
113+
protected function responseWithCacheHeaders(HttpResponse $response, int $mtime, string $etag): HttpResponse
85114
{
86115
$lastModified = gmdate('D, d M Y H:i:s', $mtime).' GMT';
87116

@@ -102,9 +131,14 @@ protected function responseWithCacheHeaders(\Illuminate\Http\Response $response,
102131
]);
103132
}
104133

105-
public function searchIndex(Request $request, string $locale): \Illuminate\Http\JsonResponse
134+
/**
135+
* Search index endpoint.
136+
*/
137+
public function searchIndex(Request $request, string $locale): JsonResponse
106138
{
107139
$version = $request->route('version');
140+
$version = is_string($version) ? $version : null;
141+
108142
$items = DocumentationService::make($version)->buildIndex($locale);
109143

110144
return response()->json($items)->header('Content-Type', 'application/json');

src/Http/Controllers/LocaleController.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Xoshbin\Pertuk\Http\Controllers;
46

57
use Illuminate\Http\JsonResponse;
@@ -9,12 +11,10 @@
911
use Illuminate\Routing\Controller;
1012
use Illuminate\Support\Facades\App;
1113
use Illuminate\Support\Facades\Session;
14+
use Xoshbin\Pertuk\Services\DocumentationService;
1215

1316
class LocaleController extends Controller
1417
{
15-
/**
16-
* Set the application locale and store it in session.
17-
*/
1818
/**
1919
* Set the application locale and store it in session.
2020
*/
@@ -44,7 +44,7 @@ public function setLocale(Request $request, string $locale): Response|JsonRespon
4444
// Standard redirection
4545
$redirectUrl = $request->get('redirect') ?: $request->header('referer');
4646

47-
if ($redirectUrl && $this->isDocsUrl($redirectUrl)) {
47+
if ($redirectUrl && is_string($redirectUrl) && $this->isDocsUrl($redirectUrl)) {
4848
return redirect($this->getLocaleEquivalentUrl($redirectUrl, $locale));
4949
}
5050

@@ -68,7 +68,7 @@ protected function isDocsUrl(string $url): bool
6868
protected function getLocaleEquivalentUrl(string $url, string $locale): string
6969
{
7070
$docsPrefix = config('pertuk.route_prefix', 'docs');
71-
$versions = \Xoshbin\Pertuk\Services\DocumentationService::getAvailableVersions();
71+
$versions = DocumentationService::getAvailableVersions();
7272

7373
// Parse path from URL
7474
$path = parse_url($url, PHP_URL_PATH) ?? '/';
@@ -77,10 +77,7 @@ protected function getLocaleEquivalentUrl(string $url, string $locale): string
7777
$segments = explode('/', $path);
7878

7979
// Find where the docs prefix ends.
80-
// We assume the prefix is at the start of the path relative to the domain root.
81-
// If the app is in a subdirectory, this logic might need adjustment, but for now assuming standard setup.
82-
83-
$prefixSegments = explode('/', $docsPrefix);
80+
$prefixSegments = explode('/', (string) $docsPrefix);
8481
$remainder = array_slice($segments, count($prefixSegments));
8582

8683
if (empty($remainder)) {
@@ -90,7 +87,7 @@ protected function getLocaleEquivalentUrl(string $url, string $locale): string
9087
// Check structure: [version, locale, slug...] or [locale, slug...]
9188
$first = $remainder[0];
9289

93-
if (in_array($first, $versions)) {
90+
if (in_array($first, $versions, true)) {
9491
// Versioned URL: /docs/{version}/{locale}/{slug}
9592
// Replace the second segment (locale)
9693
if (isset($remainder[1])) {

0 commit comments

Comments
 (0)