Skip to content

Commit df83f30

Browse files
authored
Merge pull request #83 from wp-cli/add/phpstan
2 parents 771778e + f9bc181 commit df83f30

7 files changed

Lines changed: 59 additions & 168 deletions

File tree

‎composer.json‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
},
1616
"require-dev": {
1717
"wp-cli/entity-command": "^1.3 || ^2",
18-
"wp-cli/wp-cli-tests": "^4"
18+
"wp-cli/wp-cli-tests": "^5"
1919
},
2020
"config": {
2121
"process-timeout": 7200,
2222
"sort-packages": true,
2323
"allow-plugins": {
2424
"dealerdirect/phpcodesniffer-composer-installer": true,
25-
"johnpbloch/wordpress-core-installer": true
25+
"johnpbloch/wordpress-core-installer": true,
26+
"phpstan/extension-installer": true
2627
},
2728
"lock": false
2829
},
@@ -60,12 +61,14 @@
6061
"behat-rerun": "rerun-behat-tests",
6162
"lint": "run-linter-tests",
6263
"phpcs": "run-phpcs-tests",
64+
"phpstan": "run-phpstan-tests",
6365
"phpcbf": "run-phpcbf-cleanup",
6466
"phpunit": "run-php-unit-tests",
6567
"prepare-tests": "install-package-tests",
6668
"test": [
6769
"@lint",
6870
"@phpcs",
71+
"@phpstan",
6972
"@phpunit",
7073
"@behat"
7174
]

‎features/fetch.feature‎

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,6 @@ Feature: Manage oEmbed fetch.
9090
<a
9191
"""
9292

93-
# `wp_filter_oembed_result` filter introduced WP 4.4 which sanitizes oEmbed responses that don't include an iframe.
94-
@less-than-wp-4.9 @require-wp-4.4
95-
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
96-
When I try `wp embed fetch https://view.ceros.com/ceros/new-experience-3/p/1`
97-
Then the return code should be 1
98-
And STDERR should be:
99-
"""
100-
Error: There was an error fetching the oEmbed data.
101-
"""
102-
And STDOUT should be empty
103-
104-
# No sanitization prior to WP 4.4.
105-
@less-than-wp-4.4 @require-wp-4.0
106-
Scenario: Get HTML embed code for a given URL that requires discovery and is sanitized
107-
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
108-
When I try `wp embed fetch https://view.ceros.com/ceros/new-experience-3/p/1`
109-
Then the return code should be 0
110-
And STDERR should not contain:
111-
"""
112-
Error:
113-
"""
114-
And STDOUT should contain:
115-
"""
116-
ceros.com/
117-
"""
118-
11993
@require-wp-4.0
12094
Scenario: Get raw oEmbed data for a given URL
12195
When I run `wp embed fetch https://www.youtube.com/watch?v=dQw4w9WgXcQ --raw`
@@ -192,18 +166,6 @@ Feature: Manage oEmbed fetch.
192166
<a href="https://foo.example.com">https://foo.example.com</a>
193167
"""
194168

