Skip to content

Commit dad6e22

Browse files
chore: add mago for linting and formatting
1 parent 3749f68 commit dad6e22

20 files changed

+160
-129
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
"scripts": {
2020
"test": "vendor/bin/phpunit",
2121
"mago:lint": "vendor/bin/mago lint",
22-
"mago:fix": "vendor/bin/mago fix"
22+
"mago:fix": "vendor/bin/mago lint --fix",
23+
"mago:fmt": "vendor/bin/mago fmt",
24+
"qa": [
25+
"@mago:fmt",
26+
"@mago:lint",
27+
"@test"
28+
]
2329
},
2430
"authors": [
2531
{

src/Contracts/MergeableProp.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
interface MergeableProp
88
{
9-
public bool $shouldMerge { get; }
9+
public bool $shouldMerge {
10+
get;
11+
}
1012

1113
public function merge(): self;
1214
}

src/Http/InertiaResponse.php

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use function Tempest\invoke;
2020
use function Tempest\Support\arr;
21+
use function Tempest\Support\str;
2122

2223
final class InertiaResponse implements Response
2324
{
@@ -30,36 +31,34 @@ public function __construct(
3031
readonly string $rootView,
3132
readonly string $version,
3233
) {
33-
3434
$deferredProps = self::resolvePropKeysThatShouldDefer(
3535
props: $props,
3636
request: $request,
37-
component: $page
37+
component: $page,
3838
);
3939

4040
$mergeProps = self::resolvePropKeysThatShouldMerge(
4141
props: $props,
42-
request: $request
42+
request: $request,
4343
);
4444

4545
// Build page data immutably
4646
$pageData = array_merge(
4747
[
48-
'component' => $page,
49-
'props' => self::composeProps(
48+
'component' => $page,
49+
'props' => self::composeProps(
5050
props: $this->props,
5151
request: $this->request,
52-
component: $page
52+
component: $page,
5353
),
54-
'url' => $request->uri,
55-
'version' => $version,
54+
'url' => $request->uri,
55+
'version' => $version,
5656
],
5757
count($deferredProps) ? ['deferredProps' => $deferredProps] : [],
58-
count($mergeProps) ? ['mergeProps' => $mergeProps] : []
58+
count($mergeProps) ? ['mergeProps' => $mergeProps] : [],
5959
);
6060

61-
$isInertia = ($request->headers->offsetExists(Header::INERTIA)
62-
&& $request->headers[Header::INERTIA] === 'true');
61+
$isInertia = $request->headers->offsetExists(Header::INERTIA) && $request->headers[Header::INERTIA] === 'true';
6362

6463
$this->body = $isInertia
6564
? (function () use ($pageData) {
@@ -70,7 +69,7 @@ public function __construct(
7069
})()
7170
: new InertiaBaseView(
7271
view: $rootView,
73-
pageData: $pageData
72+
pageData: $pageData,
7473
);
7574
}
7675

@@ -88,11 +87,7 @@ private static function composeProps(array $props, Request $request, string $com
8887
$always = static::resolveAlwaysProps($props);
8988
$partial = static::resolvePartialProps($request, $component, $props);
9089

91-
return static::evaluateProps(
92-
array_merge($always, $partial),
93-
$request,
94-
true
95-
);
90+
return static::evaluateProps(array_merge($always, $partial), $request, true);
9691
}
9792

9893
/**
@@ -119,20 +114,13 @@ private static function resolvePartialProps(Request $request, string $component,
119114
$only = array_filter(explode(',', $headers[Header::PARTIAL_ONLY] ?? ''));
120115
$except = array_filter(explode(',', $headers[Header::PARTIAL_EXCEPT] ?? ''));
121116

122-
$filtered = $only
123-
? array_intersect_key($props, array_flip($only))
124-
: $props;
117+
$filtered = $only ? array_intersect_key($props, array_flip($only)) : $props;
125118

126-
return array_filter(
127-
$filtered,
128-
static fn($key) => !in_array($key, $except, true),
129-
ARRAY_FILTER_USE_KEY
130-
);
119+
return array_filter($filtered, static fn($key) => !in_array($key, $except, true), ARRAY_FILTER_USE_KEY);
131120
}
132121

133122
private static function resolvePropKeysThatShouldDefer(array $props, Request $request, string $component): array
134123
{
135-
136124
if (static::isPartial($request, $component)) {
137125
return [];
138126
}
@@ -143,7 +131,7 @@ private static function resolvePropKeysThatShouldDefer(array $props, Request $re
143131
})
144132
->map(fn(DeferProp $prop, string $key) => [
145133
'group' => $prop->group,
146-
'key' => $key,
134+
'key' => $key,
147135
])
148136
->groupBy(fn(array $prop) => $prop['group'])
149137
->map(fn(array $group) => arr($group)->pluck('key')->toArray())
@@ -164,33 +152,27 @@ private static function resolvePropKeysThatShouldMerge(array $props, Request $re
164152
* @pure
165153
* Evaluates props recursively.
166154
*/
167-
private static function evaluateProps(array $props, Request $request): array
155+
private static function evaluateProps(array $props, Request $request, bool $unpackDotProps = true): array // @mago-expect best-practices/no-boolean-flag-parameter
168156
{
169157
return arr($props)
170158
->map(function ($value, string|int $key) use ($request): array {
171-
$evaluated = $value instanceof Closure ? invoke($value) : $value;
172-
$evaluated = ($evaluated instanceof LazyProp || $evaluated instanceof AlwaysProp)
173-
? $evaluated()
174-
: $evaluated;
175-
$evaluated = $evaluated instanceof ImmutableArray
176-
? $evaluated->toArray()
177-
: $evaluated;
159+
$evaluated = ($value instanceof Closure) ? invoke($value) : $value;
160+
$evaluated =
161+
$evaluated instanceof LazyProp || $evaluated instanceof AlwaysProp ? $evaluated() : $evaluated;
162+
$evaluated = ($evaluated instanceof ImmutableArray) ? $evaluated->toArray() : $evaluated;
178163
$evaluated = is_array($evaluated)
179-
? self::evaluateProps($evaluated, $request, false)
164+
? self::evaluateProps($evaluated, $request, unpackDotProps: false)
180165
: $evaluated;
181166

182167
return [$key, $evaluated];
183168
})
184-
->reduce(
185-
function (array $acc, array $item): array {
186-
[$key, $value] = $item;
187-
if (str_contains($key, '.')) {
188-
return arr($acc)->set($key, $value)->toArray();
189-
}
190-
$acc[$key] = $value;
191-
return $acc;
192-
},
193-
[]
194-
);
169+
->reduce(function (array $acc, array $item) use ($unpackDotProps): array {
170+
[$key, $value] = $item;
171+
if ($unpackDotProps && is_string($key) && str_contains($key, '.')) {
172+
return arr($acc)->set($key, $value)->toArray();
173+
}
174+
$acc[$key] = $value;
175+
return $acc;
176+
}, []);
195177
}
196178
}

src/Http/Middleware.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
final class Middleware implements HttpMiddleware
2121
{
22-
public function __construct(private Inertia $inertia, private Container $container) {}
22+
public function __construct(
23+
private Inertia $inertia,
24+
private Container $container,
25+
) {
26+
}
2327

2428
#[EventHandler(event: KernelEvent::BOOTED)]
2529
public function register(): void
@@ -47,7 +51,10 @@ public function __invoke(Request $request, HttpMiddlewareCallable $next): Respon
4751
return $this->inertia->location($request->uri);
4852
}
4953

50-
if ($response->status === Status::FOUND && in_array($request->method, [Method::POST, Method::PUT, Method::PATCH], strict: true)) {
54+
if (
55+
$response->status === Status::FOUND &&
56+
in_array($request->method, [Method::POST, Method::PUT, Method::PATCH], strict: true)
57+
) {
5158
// TODO: set status to 303
5259
// return new GenericResponse(
5360
// status: Status::SEE_OTHER,

src/Inertia.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace NeoIsRecursive\Inertia;
66

7-
use NeoIsRecursive\Inertia\InertiaConfig;
87
use NeoIsRecursive\Inertia\Http\InertiaResponse;
8+
use NeoIsRecursive\Inertia\InertiaConfig;
99
use NeoIsRecursive\Inertia\Support\Header;
1010
use Tempest\Container\Container;
1111
use Tempest\Container\Singleton;
@@ -18,13 +18,13 @@
1818
#[Singleton]
1919
final class Inertia
2020
{
21-
2221
public function __construct(
2322
private Container $container,
24-
private InertiaConfig $config
25-
) {}
23+
private InertiaConfig $config,
24+
) {
25+
}
2626

27-
public function share(string|array $key, ?string $value = null): void
27+
public function share(string|array $key, null|string $value = null): void
2828
{
2929
$this->config->share($key, $value);
3030
}
@@ -38,18 +38,14 @@ public function flushShared(): void
3838
get => $this->container->invoke($this->config->versionResolver->resolve(...));
3939
}
4040

41-
4241
public function render(string $page, array $props = []): InertiaResponse
4342
{
4443
return new InertiaResponse(
4544
request: $this->container->get(Request::class),
4645
page: $page,
47-
props: array_merge(
48-
$this->config->sharedProps,
49-
$props
50-
),
46+
props: array_merge($this->config->sharedProps, $props),
5147
rootView: $this->config->rootView,
52-
version: $this->version
48+
version: $this->version,
5349
);
5450
}
5551

@@ -67,10 +63,10 @@ public function location(string|Redirect $url): Response
6763
body: '',
6864
headers: [
6965
Header::LOCATION => $url,
70-
]
66+
],
7167
);
7268
}
7369

74-
return $url instanceof Redirect ? $url : new Redirect($url);
70+
return ($url instanceof Redirect) ? $url : new Redirect($url);
7571
}
7672
}

