Skip to content

Commit 3d20af3

Browse files
author
thomaskeyte
committed
Install & run phpcs
1 parent 402aaf0 commit 3d20af3

13 files changed

+260
-103
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"require-dev": {
2626
"orchestra/testbench": "^4.0",
27-
"phpunit/phpunit": "^9.0"
27+
"phpunit/phpunit": "^9.0",
28+
"squizlabs/php_codesniffer": "3.*"
2829
},
2930
"autoload": {
3031
"psr-4": {

composer.lock

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

phpcs.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<description>PHPCS configuration file.</description>
4+
<rule ref="PSR2"/>
5+
6+
<file>src</file>
7+
<file>tests</file>
8+
9+
<exclude-pattern>*/vendor/*</exclude-pattern>
10+
11+
<arg name="report" value="summary"/>
12+
<arg name="colors"/>
13+
<arg value="p"/>
14+
</ruleset>

src/Http2Push.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct()
2424
/**
2525
* Add a resource to the push stack
2626
*
27-
* @param string $resource
27+
* @param string $resource
2828
* @param int|null $expires
2929
*/
3030
public function add($resource, $expires = null): void
@@ -41,14 +41,16 @@ public function add($resource, $expires = null): void
4141
private function addPermanents()
4242
{
4343
collect(config('http2push.always'))
44-
->each(function ($resource) {
45-
if (is_string($resource)) {
46-
$this->add($resource);
44+
->each(
45+
function ($resource) {
46+
if (is_string($resource)) {
47+
$this->add($resource);
48+
}
49+
if (is_array($resource) && isset($resource['src'])) {
50+
$this->add($resource['src'], $resource['expires'] ?? null);
51+
}
4752
}
48-
if (is_array($resource) && isset($resource['src'])) {
49-
$this->add($resource['src'], $resource['expires'] ?? null);
50-
}
51-
});
53+
);
5254
}
5355

5456
/**
@@ -59,30 +61,37 @@ private function addPermanents()
5961
public function buildLinkHeader(): string
6062
{
6163
return $this->toPush()
62-
->map(function ($resource) {
63-
$src = $resource->getSrc();
64-
$attrs = $resource->getAttrString();
65-
return sprintf("<%s>; rel=preload; %s", $src, $attrs);
66-
})
67-
->map(function ($resource) {
68-
return rtrim($resource, ";");
69-
})
64+
->map(
65+
function ($resource) {
66+
$src = $resource->getSrc();
67+
$attrs = $resource->getAttrString();
68+
return sprintf("<%s>; rel=preload; %s", $src, $attrs);
69+
}
70+
)
71+
->map(
72+
function ($resource) {
73+
return rtrim($resource, ";");
74+
}
75+
)
7076
->implode(', ');
7177
}
7278

7379
/**
7480
* Filter out any non unique, or those that have already been pushed
75-
*
7681
*/
7782
private function toPush(): \Illuminate\Support\Collection
7883
{
7984
return $this->resources
80-
->unique(function ($resource) {
81-
return $resource->getSrc();
82-
})
83-
->filter(function ($resource) {
84-
return !$resource->wasPushed();
85-
});
85+
->unique(
86+
function ($resource) {
87+
return $resource->getSrc();
88+
}
89+
)
90+
->filter(
91+
function ($resource) {
92+
return !$resource->wasPushed();
93+
}
94+
);
8695
}
8796

8897
/**
@@ -93,13 +102,15 @@ private function toPush(): \Illuminate\Support\Collection
93102
public function setPushCookies($response): void
94103
{
95104
$this->toPush()
96-
->each(function ($resource) use (&$response) {
97-
if ($resource->getCookieExpires() !== false) {
98-
$response->withCookie(
99-
(new PushCookie($resource->getSrc(), $resource->getCookieExpires()))->makeCookie()
100-
);
105+
->each(
106+
function ($resource) use (&$response) {
107+
if ($resource->getCookieExpires() !== false) {
108+
$response->withCookie(
109+
(new PushCookie($resource->getSrc(), $resource->getCookieExpires()))->makeCookie()
110+
);
111+
}
101112
}
102-
});
113+
);
103114
}
104115

105116
/**

src/Http2PushServiceProvider.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public function boot(): void
1717

1818
$this->registerBladeDirective();
1919
if ($this->app->runningInConsole()) {
20-
$this->publishes([
20+
$this->publishes(
21+
[
2122
__DIR__ . '/config/config.php' => config_path('http2push.php'),
22-
], 'config');
23+
],
24+
'config'
25+
);
2326
}
2427
}
2528

@@ -36,9 +39,12 @@ private function addMiddleware(): void
3639
*/
3740
private function registerBladeDirective(): void
3841
{
39-
Blade::directive('h2push', function ($arguments) {
40-
return "<?php echo h2push({$arguments}); ?>";
41-
});
42+
Blade::directive(
43+
'h2push',
44+
function ($arguments) {
45+
return "<?php echo h2push({$arguments}); ?>";
46+
}
47+
);
4248
}
4349

4450
/**
@@ -50,8 +56,11 @@ public function register(): void
5056
$this->mergeConfigFrom(__DIR__ . '/config/config.php', 'http2push');
5157

5258
// Register the main class to use with the facade
53-
$this->app->singleton('http2push', function () {
54-
return new Http2Push;
55-
});
59+
$this->app->singleton(
60+
'http2push',
61+
function () {
62+
return new Http2Push;
63+
}
64+
);
5665
}
5766
}

src/InferType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function infer($resource): ?string
4949
*/
5050
private static function getExtension($resource): string
5151
{
52-
$resource = strtok($resource, '?'); # Trim query parameters
52+
$resource = strtok($resource, '?'); // Trim query parameters
5353
$ext = strrchr($resource, '.');
5454
$trimmed = ltrim($ext, ".");
5555
return strtolower($trimmed);

src/Middleware/AddLinkHeader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class AddLinkHeader
99
/**
1010
* Handle an incoming request.
1111
*
12-
* @param \Illuminate\Http\Request $request
13-
* @param \Closure $next
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
1414
*
1515
* @return \Illuminate\Http\Response
1616
*/

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Helper function to add a resource to the push stack
55
*
6-
* @param string $resource
6+
* @param string $resource
77
* @param int|null $expires
88
*/
99
function h2push($resource, $expires = null)

tests/ConfigTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ protected function getPackageProviders($app)
1616
return [Http2PushServiceProvider::class];
1717
}
1818

19-
/** @test */
19+
/**
20+
* @test
21+
*/
2022
public function it_respects_cookie_prefix()
2123
{
2224
$prefix = 'test_prefix_';
@@ -25,7 +27,9 @@ public function it_respects_cookie_prefix()
2527
$this->assertStringStartsWith($prefix, (new PushCookie('app.js'))->getName());
2628
}
2729

28-
/** @test */
30+
/**
31+
* @test
32+
*/
2933
public function it_respects_global_cookie_expiry()
3034
{
3135
$expiry = 27;
@@ -34,7 +38,9 @@ public function it_respects_global_cookie_expiry()
3438
$this->assertEquals($expiry, (new PushCookie('/app.js'))->getExpires());
3539
}
3640

37-
/** @test */
41+
/**
42+
* @test
43+
*/
3844
public function it_respects_type_specific_expiry()
3945
{
4046
$script_expiry = 17;

0 commit comments

Comments
 (0)