Skip to content

Commit c1dec3d

Browse files
refactor!: tighten types, use tempest json\encode and update mago to
1.0.0-beta.24
1 parent 9fdca05 commit c1dec3d

File tree

14 files changed

+303
-144
lines changed

14 files changed

+303
-144
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"require-dev": {
4141
"phpunit/phpunit": "^11.5.15",
42-
"carthage-software/mago": "1.0.0-beta.16"
42+
"carthage-software/mago": "1.0.0-beta.24"
4343
},
4444
"minimum-stability": "dev",
4545
"config": {

composer.lock

Lines changed: 251 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mago.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ null-type-hint = "Question"
2222
integrations = ["phpunit"]
2323

2424
[linter.rules]
25+
yoda-conditions = { enabled = false }
2526
ambiguous-function-call = { enabled = false }
2627
literal-named-argument = { enabled = false }
2728
halstead = { effort-threshold = 7000 }

src/Http/InertiaResponse.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static function composeProps(array $props, Request $request, string $com
8484
$always = static::resolveAlwaysProps($props);
8585
$partial = static::resolvePartialProps($request, $component, $props);
8686

87-
return static::evaluateProps(array_merge($always, $partial), $request, unpackDotProps: true);
87+
return static::evaluateProps(array_merge($always, $partial), unpackDotProps: true);
8888
}
8989

9090
/**
@@ -131,12 +131,12 @@ private static function resolvePropKeysThatShouldDefer(array $props, Request $re
131131

132132
$propKeysToMerge = arr($props)
133133
->filter(static fn($prop) => $prop instanceof DeferProp)
134-
->map(static fn(DeferProp $prop, string $key) => [
134+
->map(static fn(DeferProp $prop, string|int $key) => [
135135
'group' => $prop->group,
136136
'key' => $key,
137137
])
138-
->groupBy(static fn(array $prop) => $prop['group'])
139-
->map(static fn(array $group) => arr($group)->pluck(value: 'key')->toArray());
138+
->groupBy(static fn($prop) => $prop['group'])
139+
->map(static fn($group) => arr($group)->pluck(value: 'key')->toArray());
140140

141141
return $propKeysToMerge->isEmpty() ? null : $propKeysToMerge->toArray();
142142
}
@@ -158,22 +158,22 @@ private static function resolvePropKeysThatShouldMerge(array $props, Request $re
158158

159159
/**
160160
* Evaluates props recursively.
161+
* @param array<mixed> $props
161162
* @pure
162163
* @mago-expect lint:no-boolean-flag-parameter
164+
* @mago-expect analysis:mixed-assignment
163165
*/
164-
private static function evaluateProps(array $props, Request $request, bool $unpackDotProps = true): array
166+
private static function evaluateProps(array $props, bool $unpackDotProps = true): array
165167
{
166-
return arr($props)->map(function ($value, string|int $key) use ($request): array {
168+
return arr($props)->map(static function (mixed $value, string|int $key): array {
167169
$evaluated = $value instanceof Closure ? invoke($value) : $value;
168170
$evaluated = $evaluated instanceof CallableProp ? $evaluated() : $evaluated;
169171
$evaluated = $evaluated instanceof ArrayInterface ? $evaluated->toArray() : $evaluated;
170-
$evaluated = is_array($evaluated)
171-
? self::evaluateProps($evaluated, $request, unpackDotProps: false)
172-
: $evaluated;
172+
$evaluated = is_array($evaluated) ? static::evaluateProps($evaluated, unpackDotProps: false) : $evaluated;
173173

174174
return [$key, $evaluated];
175175
})->reduce(
176-
function (array $acc, array $item) use ($unpackDotProps): array {
176+
static function (array $acc, array $item) use ($unpackDotProps): array {
177177
/** @var string|int $key */
178178
[$key, $value] = $item;
179179
if ($unpackDotProps && is_string($key) && str_contains($key, '.')) {

src/Inertia.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
namespace NeoIsRecursive\Inertia;
66

7-
use Closure;
7+
use Exception;
88
use NeoIsRecursive\Inertia\Http\InertiaResponse;
99
use NeoIsRecursive\Inertia\InertiaConfig;
10-
use NeoIsRecursive\Inertia\Props\AlwaysProp;
11-
use NeoIsRecursive\Inertia\Props\DeferProp;
12-
use NeoIsRecursive\Inertia\Props\LazyProp;
1310
use NeoIsRecursive\Inertia\Support\Header;
1411
use Tempest\Container\Container;
1512
use Tempest\Container\Singleton;
@@ -29,10 +26,11 @@ public function __construct(
2926
private InertiaConfig $config,
3027
) {}
3128

32-
public function share(
33-
string|array $key,
34-
LazyProp|AlwaysProp|DeferProp|Closure|string|array|null $value = null,
35-
): self {
29+
/**
30+
* @param string|array<string, mixed> $key
31+
*/
32+
public function share(string|array $key, mixed $value = null): self
33+
{
3634
$this->config->share($key, $value);
3735

3836
return $this;
@@ -49,10 +47,20 @@ public function flushShared(): self
4947
get => $this->container->invoke($this->config->versionResolver->resolve(...));
5048
}
5149

50+
/**
51+
* @param array<string, mixed> $props
52+
* @mago-expect analyzer:unhandled-thrown-type
53+
*/
5254
public function render(string $component, array $props = []): InertiaResponse
5355
{
56+
$request = $this->container->get(Request::class);
57+
58+
if (!$request) {
59+
throw new Exception('No request');
60+
}
61+
5462
return new InertiaResponse(
55-
request: $this->container->get(Request::class),
63+
request: $request,
5664
component: $component,
5765
props: array_merge($this->config->sharedProps, $props),
5866
rootView: $this->config->rootView,
@@ -90,12 +98,13 @@ public function clearHistory(): self
9098

9199
public function location(string|Redirect $url): Response
92100
{
93-
$isInertiaRequest = $this->container->get(Request::class)->headers->has(Header::INERTIA);
101+
/** @var bool */
102+
$isInertiaRequest = $this->container->get(Request::class)?->headers?->has(Header::INERTIA) ?? false;
94103

95104
if ($isInertiaRequest) {
96105
if ($url instanceof Redirect) {
97106
/** @var string */
98-
$url = $url->getHeader(name: 'Location')->values[0];
107+
$url = $url->getHeader(name: 'Location')->values[0] ?? '/';
99108
}
100109

101110
return new GenericResponse(

src/InertiaConfig.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44

55
namespace NeoIsRecursive\Inertia;
66

7-
use Closure;
87
use NeoIsRecursive\Inertia\Contracts\InertiaVersionResolver;
9-
use NeoIsRecursive\Inertia\Props\AlwaysProp;
10-
use NeoIsRecursive\Inertia\Props\DeferProp;
11-
use NeoIsRecursive\Inertia\Props\LazyProp;
128

139
final class InertiaConfig
1410
{
11+
/**
12+
* @param array<string, mixed> $sharedProps
13+
*/
1514
public function __construct(
1615
public readonly string $rootView,
1716
public readonly InertiaVersionResolver $versionResolver = new ManifestVersionResolver(),
18-
/** @var array<AlwaysProp|LazyProp|DeferProp|Closure|string|array> */
1917
public private(set) array $sharedProps = [],
2018
) {}
2119

@@ -26,7 +24,11 @@ public function flushSharedProps(): self
2624
return $this;
2725
}
2826

29-
public function share(string|array $key, LazyProp|AlwaysProp|DeferProp|Closure|string|array|null $value): self
27+
/**
28+
* @param (string|array<string, mixed>) $key
29+
* @param ($key is string ? mixed : null) $value
30+
*/
31+
public function share(string|array $key, mixed $value = null): self
3032
{
3133
if (is_array($key)) {
3234
$this->sharedProps = array_merge($this->sharedProps, $key);

src/Props/DeferProp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class DeferProp implements CallableProp, MergeableProp
1919
use IsMergeableProp;
2020

2121
public function __construct(
22-
public MethodReflector|FunctionReflector|string|array|Closure $callback,
22+
public MethodReflector|FunctionReflector|Closure $callback,
2323
public string $group = 'default',
2424
public private(set) bool $shouldMerge = false,
2525
) {}

src/Props/LazyProp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class LazyProp implements CallableProp, MergeableProp
1919
use IsMergeableProp;
2020

2121
public function __construct(
22-
public MethodReflector|FunctionReflector|string|array|Closure $callback,
22+
public MethodReflector|FunctionReflector|Closure $callback,
2323
public private(set) bool $shouldMerge = false,
2424
) {}
2525

src/Support/ResolveErrorProps.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function resolve(): array
2424

2525
return arr($failingRules)->map(
2626
fn(array $rules): array => arr($rules)->map(
27+
// @mago-expect lint:prefer-first-class-callable
2728
fn(Rule $rule): string => $this->validator->getErrorMessage($rule),
2829
)->toArray(),
2930
)->toArray();

src/Views/Components/x-inertia.view.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
/**
66
* @var NeoIsRecursive\Inertia\Views\InertiaBaseView $this
7-
* @var NeoIsRecursive\Inertia\PageData $page
7+
* @var NeoIsRecursive\Inertia\PageData|null $page
88
* @var string|null $id
99
*/
1010

1111
?>
1212

13-
<div id="<?= $id ?? 'app'; ?>" data-page="<?= htmlentities(json_encode($page ?? $this->page)); ?>"></div>
13+
<div id="<?= $id ?? 'app'; ?>" data-page="<?= htmlentities(\Tempest\Support\Json\encode($page ?? $this->page)) ?>"></div>

0 commit comments

Comments
 (0)