Skip to content

Commit 6380117

Browse files
committed
- Added declare(strict-types=1); to ensure that every type is strictly followed.
- Added missing return types so callables will know what will be returned - Reformated code so its more readable with && and || signs. - Re-arranged the simplicity of the || checks. - Removed intval() and did (int) casting. Which performs up to 6 times faster. - Returned void for addLinkHeaders since its private and the return is never used. - For LinkHeaders.php I've removed the null-operator (?). This could cause an error whenever the null was passed. Now it can't throw an error anymore due to the foreach which was a few lines later. - Renamed variable instead of rewriting the variable (bad coding behavior) - Tested everything in the test folder to assure everything complies.
1 parent cf17e82 commit 6380117

9 files changed

+40
-38
lines changed

src/Data/LinkHeaders.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Data;
44

@@ -30,7 +30,7 @@ public function setLinkProvider(EvolvableLinkProviderInterface $linkProvider): s
3030
return $this;
3131
}
3232

33-
public function addLink(EvolvableLinkInterface|string|array $uri, string|array|null $rel = null, ?array $attributes = []): static
33+
public function addLink(EvolvableLinkInterface|string|array $uri, string|array|null $rel = null, array $attributes = []): static
3434
{
3535
if (is_array($uri)) {
3636
foreach ($uri as $data) {
@@ -71,23 +71,23 @@ public function addLink(EvolvableLinkInterface|string|array $uri, string|array|n
7171

7272
public function addFromString(string $link): static
7373
{
74-
$links = explode(',', trim($link));
75-
foreach ($links as $link) {
76-
$parts = explode('; ', trim($link));
74+
$explodedLinks = explode(',', trim($link));
75+
foreach ($explodedLinks as $explodedLink) {
76+
$parts = explode('; ', trim($explodedLink));
7777
$uri = trim(array_shift($parts), '<>');
7878
$rel = null;
7979
$attributes = [];
8080
foreach ($parts as $part) {
8181
preg_match('/(?<key>[^=]+)(?:="?(?<value>.*)"?)?/', trim($part), $matches);
8282
$key = $matches['key'];
83-
$value = $matches['value'] ?? null;
83+
$value = $matches['value'] ?? '1';
8484

8585
if ($key === 'rel') {
8686
$rel = $value;
8787

8888
continue;
8989
}
90-
$attributes[$key] = $value ?? true;
90+
$attributes[$key] = $value;
9191
}
9292

9393
$this->addLink($uri, $rel, $attributes);
@@ -96,14 +96,14 @@ public function addFromString(string $link): static
9696
return $this;
9797
}
9898

99-
public function makeUnique()
99+
public function makeUnique(): static
100100
{
101101
$handledHashes = [];
102102

103103
foreach ($this->getLinkProvider()->getLinks() as $link) {
104104
/** @var Link $link */
105-
$hash = md5($link->getHref(), serialize($link->getRels()));
106-
if (! in_array($hash, $handledHashes)) {
105+
$hash = md5($link->getHref(), true); // Previous the second parameter was: serialize($link->getRels()) which gives a string, where the second parameter excepts a boolean.
106+
if (!in_array($hash, $handledHashes, true)) {
107107
$handledHashes[] = $hash;
108108

109109
continue;
@@ -115,18 +115,18 @@ public function makeUnique()
115115
return $this;
116116
}
117117

118-
public function __toString()
118+
public function __toString(): string
119119
{
120120
return trim(collect($this->getLinkProvider()->getLinks())
121-
->map([static::class, 'linkToString'])
121+
->map([self::class, 'linkToString'])
122122
->filter()
123123
->implode(','));
124124
}
125125

126-
public static function linkToString(LinkInterface $link)
126+
public static function linkToString(LinkInterface $link): ?string
127127
{
128128
if ($link->isTemplated()) {
129-
return;
129+
return null;
130130
}
131131

132132
$attributes = ['', sprintf('rel="%s"', implode(' ', $link->getRels()))];
@@ -140,7 +140,7 @@ public static function linkToString(LinkInterface $link)
140140
continue;
141141
}
142142

143-
if (! \is_bool($value)) {
143+
if (!\is_bool($value)) {
144144
$attributes[] = sprintf('%s="%s"', $key, $value);
145145

146146
continue;

src/Events/GenerateEarlyHints.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Events;
44

src/Listeners/AddDefaultHeaders.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Listeners;
44

@@ -7,14 +7,14 @@
77

88
class AddDefaultHeaders
99
{
10-
public function handle(GenerateEarlyHints $event)
10+
public function handle(GenerateEarlyHints $event): void
1111
{
1212
foreach (config('http3earlyhints.default_headers', []) as $header) {
1313
$event->linkHeaders->addFromString($header);
1414
}
1515
}
1616

17-
public static function register()
17+
public static function register(): void
1818
{
1919
Event::listen(GenerateEarlyHints::class, static::class);
2020
}

src/Listeners/AddFromBody.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Listeners;
44

src/Middleware/AddHttp3EarlyHints.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Middleware;
44

@@ -26,7 +26,10 @@ public function handle(Request $request, Closure $next, ?int $sizeLimit = null):
2626
$lastPath = Str::afterLast($request->path(), '/');
2727
if (
2828
$request->format() !== 'html'
29-
|| (str_contains($lastPath, '.') && ! in_array(Str::afterLast($lastPath, '.'), config('http3earlyhints.extensions', ['', 'php', 'html'])))
29+
|| (
30+
str_contains($lastPath, '.')
31+
&& !in_array(Str::afterLast($lastPath, '.'), config('http3earlyhints.extensions', ['', 'php', 'html']), true)
32+
)
3033
) {
3134
$this->skipCurrentRequest = true;
3235

@@ -84,15 +87,15 @@ public function terminate(Request $request, SymfonyResponse $response): void
8487
$this->handleGeneratingLinkHeaders($request, $response);
8588
}
8689

87-
public function handleGeneratingLinkHeaders(Request $request, SymfonyResponse $response)
90+
public function handleGeneratingLinkHeaders(Request $request, SymfonyResponse $response): ?LinkHeaders
8891
{
8992
if (
90-
! $response instanceof Response
93+
$this->skipCurrentRequest
94+
|| !$response instanceof Response
9195
|| $response->isRedirection()
92-
|| ! $response->isSuccessful()
93-
|| $this->skipCurrentRequest
96+
|| !$response->isSuccessful()
9497
) {
95-
return;
98+
return null;
9699
}
97100
$linkHeaders = $this->generateLinkHeaders($request, $response, $this->sizeLimit);
98101

@@ -112,7 +115,7 @@ protected function generateLinkHeaders(Request $request, SymfonyResponse $respon
112115

113116
$this->linkHeaders->makeUnique();
114117

115-
$sizeLimit = $sizeLimit ?? max(1, intval(config('http3earlyhints.size_limit', 32 * 1024)));
118+
$sizeLimit = $sizeLimit ?? max(1, (int)config('http3earlyhints.size_limit', 32 * 1024));
116119
$headersText = $this->linkHeaders->__toString();
117120

118121
while (strlen($headersText) > $sizeLimit) {
@@ -126,18 +129,17 @@ protected function generateLinkHeaders(Request $request, SymfonyResponse $respon
126129
/**
127130
* Add Link Header
128131
*/
129-
private function addLinkHeaders(SymfonyResponse $response, LinkHeaders $linkHeaders): SymfonyResponse
132+
private function addLinkHeaders(SymfonyResponse $response, LinkHeaders $linkHeaders): void
130133
{
131134
$link = $linkHeaders->__toString();
132-
if (! $link || ! $response instanceof Response) {
133-
return $response;
135+
if (!$link || !$response instanceof Response) {
136+
return;
134137
}
138+
135139
if ($response->headers->get('Link')) {
136140
$link = $response->headers->get('Link').','.$link;
137141
}
138142

139143
$response->header('Link', $link);
140-
141-
return $response;
142144
}
143145
}

src/ServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints;
44

@@ -13,7 +13,7 @@ class ServiceProvider extends LaravelServiceProvider
1313
*
1414
* @return void
1515
*/
16-
public function boot()
16+
public function boot(): void
1717
{
1818
$this->mergeConfigFrom(__DIR__.'/config.php', 'http3earlyhints');
1919

src/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
return [
44
/**

tests/AddHttp3EarlyHintsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Tests;
44

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace JustBetter\Http3EarlyHints\Tests;
44

0 commit comments

Comments
 (0)