Skip to content

Commit ae49fe0

Browse files
authored
Merge pull request #9 from alleyinteractive/feature/terminate-request
Add phpstan, use terminate request if found
2 parents e99d2e3 + af1fba6 commit ae49fe0

File tree

8 files changed

+109
-30
lines changed

8 files changed

+109
-30
lines changed

.gitattributes

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
# Via WPCS.
77
#
88
/.github export-ignore
9-
/phpcs.xml export-ignore
109
/.phpcs export-ignore
11-
/phpunit.xml export-ignore
12-
/tests export-ignore
1310
/configure.php export-ignore
1411
/Makefile export-ignore
12+
/phpcs.xml export-ignore
13+
/phpstan.neon export-ignore
14+
/phpunit.xml export-ignore
15+
/tests export-ignore
1516

1617
#
1718
# Auto detect text files and perform LF normalization.

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `Create PHP Package` will be documented in this file.
44

5+
## v1.1.1
6+
7+
- Added PHPStan testing.
8+
- Use Mantle's [`terminate_request`](https://mantle.alley.com/docs/features/support/helpers#terminate_request) helper to terminate requests if available.
9+
510
## v1.0.0 - 2023-02-07
611

712
- Initial release of standalone package.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
},
2121
"require-dev": {
2222
"alleyinteractive/alley-coding-standards": "^2.0",
23-
"mantle-framework/testkit": "^1.0"
23+
"mantle-framework/testkit": "^1.8",
24+
"szepeviktor/phpstan-wordpress": "^2.0"
2425
},
2526
"minimum-stability": "dev",
2627
"prefer-stable": true,
@@ -52,9 +53,11 @@
5253
"scripts": {
5354
"phpcbf": "phpcbf .",
5455
"phpcs": "phpcs .",
56+
"phpstan": "phpstan --memory-limit=512M",
5557
"phpunit": "phpunit",
5658
"test": [
5759
"@phpcs",
60+
"@phpstan",
5861
"@phpunit"
5962
]
6063
}

phpstan.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/szepeviktor/phpstan-wordpress/extension.neon
3+
4+
parameters:
5+
# Level 9 is the highest level
6+
level: max
7+
8+
paths:
9+
- src/

src/class-path-dispatch.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,53 @@
1010

1111
use WP_Query;
1212