src/InertiaConfig.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
final class InertiaConfig
1313
{
14-
1514
public function __construct(
16-
readonly public string $rootView,
15+
public readonly string $rootView,
1716
/** @var class-string<InertiaVersionResolver> */
18-
readonly public InertiaVersionResolver $versionResolver = new ManifestVersionResolver(),
17+
public readonly InertiaVersionResolver $versionResolver = new ManifestVersionResolver(),
1918
/** @var array<AlwaysProp|LazyProp|string|array|Closue> */
2019
public private(set) array $sharedProps = [],
21-
) {}
20+
) {
21+
}
2222

2323
public function flushSharedProps(): self
2424
{

src/ManifestVersionResolver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
final readonly class ManifestVersionResolver implements InertiaVersionResolver
1313
{
14-
public function __construct(public ?string $manifestPath = null) {}
14+
public function __construct(
15+
public null|string $manifestPath = null,
16+
) {
17+
}
1518

1619
public function resolve(Container $container): string
1720
{
@@ -21,6 +24,6 @@ public function resolve(Container $container): string
2124
return hash_file('xxh128', $manifestPath);
2225
}
2326

24-
return "";
27+
return '';
2528
}
2629
}

src/Props/AlwaysProp.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ final class AlwaysProp implements MergeableProp
1818

1919
public function __construct(
2020
public readonly MethodReflector|FunctionReflector|string|Closure|array $value,
21-
public private(set) bool $shouldMerge = false
22-
) {}
21+
public private(set) bool $shouldMerge = false,
22+
) {
23+
}
2324

2425
public function __invoke(): mixed
2526
{

src/Props/DeferProp.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ final class DeferProp implements MergeableProp
1919
public function __construct(
2020
public MethodReflector|FunctionReflector|string|array|Closure $callback,
2121
public string $group = 'default',
22-
public private(set) bool $shouldMerge = false
23-
) {}
22+
public private(set) bool $shouldMerge = false,
23+
) {
24+
}
2425

2526
public function __invoke(): mixed
2627
{

src/Props/LazyProp.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ final class LazyProp implements MergeableProp
1818

1919
public function __construct(
2020
public MethodReflector|FunctionReflector|string|array|Closure $callback,
21-
public private(set) bool $shouldMerge = false
22-
) {}
21+
public private(set) bool $shouldMerge = false,
22+
) {
23+
}
2324

2425
public function __invoke(): mixed
2526
{

0 commit comments

Comments
 (0)