Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round 1: Format code to WordPress coding standards #78

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: composer install --no-interaction --prefer-dist --optimize-autoloader

- name: PHPCS checks
run: ./vendor/bin/phpcs ${{ github.workspace }}/src
run: ./vendor/bin/phpcs ${{ github.workspace }}/src/WordPress

test-unit:
runs-on: ${{ matrix.os }}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"nette/php-generator": "*",
"jane-php/json-schema": "*",
"bamarni/composer-bin-plugin": "*",
"wp-coding-standards/wpcs": "3.0"
"wp-coding-standards/wpcs": "3.0",
"phpcompatibility/php-compatibility": "^9.3"
},
"config": {
"optimize-autoloader": true,
Expand Down
64 changes: 63 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@

<config name="testVersion" value="7.0"/>

<rule ref="WordPress">
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
<exclude name="Squiz.Commenting.BlockComment.CloserSameLine"/>
<exclude name="Squiz.Commenting.ClassComment.Missing"/>
<exclude name="Squiz.Commenting.FileComment.WrongStyle"/>
<exclude name="Squiz.Commenting.FileComment.Missing"/>
<exclude name="Squiz.Commenting.FunctionComment.Missing"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamType"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
<exclude name="Squiz.Commenting.VariableComment.Missing"/>
<exclude name="Squiz.PHP.DisallowSizeFunctionsInLoops.Found"/>
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/>
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
<exclude name="WordPress.Security.EscapeOutput.OutputNotEscaped"/>
<exclude name="WordPress.WP.AlternativeFunctions.file_system_operations_fclose"/>
</rule>
<rule ref="PHPCompatibility">
<exclude name="PHPCompatibility.Keywords.ForbiddenNamesAsDeclared"/>
</rule>
Expand Down
48 changes: 27 additions & 21 deletions src/WordPress/AsyncHttp/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Client {
protected $queue_needs_processing = false;

public function __construct() {
$this->requests = new Map();
$this->requests = new Map();
$this->onProgress = function () {
};
}
Expand Down Expand Up @@ -163,13 +163,13 @@ public function get_stream( $request ) {
}

/**
* @param \WordPress\AsyncHttp\Request $request
*/
protected function enqueue_request( $request ) {
$stream = StreamWrapper::create_resource(
* @param \WordPress\AsyncHttp\Request $request
*/
protected function enqueue_request( $request ) {
$stream = StreamWrapper::create_resource(
new StreamData( $request, $this )
);
$this->requests[ $request ] = new RequestInfo( $stream );
$this->requests[ $request ] = new RequestInfo( $stream );
$this->queue_needs_processing = true;

return $stream;
Expand All @@ -182,18 +182,18 @@ public function process_queue() {
$this->queue_needs_processing = false;

$active_requests = count( $this->get_streamed_requests() );
$backfill = $this->concurrency - $active_requests;
$backfill = $this->concurrency - $active_requests;
if ( $backfill <= 0 ) {
return;
}

$enqueued = array_slice( $this->get_enqueued_request(), 0, $backfill );
$enqueued = array_slice( $this->get_enqueued_request(), 0, $backfill );
list( $streams, $response_headers ) = streams_send_http_requests( $enqueued );

foreach ( $streams as $k => $stream ) {
$request = $enqueued[ $k ];
$total = $response_headers[ $k ]['headers']['content-length'];
$this->requests[ $request ]->state = RequestInfo::STATE_STREAMING;
$request = $enqueued[ $k ];
$total = $response_headers[ $k ]['headers']['content-length'];
$this->requests[ $request ]->state = RequestInfo::STATE_STREAMING;
$this->requests[ $request ]->stream = stream_monitor_progress(
$stream,
function ( $downloaded ) use ( $request, $total ) {
Expand All @@ -205,7 +205,7 @@ function ( $downloaded ) use ( $request, $total ) {
}

protected function get_enqueued_request() {
$enqueued_requests = [];
$enqueued_requests = array();
foreach ( $this->requests as $request => $info ) {
if ( $info->state === RequestInfo::STATE_ENQUEUED ) {
$enqueued_requests[] = $request;
Expand All @@ -216,7 +216,7 @@ protected function get_enqueued_request() {
}

protected function get_streamed_requests() {
$active_requests = [];
$active_requests = array();
foreach ( $this->requests as $request => $info ) {
if ( $info->state !== RequestInfo::STATE_ENQUEUED ) {
$active_requests[] = $request;
Expand Down Expand Up @@ -245,12 +245,15 @@ public function read_bytes( $request, $length ) {
}

$request_info = $this->requests[ $request ];
$stream = $request_info->stream;
$stream = $request_info->stream;

$active_requests = $this->get_streamed_requests();
$active_streams = array_map( function ( $request ) {
return $this->requests[ $request ]->stream;
}, $active_requests );
$active_streams = array_map(
function ( $request ) {
return $this->requests[ $request ]->stream;
},
$active_requests
);

if ( ! count( $active_streams ) ) {
return false;
Expand All @@ -264,7 +267,7 @@ public function read_bytes( $request, $length ) {
}

if ( strlen( $request_info->buffer ) >= $length ) {
$buffered = substr( $request_info->buffer, 0, $length );
$buffered = substr( $request_info->buffer, 0, $length );
$request_info->buffer = substr( $request_info->buffer, $length );

return $buffered;
Expand All @@ -274,9 +277,12 @@ public function read_bytes( $request, $length ) {
return $request_info->buffer;
}

$active_streams = array_filter( $active_streams, function ( $stream ) {
return ! feof( $stream );
} );
$active_streams = array_filter(
$active_streams,
function ( $stream ) {
return ! feof( $stream );
}
);
if ( ! count( $active_streams ) ) {
continue;
}
Expand Down
1 change: 0 additions & 1 deletion src/WordPress/AsyncHttp/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ class Request {
public function __construct( string $url ) {
$this->url = $url;
}

}
3 changes: 1 addition & 2 deletions src/WordPress/AsyncHttp/RequestInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public function __construct( $stream ) {
$this->stream = $stream;
}

public function isFinished() {
public function is_finished() {
return $this->state === self::STATE_FINISHED;
}

}
3 changes: 1 addition & 2 deletions src/WordPress/AsyncHttp/StreamData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class StreamData extends VanillaStreamWrapperData {
public function __construct( Request $request, Client $group ) {
parent::__construct( null );
$this->request = $request;
$this->client = $group;
$this->client = $group;
}

}
9 changes: 4 additions & 5 deletions src/WordPress/AsyncHttp/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function stream_open( $path, $mode, $options, &$opened_path ) {
}

/**
* @param int $cast_as
*/
public function stream_cast( $cast_as ) {
* @param int $cast_as
*/
public function stream_cast( $cast_as ) {
$this->initialize();

return parent::stream_cast( $cast_as );
Expand Down Expand Up @@ -103,7 +103,6 @@ public function stream_seek( $offset, $whence ) {
* Let's refuse to call fclose() in that scenario.
*/
protected function has_valid_stream() {
return get_resource_type( $this->stream ) !== "Unknown";
return get_resource_type( $this->stream ) !== 'Unknown';
}

}
Loading
Loading