13+
use function Mantle\Support\Helpers\terminate_request;
14+
1315
/**
1416
* Path Dispatch
17+
*
18+
* @phpstan-type PathArgs array{
19+
* path?: string,
20+
* callback?: callable,
21+
* action?: string,
22+
* rewrite?: array{
23+
* rule: string,
24+
* redirect?: string,
25+
* position?: 'top'|'bottom',
26+
* query_vars?: string|array<string>
27+
* },
28+
* template?: string
29+
* }
1530
*/
1631
class Path_Dispatch {
1732

1833
/**
1934
* Query vars that should be allowed.
2035
*
21-
* @var array
36+
* @var string[]
2237
*/
23-
public $qv = [ 'dispatch' ];
38+
public array $qv = [ 'dispatch' ];
2439

2540
/**
2641
* Array of basic paths.
2742
*
28-
* @var array
43+
* @var array<string, PathArgs>
2944
*/
30-
public $basic_paths = [];
45+
public array $basic_paths = [];
3146

3247
/**
3348
* Array of rewrite paths.
3449
*
35-
* @var array
50+
* @var array<string, PathArgs>
3651
*/
37-
public $rewrite_paths = [];
52+
public array $rewrite_paths = [];
3853

3954
/**
4055
* Instance of this class.
4156
*
42-
* @var Path_Dispatch
57+
* @var Path_Dispatch|null
4358
*/
44-
private static $instance;
59+
private static ?Path_Dispatch $instance;
4560

4661
/**
4762
* Don't allow __clone.
@@ -62,17 +77,18 @@ public function __wakeup() {
6277
*
6378
* @return Path_Dispatch
6479
*/
65-
public static function instance() {
80+
public static function instance(): Path_Dispatch {
6681
if ( ! isset( self::$instance ) ) {
6782
self::$instance = new Path_Dispatch();
6883
}
84+
6985
return self::$instance;
7086
}
7187

7288
/**
7389
* Clear the instance of this class.
7490
*/
75-
public static function clear_instance() {
91+
public static function clear_instance(): void {
7692
self::$instance = null;
7793
}
7894

@@ -114,8 +130,9 @@ protected function __construct() {
114130
* @see http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
115131
* }
116132
* }
133+
* @phpstan-param PathArgs|string $args
117134
*/
118-
public function add_path( $args = [] ) {
135+
public function add_path( array|string $args = [] ): void {
119136
if ( is_string( $args ) && ! empty( $args ) ) {
120137
$args = [
121138
'path' => $args,
@@ -143,11 +160,12 @@ public function add_path( $args = [] ) {
143160
/**
144161
* Add multiple paths in one call.
145162
*
146-
* @see Path_Dispatch::add_path
163+
* @see Path_Dispatch::add_path()
147164
*
148165
* @param array $paths An array of arrays that would be passed to add_path.
166+
* @phpstan-param array<PathArgs> $paths
149167
*/
150-
public function add_paths( $paths ) {
168+
public function add_paths( array $paths ): void {
151169
foreach ( $paths as $path ) {
152170
$this->add_path( $path );
153171
}
@@ -156,17 +174,17 @@ public function add_paths( $paths ) {
156174
/**
157175
* Add the class query var "dispatch" as well as any others added through add_path.
158176
*
159-
* @param array $qv The current query vars.
160-
* @return array The modified query vars.
177+
* @param string[] $qv The current query vars.
178+
* @return string[] The modified query vars.
161179
*/
162-
public function add_query_var( $qv ) {
180+
public function add_query_var( array $qv ): array {
163181
return array_merge( $qv, $this->qv );
164182
}
165183

166184
/**
167185
* Add rewrite rules for our dispatched paths.
168186
*/
169-
public function add_rewrite_rules() {
187+
public function add_rewrite_rules(): void {
170188
if ( ! empty( $this->basic_paths ) ) {
171189
$slugs = array_map( 'preg_quote', array_keys( $this->basic_paths ) );
172190
$slugs = implode( '|', $slugs );
@@ -194,9 +212,9 @@ public function add_rewrite_rules() {
194212
*
195213
* @param WP_Query $query The WP_Query instance.
196214
*/
197-
public function dispatch_path( &$query ) {
215+
public function dispatch_path( &$query ): void {
198216
$path = get_query_var( 'dispatch' );
199-
if ( $query->is_main_query() && $path ) {
217+
if ( $query->is_main_query() && $path && is_string( $path ) ) {
200218
$args = [];
201219

202220
if ( ! empty( $this->basic_paths[ $path ] ) ) {
@@ -213,8 +231,23 @@ public function dispatch_path( &$query ) {
213231

214232
if ( ! empty( $args['template'] ) ) {
215233
get_template_part( 'dispatch', $args['template'] );
216-
exit;
234+
235+
$this->terminate_request();
217236
}
218237
}
219238
}
239+
240+
/**
241+
* Terminate the request.
242+
*
243+
* @param int $exit_status Exit status code.
244+
* @return never
245+
*/
246+
protected function terminate_request( int $exit_status = 0 ): never {
247+
if ( function_exists( 'Mantle\Support\Helpers\terminate_request' ) ) {
248+
\Mantle\Support\Helpers\terminate_request( $exit_status );
249+
}
250+
251+
exit( (int) $exit_status );
252+
}
220253
}

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
*
1515
* @return Path_Dispatch
1616
*/
17-
function Path_Dispatch() {
17+
function Path_Dispatch(): Path_Dispatch {
1818
return Path_Dispatch::instance();
1919
}

tests/Feature/PathDispatchTest.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use WP_Path_Dispatch\Path_Dispatch;
55
use WP_Path_Dispatch\Tests\TestCase;
66

7+
use function Mantle\Support\Helpers\terminate_request;
78
use function WP_Path_Dispatch\Path_Dispatch;
89

910
/**
@@ -29,6 +30,7 @@ public function test_add_simple_path() {
2930
'path' => 'some-path',
3031
'callback' => function () {
3132
echo 'some-response';
33+
terminate_request();
3234
},
3335
]
3436
);
@@ -37,7 +39,7 @@ public function test_add_simple_path() {
3739

3840
$this->get( '/some-path/' )
3941
->assertStatus( 200 )
40-
->assertSee( 'some-response' );
42+
->assertContent( 'some-response' );
4143
}
4244

4345
public function test_add_multiple_paths() {
@@ -47,12 +49,14 @@ public function test_add_multiple_paths() {
4749
'path' => 'some-path',
4850
'callback' => function () {
4951
echo 'some-response';
52+
terminate_request();
5053
},
5154
],
5255
[
5356
'path' => 'some-other-path',
5457
'callback' => function () {
5558
echo 'some-other-response';
59+
terminate_request();
5660
},
5761
],
5862
]
@@ -62,11 +66,11 @@ public function test_add_multiple_paths() {
6266

6367
$this->get( '/some-path/' )
6468
->assertStatus( 200 )
65-
->assertSee( 'some-response' );
69+
->assertContent( 'some-response' );
6670

6771
$this->get( '/some-other-path/' )
6872
->assertStatus( 200 )
69-
->assertSee( 'some-other-response' );
73+
->assertContent( 'some-other-response' );
7074
}
7175

7276
public function test_action_path() {
@@ -95,6 +99,7 @@ public function test_custom_rewrite_path() {
9599
],
96100
'callback' => function () {
97101
echo 'some-response: ' . get_query_var( 'some_query_var' );
102+
terminate_request();
98103
},
99104
]
100105
);
@@ -103,7 +108,30 @@ public function test_custom_rewrite_path() {
103108

104109
$this->get( '/example/foo/' )
105110
->assertOk()
106-
->assertSee( 'some-response: foo' );
111+
->assertContent( 'some-response: foo' );
112+
}
113+
114+
public function test_template_path(): void {
115+
// Fake the template part being loaded.
116+
add_action(
117+
'get_template_part',
118+
function (): void {
119+
echo 'This is a template path response.';
120+
}
121+
);
122+
123+
Path_Dispatch()->add_path(
124+
[
125+
'path' => 'some-path',
126+
'template' => 'some-template',
127+
]
128+
);
129+
130+
$this->register_rules();
131+
132+
$this->get( '/some-path/' )
133+
->assertOk()
134+
->assertContent( 'This is a template path response.' );
107135
}
108136

109137
protected function register_rules() {

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace WP_Path_Dispatch\Tests;
33

4-
use Mantle\Testkit\Test_Case as TestkitTest_Case;
4+
use Mantle\Testkit\TestCase as TestkitTest_Case;
55

66
/**
77
* WP Path Dispatch Base Test Case

0 commit comments

Comments
 (0)