Skip to content

Commit 030d0a7

Browse files
authored
PHPCS – fix more warnings (#178)
Lints a few more files as a part of the ongoing #157 effort
1 parent 76ad402 commit 030d0a7

File tree

13 files changed

+77
-55
lines changed

13 files changed

+77
-55
lines changed

components/Blueprints/SiteResolver/class-newsiteresolver.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ public static function resolve( Runtime $runtime, Tracker $progress, ?VersionCon
152152
private static function is_wordpress_installed( Runtime $runtime, Tracker $progress ) {
153153
$install_check = $runtime->eval_php_code_in_subprocess(
154154
<<<'PHP'
155-
<?php
156-
$wp_load = getenv('DOCROOT') . '/wp-load.php';
157-
if (!file_exists($wp_load)) {
158-
append_output('0');
159-
exit;
160-
}
161-
require $wp_load;
155+
<?php
156+
$wp_load = getenv('DOCROOT') . '/wp-load.php';
157+
if (!file_exists($wp_load)) {
158+
append_output('0');
159+
exit;
160+
}
161+
require $wp_load;
162162
163-
append_output( function_exists('is_blog_installed') && is_blog_installed() ? '1' : '0' );
163+
append_output( function_exists('is_blog_installed') && is_blog_installed() ? '1' : '0' );
164164
PHP
165165
,
166166
array(

components/Blueprints/Steps/scripts/import-content.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ function map_file_path_to_wordpress_url( $path ) {
400400
* to the format they will have after being imported into WordPress (e.g. /docs/getting-started).
401401
*/
402402
add_action(
403-
'data_liberation.stream_importer.postprocess_url',
403+
'data_liberation_stream_importer_postprocess_url',
404404
function (
405405
$processor,
406406
$context
@@ -471,7 +471,7 @@ function (
471471
* Assigns post_name to every imported static page.
472472
*/
473473
add_filter(
474-
'data_liberation.stream_importer.preprocess_entity',
474+
'data_liberation_stream_importer_preprocess_entity',
475475
function ( $entity ) use ( &$import_path_prefix, $index_file_pattern ) {
476476
static $preprocessed_an_entity = false;
477477
if ( 'post' !== $entity->get_type() ) {
@@ -560,7 +560,7 @@ function ( $entity ) use ( &$import_path_prefix, $index_file_pattern ) {
560560
* Drop .xhtml extension from the links.
561561
*/
562562
add_action(
563-
'data_liberation.stream_importer.postprocess_url',
563+
'data_liberation_stream_importer_postprocess_url',
564564
function ( $processor ) {
565565
$parsed_url = $processor->get_parsed_url();
566566
if ( ! str_ends_with( $parsed_url->pathname, '.xhtml' ) ) {

components/DataLiberation/Importer/class-attachmentdownloader.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class AttachmentDownloader {
1919
*/
2020
private $source_from_filesystem;
2121

22-
private $current_event;
2322
private $pending_events = array();
2423
private $enqueued_url;
2524
private $progress = array();
@@ -151,19 +150,15 @@ public function queue_full() {
151150
return count( $this->client->get_active_requests() ) >= 10;
152151
}
153152

154-
public function get_event() {
155-
return $this->current_event;
156-
}
157-
158-
public function next_event() {
159-
$this->current_event = null;
160-
if ( 0 === count( $this->pending_events ) ) {
161-
return false;
162-
}
163-
164-
$this->current_event = array_shift( $this->pending_events );
165-
166-
return true;
153+
/**
154+
* Returns and clears all pending events.
155+
*
156+
* @return AttachmentDownloaderEvent[]
157+
*/
158+
public function get_events() {
159+
$events = $this->pending_events;
160+
$this->pending_events = array();
161+
return $events;
167162
}
168163

169164
public function poll() {
@@ -194,6 +189,7 @@ public function poll() {
194189
if ( file_exists( $this->output_paths[ $original_request_id ] . '.partial' ) ) {
195190
unlink( $this->output_paths[ $original_request_id ] . '.partial' );
196191
}
192+
echo $this->output_paths[ $original_request_id ] . "\n";
197193
$fp = fopen( $this->output_paths[ $original_request_id ] . '.partial', 'wb' );
198194
if ( false !== $fp ) {
199195
$this->fps[ $original_request_id ] = $fp;
@@ -232,7 +228,10 @@ public function poll() {
232228

233229
private function on_failure( $original_url, $original_request_id, $error = null ) {
234230
if ( isset( $this->fps[ $original_request_id ] ) ) {
235-
fclose( $this->fps[ $original_request_id ] );
231+
if ( is_resource( $this->fps[ $original_request_id ] ) ) {
232+
fclose( $this->fps[ $original_request_id ] );
233+
}
234+
unset( $this->fps[ $original_request_id ] );
236235
}
237236
if ( isset( $this->output_paths[ $original_request_id ] ) ) {
238237
$partial_file = $this->output_paths[ $original_request_id ] . '.partial';
@@ -252,7 +251,10 @@ private function on_failure( $original_url, $original_request_id, $error = null
252251
private function on_success( $original_url, $original_request_id ) {
253252
// Only clean up if this was the last request in the chain.
254253
if ( isset( $this->fps[ $original_request_id ] ) ) {
255-
fclose( $this->fps[ $original_request_id ] );
254+
if ( is_resource( $this->fps[ $original_request_id ] ) ) {
255+
fclose( $this->fps[ $original_request_id ] );
256+
}
257+
unset( $this->fps[ $original_request_id ] );
256258
}
257259
if ( isset( $this->output_paths[ $original_request_id ] ) ) {
258260
if ( false === rename(
@@ -269,4 +271,15 @@ private function on_success( $original_url, $original_request_id ) {
269271
unset( $this->progress[ $original_url ] );
270272
unset( $this->output_paths[ $original_request_id ] );
271273
}
274+
275+
public function __destruct() {
276+
// Ensure any remaining open file descriptors are closed.
277+
foreach ( $this->fps as $request_id => $fp ) {
278+
if ( is_resource( $fp ) ) {
279+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
280+
@fclose( $fp );
281+
}
282+
unset( $this->fps[ $request_id ] );
283+
}
284+
}
272285
}

components/DataLiberation/Importer/class-streamimporter.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,7 @@ public function get_frontloading_progress() {
595595
* the relevant files already exist in the filesystem.
596596
*/
597597
protected function frontloading_advance_reentrancy_cursor() {
598-
while ( $this->downloader->next_event() ) {
599-
$event = $this->downloader->get_event();
598+
foreach ( $this->downloader->get_events() as $event ) {
600599
switch ( $event->type ) {
601600
case AttachmentDownloaderEvent::FAILURE:
602601
case AttachmentDownloaderEvent::SUCCESS:
@@ -743,7 +742,7 @@ protected function frontload_next_entity() {
743742
public function get_current_entity() {
744743
$entity = $this->entity_iterator->current();
745744
$entity = apply_filters(
746-
'data_liberation.stream_importer.preprocess_entity',
745+
'data_liberation_stream_importer_preprocess_entity',
747746
$entity,
748747
array(
749748
'importer' => $this,
@@ -755,7 +754,7 @@ public function get_current_entity() {
755754

756755
protected function get_post_base_url( $post ) {
757756
return apply_filters(
758-
'data_liberation.stream_importer.post_base_url',
757+
'data_liberation_stream_importer_post_base_url',
759758
$post['link'] ?? $this->source_site_url,
760759
array(
761760
'post' => $post,
@@ -873,12 +872,12 @@ protected function import_next_entity() {
873872
$p->replace_base_url( $mapping_pair['to'], $mapping_pair['from'] );
874873
}
875874
do_action(
876-
'data_liberation.stream_importer.postprocess_url',
875+
'data_liberation_stream_importer_postprocess_url',
877876
$p,
878877
array(
879878
'applied_base_url_mapping' => $mapping_pair,
880-
'raw_url_before' => $raw_url_before,
881-
'entity' => $entity,
879+
'raw_url_before' => $raw_url_before,
880+
'entity' => $entity,
882881
)
883882
);
884883
}
@@ -1037,7 +1036,7 @@ protected function url_processor_matched_asset_url( BlockMarkupUrlProcessor $p )
10371036
*/
10381037
$path = $p->get_parsed_url()->pathname;
10391038
$extension = pathinfo( $path, PATHINFO_EXTENSION );
1040-
if ( ! in_array( $extension, array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg' ) ) ) {
1039+
if ( ! in_array( $extension, array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg' ), true ) ) {
10411040
/**
10421041
* Absent an extension, try to guess whether it's a static asset based
10431042
* on its location in the document. For now, we only accept images.

components/Filesystem/class-inmemoryfilesystem.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ protected function get_root(): string {
3939
return '/';
4040
}
4141

42-
public function ls( $parent = '/' ) {
43-
if ( ! isset( $this->files[ $parent ] ) || 'dir' !== $this->files[ $parent ]['type'] ) {
42+
public function ls( $dir = '/' ) {
43+
if ( ! isset( $this->files[ $dir ] ) || 'dir' !== $this->files[ $dir ]['type'] ) {
4444
throw new FilesystemException(
45-
sprintf( 'Directory not found: %s', $parent )
45+
sprintf( 'Directory not found: %s', $dir )
4646
);
4747
}
4848

49-
return array_keys( $this->files[ $parent ]['contents'] );
49+
return array_keys( $this->files[ $dir ]['contents'] );
5050
}
5151

5252
public function is_dir( $path ) {

components/Filesystem/class-localfilesystem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function rename( $old_path, $new_path, $options = array() ) {
114114
}
115115

116116
public function copy_file( $from_path, $to_path, $options ) {
117+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
117118
if ( false === @copy( $from_path, $to_path ) ) {
118119
throw new FilesystemException(
119120
sprintf( 'Failed to copy file: %s to %s', $from_path, $to_path )
@@ -134,6 +135,7 @@ protected function mkdir_single( $path, $options = array() ) {
134135
);
135136
}
136137
if ( isset( $options['chmod'] ) ) {
138+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
137139
if ( false === @chmod( $path, $options['chmod'] ) ) {
138140
throw new FilesystemException(
139141
sprintf( 'Failed to chmod directory: %s', $path )
@@ -159,6 +161,7 @@ protected function rmdir_single( $path, $options = array() ) {
159161
}
160162

161163
public function put_contents( $path, $data, $options = array() ) {
164+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
162165
if ( false === @file_put_contents(
163166
$path,
164167
$data

components/Filesystem/class-uploadedfilesystem.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ private function __construct( $request, $tree_parameter_name, $options = array()
8181
$this->uploads_fs = $options['uploads_fs'] ?? LocalFilesystem::create( '/' );
8282
}
8383

84-
public function ls( $parent = '/' ) {
85-
$parent = '/' . ltrim( wp_unix_path_resolve_dots( $parent ), '/' );
86-
$node = $this->find_node( $parent );
84+
public function ls( $parent_path = '/' ) {
85+
$parent_path = '/' . ltrim( wp_unix_path_resolve_dots( $parent_path ), '/' );
86+
$node = $this->find_node( $parent_path );
8787
if ( ! $node || 'directory' !== $node['type'] ) {
8888
return array();
8989
}

components/Filesystem/interface-filesystem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ interface Filesystem {
1717
/**
1818
* List the contents of a directory.
1919
*
20-
* @param string $parent The path to the parent directory.
20+
* @param string $dir The path to the parent directory.
2121
*
2222
* @return array<string> The contents of the directory.
2323
* @throws FilesystemException If the directory cannot be listed.
2424
*/
25-
public function ls( $parent = '/' );
25+
public function ls( $dir = '/' );
2626

2727
/**
2828
* Check if a path is a directory.

components/HttpClient/Middleware/class-redirectionmiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function handle_redirect( Request $request ) {
6666
return;
6767
}
6868
$code = $response->status_code;
69-
if ( ! in_array( $code, array( 301, 302, 303, 307, 308 ) ) ) {
69+
if ( ! in_array( $code, array( 301, 302, 303, 307, 308 ), true ) ) {
7070
return;
7171
}
7272

components/HttpClient/Transport/class-sockettransport.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function open_nonblocking_http_sockets( $requests ) {
130130
$url = $request->url;
131131
$parts = parse_url( $url );
132132
$scheme = $parts['scheme'];
133-
if ( ! in_array( $scheme, array( 'http', 'https' ) ) ) {
133+
if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) {
134134
$this->set_error(
135135
$request,
136136
new HttpError( 'stream_http_open_nonblocking: Invalid scheme in URL ' . $url . ' – only http:// and https:// URLs are supported' )
@@ -153,6 +153,7 @@ protected function open_nonblocking_http_sockets( $requests ) {
153153
)
154154
);
155155

156+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
156157
$stream = @stream_socket_client(
157158
'tcp://' . $host . ':' . $port,
158159
$errno,
@@ -200,7 +201,7 @@ protected function decode_and_monitor_response_body_stream( Request $request ) {
200201
}
201202

202203
$content_encoding = $request->response->get_header( 'content-encoding' );
203-
if ( $content_encoding && ! in_array( $content_encoding, $transfer_encodings ) ) {
204+
if ( $content_encoding && ! in_array( $content_encoding, $transfer_encodings, true ) ) {
204205
$transfer_encodings[] = $content_encoding;
205206
}
206207

@@ -249,9 +250,11 @@ protected function decode_and_monitor_response_body_stream( Request $request ) {
249250
*/
250251
protected function enable_crypto( array $requests ) {
251252
foreach ( $this->stream_select( $requests, static::STREAM_SELECT_WRITE ) as $request ) {
253+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
252254
@stream_set_timeout( $this->state->connections[ $request->id ]->http_socket, 1 );
253255

254256
// Use @ to suppress warnings. They're collected by error_get_last().
257+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
255258
$enabled_crypto = @stream_socket_enable_crypto(
256259
$this->state->connections[ $request->id ]->http_socket,
257260
true,
@@ -282,6 +285,7 @@ protected function enable_crypto( array $requests ) {
282285
protected function send_request_headers( array $requests ) {
283286
foreach ( $this->stream_select( $requests, static::STREAM_SELECT_WRITE ) as $request ) {
284287
$header_bytes = static::prepare_request_headers( $request );
288+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
285289
if ( false === @fwrite( $this->state->connections[ $request->id ]->http_socket, $header_bytes ) ) {
286290
$last_error = error_get_last();
287291
$last_error_message = is_array( $last_error ) ? $last_error['message'] : 'unknown';
@@ -330,6 +334,7 @@ protected function send_request_body( array $requests ) {
330334
}
331335

332336
$chunk = $request->upload_body_stream->consume( $available_bytes );
337+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
333338
if ( ! @fwrite( $this->state->connections[ $request->id ]->http_socket, $chunk ) ) {
334339
$last_error = error_get_last();
335340
$last_error_message = is_array( $last_error ) ? $last_error['message'] : 'unknown';
@@ -360,6 +365,7 @@ protected function receive_response_headers( $requests ) {
360365
if (
361366
! $this->state->connections[ $request->id ]->http_socket ||
362367
! is_resource( $this->state->connections[ $request->id ]->http_socket ) ||
368+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
363369
@feof( $this->state->connections[ $request->id ]->http_socket )
364370
) {
365371
$this->set_error( $request, new HttpError( 'Connection closed while reading response headers.' ) );
@@ -372,6 +378,7 @@ protected function receive_response_headers( $requests ) {
372378
if (
373379
! $this->state->connections[ $request->id ]->http_socket ||
374380
! is_resource( $this->state->connections[ $request->id ]->http_socket ) ||
381+
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
375382
@feof( $this->state->connections[ $request->id ]->http_socket )
376383
) {
377384
$this->set_error( $request, new HttpError( 'Connection closed while reading response headers.' ) );
@@ -493,7 +500,7 @@ protected function filter_requests( array $requests, $states ) {
493500
}
494501
$results = array();
495502
foreach ( $requests as $request ) {
496-
if ( in_array( $request->state, $states ) ) {
503+
if ( in_array( $request->state, $states, true ) ) {
497504
$results[] = $request;
498505
}
499506
}

0 commit comments

Comments
 (0)