195-
# WP prior to 4.9 does not return clickable link.
196-
@less-than-wp-4.9 @require-wp-4.0
197-
Scenario: Doesn't make unknown URLs clickable
198-
When I try `wp embed fetch https://foo.example.com`
199-
Then the return code should be 1
200-
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "contain" to cater for these.
201-
And STDERR should contain:
202-
"""
203-
Error: There was an error fetching the oEmbed data.
204-
"""
205-
And STDOUT should be empty
206-
207169
@require-wp-4.0
208170
Scenario: Caches oEmbed response data for a given post
209171
# Note need post author for 'unfiltered_html' check to work for WP < 4.4.
@@ -410,23 +372,6 @@ Feature: Manage oEmbed fetch.
410372
<video
411373
"""
412374

413-
# `wp_embed_handler_googlevideo` handler deprecated WP 4.6.
414-
@less-than-wp-4.6 @require-wp-4.0
415-
Scenario: Invoke built-in Google Video handler
416-
When I run `wp post create --post_title="Foo Bar" --porcelain`
417-
Then STDOUT should be a number
418-
And save STDOUT as {POST_ID}
419-
420-
When I run `wp embed fetch http://video.google.com/videoplay?docid=123456789 --post-id={POST_ID}`
421-
Then STDOUT should contain:
422-
"""
423-
video.google.com
424-
"""
425-
And STDOUT should contain:
426-
"""
427-
<embed
428-
"""
429-
430375
@require-wp-4.0
431376
Scenario: Incompatible options
432377
When I try `wp embed fetch https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --limit-response-size=50000`

‎phpstan.neon.dist‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- embed-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
scanFiles:
9+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
treatPhpDocTypesAsCertain: false
11+
ignoreErrors:
12+
- identifier: missingType.iterableValue
13+
- identifier: missingType.property
14+
- identifier: missingType.parameter
15+
- identifier: missingType.return

‎src/Cache_Command.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ function ( $v ) {
100100
* 123
101101
*/
102102
public function find( $args, $assoc_args ) {
103-
if ( Utils\wp_version_compare( '4.9', '<' ) ) {
104-
WP_CLI::error( 'Requires WordPress 4.9 or greater.' );
105-
}
106-
107103
/** @var \WP_Embed $wp_embed */
108104
global $wp_embed;
109105

@@ -139,7 +135,7 @@ public function find( $args, $assoc_args ) {
139135
$cached_post_id = $wp_embed->find_oembed_post_id( $key_suffix );
140136

141137
if ( $cached_post_id ) {
142-
WP_CLI::line( $cached_post_id );
138+
WP_CLI::line( (string) $cached_post_id );
143139

144140
return;
145141
}

‎src/Fetch_Command.php‎

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,35 @@ public function __invoke( $args, $assoc_args ) {
6969
/** @var \WP_Embed $wp_embed */
7070
global $wp_embed;
7171

72-
$url = $args[0];
73-
$raw = Utils\get_flag_value( $assoc_args, 'raw' );
74-
$raw_format = Utils\get_flag_value( $assoc_args, 'raw-format' );
72+
$url = $args[0];
73+
$raw = Utils\get_flag_value( $assoc_args, 'raw' );
74+
$raw_format = Utils\get_flag_value( $assoc_args, 'raw-format' );
75+
76+
/**
77+
* @var string|null $post_id
78+
*/
7579
$post_id = Utils\get_flag_value( $assoc_args, 'post-id' );
7680
$discover = Utils\get_flag_value( $assoc_args, 'discover' );
7781
$response_size_limit = Utils\get_flag_value( $assoc_args, 'limit-response-size' );
78-
$width = Utils\get_flag_value( $assoc_args, 'width' );
79-
$height = Utils\get_flag_value( $assoc_args, 'height' );
82+
83+
/**
84+
* @var string $width
85+
*/
86+
$width = Utils\get_flag_value( $assoc_args, 'width' );
87+
88+
/**
89+
* @var string $height
90+
*/
91+
$height = Utils\get_flag_value( $assoc_args, 'height' );
8092

8193
// The `$key_suffix` used for caching is part based on serializing the attributes array without normalizing it first so need to try to replicate that.
8294
$oembed_args = array();
8395

8496
if ( null !== $width ) {
85-
$oembed_args['width'] = $width; // Keep as string as if from a shortcode attribute.
97+
$oembed_args['width'] = (int) $width;
8698
}
8799
if ( null !== $height ) {
88-
$oembed_args['height'] = $height; // Keep as string as if from a shortcode attribute.
100+
$oembed_args['height'] = (int) $height;
89101
}
90102
if ( null !== $discover ) {
91103
$oembed_args['discover'] = $discover ? '1' : '0'; // Make it a string as if from a shortcode attribute.
@@ -102,10 +114,6 @@ public function __invoke( $args, $assoc_args ) {
102114
}
103115

104116
if ( $response_size_limit ) {
105-
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
106-
WP_CLI::warning( "The 'limit-response-size' option only works for WordPress 4.0 onwards." );
107-
// Fall through anyway...
108-
}
109117
add_filter(
110118
'oembed_remote_get_args',
111119
function ( $args ) use ( $response_size_limit ) {
@@ -119,7 +127,7 @@ function ( $args ) use ( $response_size_limit ) {
119127

120128
// If raw, query providers directly, by-passing cache.
121129
if ( $raw ) {
122-
$oembed = new oEmbed(); // Needs to be here to make sure `_wp_oembed_get_object()` defined (in "wp-includes/class-oembed.php" for WP < 4.7).
130+
$oembed = new \WP_oEmbed();
123131

124132
$oembed_args['discover'] = $discover;
125133

@@ -160,27 +168,27 @@ function ( $ret, $data, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunc
160168
if ( ! class_exists( 'SimpleXMLElement' ) ) {
161169
WP_CLI::error( "The PHP extension 'SimpleXMLElement' is not available but is required for XML-formatted output." );
162170
}
163-
WP_CLI::log( $this->oembed_create_xml( (array) $data ) );
171+
WP_CLI::log( (string) $this->oembed_create_xml( (array) $data ) );
164172
} else {
165-
WP_CLI::log( json_encode( $data ) );
173+
WP_CLI::log( (string) json_encode( $data ) );
166174
}
167175

168176
return;
169177
}
170178

171179
if ( $post_id ) {
172180
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the request is asking for a post id directly so need to override the global.
173-
$GLOBALS['post'] = get_post( $post_id );
181+
$GLOBALS['post'] = get_post( (int) $post_id );
174182
if ( null === $GLOBALS['post'] ) {
175183
WP_CLI::warning( sprintf( "Post id '%s' not found.", $post_id ) );
176184
}
177185
}
178186

179-
if ( Utils\get_flag_value( $assoc_args, 'skip-sanitization' ) ) {
180-
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10, 3 );
187+
if ( Utils\get_flag_value( $assoc_args, 'skip-sanitization', false ) ) {
188+
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
181189
}
182190

183-
if ( Utils\get_flag_value( $assoc_args, 'skip-cache' ) ) {
191+
if ( Utils\get_flag_value( $assoc_args, 'skip-cache', false ) ) {
184192
$wp_embed->usecache = false;
185193
// In order to skip caching, also need `$cached_recently` to be false in `WP_Embed::shortcode()`, so set TTL to zero.
186194
add_filter( 'oembed_ttl', '__return_zero', PHP_INT_MAX );
@@ -191,27 +199,8 @@ function ( $ret, $data, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunc
191199
// `WP_Embed::shortcode()` sets the 'discover' attribute based on 'embed_oembed_discover' filter, no matter what's passed to it.
192200
add_filter( 'embed_oembed_discover', $discover ? '__return_true' : '__return_false', PHP_INT_MAX );
193201

194-
// For WP < 4.9, `WP_Embed::shortcode()` won't check providers if no post_id supplied, so set `maybe_make_link()` to return false so can check and do it ourselves.
195-
// Also set if WP < 4.4 and don't have 'unfiltered_html' privileges on post.
196-
$check_providers = Utils\wp_version_compare( '4.9', '<' ) && ( ! $post_id || ( Utils\wp_version_compare( '4.4', '<' ) && ! author_can( $post_id, 'unfiltered_html' ) ) );
197-
if ( $check_providers ) {
198-
add_filter( 'embed_maybe_make_link', '__return_false', PHP_INT_MAX );
199-
}
200-
201202
$html = $wp_embed->shortcode( $oembed_args, $url );
202203

203-
if ( false === $html && $check_providers ) {
204-
205-
// Check providers.
206-
$oembed_args['discover'] = $discover;
207-
$html = wp_oembed_get( $url, $oembed_args );
208-
209-
// `wp_oembed_get()` returns zero-length string instead of false on failure due to `_strip_newlines()` 'oembed_dataparse' filter so make sure false.
210-
if ( '' === $html ) {
211-
$html = false;
212-
}
213-
}
214-
215204
if ( false !== $html && '[' === substr( $html, 0, 1 ) && Utils\get_flag_value( $assoc_args, 'do-shortcode' ) ) {
216205
$html = do_shortcode( $html, true );
217206
}

‎src/Provider_Command.php‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ class Provider_Command extends WP_CLI_Command {
8080
* | #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/ |
8181
*
8282
* @subcommand list
83+
*
84+
* @param string[] $args Positional arguments. Unused.
85+
* @param array{field?: string, fields?: string, format: 'table'|'csv'|'json', 'force-regex'?: bool} $assoc_args Associative arguments.
8386
*/
8487
public function list_providers( $args, $assoc_args ) {
8588

86-
$oembed = new oEmbed();
89+
$oembed = new \WP_oEmbed();
8790

8891
$force_regex = Utils\get_flag_value( $assoc_args, 'force-regex' );
8992

@@ -138,14 +141,17 @@ public function list_providers( $args, $assoc_args ) {
138141
* https://www.youtube.com/oembed
139142
*
140143
* @subcommand match
144+
*
145+
* @param array{0: string} $args Positional arguments.
146+
* @param array{discover?: bool, 'limit-response-size'?: string, 'link-type'?: 'json'|'xml', } $assoc_args Associative arguments.
141147
*/
142148
public function match_provider( $args, $assoc_args ) {
143-
$oembed = new oEmbed();
149+
$oembed = new \WP_oEmbed();
144150

145151
$url = $args[0];
146-
$discover = \WP_CLI\Utils\get_flag_value( $assoc_args, 'discover', true );
147-
$response_size_limit = \WP_CLI\Utils\get_flag_value( $assoc_args, 'limit-response-size' );
148-
$link_type = \WP_CLI\Utils\get_flag_value( $assoc_args, 'link-type' );
152+
$discover = Utils\get_flag_value( $assoc_args, 'discover', true );
153+
$response_size_limit = Utils\get_flag_value( $assoc_args, 'limit-response-size' );
154+
$link_type = Utils\get_flag_value( $assoc_args, 'link-type' );
149155

150156
if ( ! $discover && ( null !== $response_size_limit || null !== $link_type ) ) {
151157
if ( null !== $response_size_limit && null !== $link_type ) {
@@ -159,14 +165,10 @@ public function match_provider( $args, $assoc_args ) {
159165
}
160166

161167
if ( $response_size_limit ) {
162-
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
163-
WP_CLI::warning( "The 'limit-response-size' option only works for WordPress 4.0 onwards." );
164-
// Fall through anyway...
165-
}
166168
add_filter(
167169
'oembed_remote_get_args',
168170
function ( $args ) use ( $response_size_limit ) {
169-
$args['limit_response_size'] = $response_size_limit;
171+
$args['limit_response_size'] = (int) $response_size_limit;
170172
return $args;
171173
}
172174
);

‎src/oEmbed.php‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)