Skip to content

PHPStan level 9 #110

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
},
"require-dev": {
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/wp-cli-tests": "dev-add/phpstan-enhancements"
},
"config": {
"process-timeout": 7200,
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
"johnpbloch/wordpress-core-installer": true,
"phpstan/extension-installer": true
},
"lock": false
},
Expand Down Expand Up @@ -72,12 +73,14 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit",
"@behat"
]
Expand Down
24 changes: 24 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
parameters:
level: 9
paths:
- src
- cache-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
- vendor/wp-cli/wp-cli-tests
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- WP_DEBUG
- WP_DEBUG_LOG
- WP_DEBUG_DISPLAY
strictRules:
uselessCast: true
closureUsesThis: true
overwriteVariablesWithLoop: true
matchingInheritedMethodNames: true
numericOperandsInArithmeticOperators: true
switchConditionsMatchingType: true
ignoreErrors:
- identifier: missingType.return
72 changes: 58 additions & 14 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@
* # Add cache.
* $ wp cache add my_key my_group my_value 300
* Success: Added object 'my_key' in group 'my_value'.
*
* @param array{string, string, string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function add( $args, $assoc_args ) {
list( $key, $value, $group, $expiration ) = $args;

if ( ! wp_cache_add( $key, $value, $group, $expiration ) ) {
if ( ! wp_cache_add( $key, $value, $group, (int) $expiration ) ) {
WP_CLI::error( "Could not add object '$key' in group '$group'. Does it already exist?" );
}

Expand Down Expand Up @@ -96,10 +99,13 @@
* # Decrease cache value.
* $ wp cache decr my_key 2 my_group
* 48
*
* @param array{string, string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function decr( $args, $assoc_args ) {
list( $key, $offset, $group ) = $args;
$value = wp_cache_decr( $key, $offset, $group );
$value = wp_cache_decr( $key, (int) $offset, $group );

if ( false === $value ) {
WP_CLI::error( 'The value was not decremented.' );
Expand Down Expand Up @@ -129,6 +135,9 @@
* # Delete cache.
* $ wp cache delete my_key my_group
* Success: Object deleted.
*
* @param array{string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function delete( $args, $assoc_args ) {
list( $key, $group ) = $args;
Expand Down Expand Up @@ -157,8 +166,9 @@
* $ wp cache flush
* Success: The cache was flushed.
*/
public function flush( $args, $assoc_args ) {

public function flush() {
// TODO: Needs fixing in wp-cli/wp-cli
// @phpstan-ignore offsetAccess.nonOffsetAccessible
if ( WP_CLI::has_config( 'url' ) && ! empty( WP_CLI::get_config()['url'] ) && is_multisite() ) {
WP_CLI::warning( 'Flushing the cache may affect all sites in a multisite installation, depending on the implementation of the object cache.' );
}
Expand Down Expand Up @@ -192,6 +202,9 @@
* # Get cache.
* $ wp cache get my_key my_group
* my_value
*
* @param array{string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
list( $key, $group ) = $args;
Expand Down Expand Up @@ -231,10 +244,13 @@
* # Increase cache value.
* $ wp cache incr my_key 2 my_group
* 50
*
* @param array{string, string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function incr( $args, $assoc_args ) {
list( $key, $offset, $group ) = $args;
$value = wp_cache_incr( $key, $offset, $group );
$value = wp_cache_incr( $key, (int) $offset, $group );

if ( false === $value ) {
WP_CLI::error( 'The value was not incremented.' );
Expand Down Expand Up @@ -273,10 +289,13 @@
* # Replace cache.
* $ wp cache replace my_key new_value my_group
* Success: Replaced object 'my_key' in group 'my_group'.
*
* @param array{string, string, string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function replace( $args, $assoc_args ) {
list( $key, $value, $group, $expiration ) = $args;
$result = wp_cache_replace( $key, $value, $group, $expiration );
$result = wp_cache_replace( $key, $value, $group, (int) $expiration );

if ( false === $result ) {
WP_CLI::error( "Could not replace object '$key' in group '$group'. Does it not exist?" );
Expand Down Expand Up @@ -315,10 +334,13 @@
* # Set cache.
* $ wp cache set my_key my_value my_group 300
* Success: Set object 'my_key' in group 'my_group'.
*
* @param array{string, string, string, string} $args Positional arguments.
* @param array<mixed> $assoc_args Associative arguments.
*/
public function set( $args, $assoc_args ) {
list( $key, $value, $group, $expiration ) = $args;
$result = wp_cache_set( $key, $value, $group, $expiration );
$result = wp_cache_set( $key, $value, $group, (int) $expiration );

if ( false === $result ) {
WP_CLI::error( "Could not add object '$key' in group '$group'." );
Expand All @@ -341,7 +363,7 @@
* $ wp cache type
* Default
*/
public function type( $args, $assoc_args ) {
public function type() {

Check warning on line 366 in src/Cache_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Cache_Command.php#L366

Added line #L366 was not covered by tests
$message = WP_CLI\Utils\wp_get_cache_type();
WP_CLI::line( $message );
}
Expand All @@ -365,8 +387,10 @@
* if ! wp cache supports non_existing; then
* echo 'non_existing is not supported'
* fi
*
* @param array{string} $args Positional arguments.
*/
public function supports( $args, $assoc_args ) {
public function supports( $args ) {
list ( $feature ) = $args;

if ( ! function_exists( 'wp_cache_supports' ) ) {
Expand Down Expand Up @@ -396,8 +420,10 @@
* Success: Cache group 'my_group' was flushed.
*
* @subcommand flush-group
*
* @param array{string} $args Positional arguments.
*/
public function flush_group( $args, $assoc_args ) {
public function flush_group( $args ) {
list( $group ) = $args;

if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
Expand Down Expand Up @@ -436,10 +462,17 @@
* - json
* - yaml
* ---
*
* @param array{string, string} $args Positional arguments.
* @param array{group: string, format: string} $assoc_args Associative arguments.
*/
public function pluck( $args, $assoc_args ) {
list( $key ) = $args;
$group = Utils\get_flag_value( $assoc_args, 'group' );

/**
* @var string $group
*/
$group = Utils\get_flag_value( $assoc_args, 'group' );

$value = wp_cache_get( $key, $group );

Expand Down Expand Up @@ -512,11 +545,22 @@
* - plaintext
* - json
* ---
*
* @param string[] $args Positional arguments.
* @param array{group: string, expiration: string, format: string} $assoc_args Associative arguments.
*/
public function patch( $args, $assoc_args ) {
list( $action, $key ) = $args;
$group = Utils\get_flag_value( $assoc_args, 'group' );
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );

/**
* @var string $group
*/
$group = Utils\get_flag_value( $assoc_args, 'group' );

/**
* @var string|null $expiration
*/
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );

$key_path = array_map(
function ( $key ) {
Expand Down Expand Up @@ -569,7 +613,7 @@
if ( $patched_value === $old_value ) {
WP_CLI::success( "Value passed for cache key '$key' is unchanged." );
} else {
$success = wp_cache_set( $key, $patched_value, $group, $expiration );
$success = wp_cache_set( $key, $patched_value, $group, (int) $expiration );
if ( $success ) {
WP_CLI::success( "Updated cache key '$key'." );
} else {
Expand Down
44 changes: 37 additions & 7 deletions src/Transient_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class Transient_Command extends WP_CLI_Command {
*
* $ wp transient get random_key
* Warning: Transient with key "random_key" is not set.
*
* @param array{string} $args Positional arguments.
* @param array{format: string} $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
list( $key ) = $args;
Expand Down Expand Up @@ -120,14 +123,17 @@ public function get( $args, $assoc_args ) {
*
* $ wp transient set sample_key "test data" 3600
* Success: Transient added.
*
* @param array{0: string, 1: string, 2?: string} $args Positional arguments.
* @param array{network?: bool} $assoc_args Associative arguments.
*/
public function set( $args, $assoc_args ) {
list( $key, $value ) = $args;

$expiration = Utils\get_flag_value( $args, 2, 0 );
$expiration = $args[2] ?? 0;

$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
if ( $func( $key, $value, $expiration ) ) {
if ( $func( $key, $value, (int) $expiration ) ) {
WP_CLI::success( 'Transient added.' );
} else {
WP_CLI::error( 'Transient could not be set.' );
Expand Down Expand Up @@ -180,6 +186,9 @@ public function set( $args, $assoc_args ) {
*
* # Delete all transients in a multisite.
* $ wp transient delete --all --network && wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --all
*
* @param array{string} $args Positional arguments.
* @param array{network?: bool, all?: bool, expired?: bool} $assoc_args Associative arguments.
*/
public function delete( $args, $assoc_args ) {
$key = ( ! empty( $args ) ) ? $args[0] : null;
Expand Down Expand Up @@ -297,6 +306,9 @@ public function type() {
* +------+-------+---------------+
*
* @subcommand list
*
* @param string[] $args Positional arguments. Unused.
* @param array{search?: string, exclude?: string, network?: bool, unserialize?: bool, 'human-readable'?: bool, fields?: string, format?: string} $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
global $wpdb;
Expand Down Expand Up @@ -434,6 +446,9 @@ public function list_( $args, $assoc_args ) {
* : Get the value of a network|site transient. On single site, this is
* a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*
* @param string[] $args Positional arguments.
* @param array{format: string} $assoc_args Associative arguments.
*/
public function pluck( $args, $assoc_args ) {
list( $key ) = $args;
Expand Down Expand Up @@ -506,10 +521,14 @@ function ( $key ) {
* : Get the value of a network|site transient. On single site, this is
* a specially-named cache key. On multisite, this is a global cache
* (instead of local to the site).
*
* @param string[] $args Positional arguments.
* @param array{format: string} $assoc_args Associative arguments.
*/
public function patch( $args, $assoc_args ) {
list( $action, $key ) = $args;
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );

$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );

$read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
Expand Down Expand Up @@ -586,20 +605,31 @@ function ( $key ) {
private function get_transient_expiration( $name, $is_site_transient = false, $human_readable = false ) {
if ( $is_site_transient ) {
if ( is_multisite() ) {
$expiration = (int) get_site_option( '_site_transient_timeout_' . $name );
/**
* @var string $expiration
*/
$expiration = get_site_option( '_site_transient_timeout_' . $name );
} else {
$expiration = (int) get_option( '_site_transient_timeout_' . $name );
/**
* @var string $expiration
*/
$expiration = get_option( '_site_transient_timeout_' . $name );
}
} else {
$expiration = (int) get_option( '_transient_timeout_' . $name );
/**
* @var string $expiration
*/
$expiration = get_option( '_transient_timeout_' . $name );
}

$expiration = (int) $expiration;

if ( 0 === $expiration ) {
return $human_readable ? 'never expires' : 'false';
}

if ( ! $human_readable ) {
return $expiration;
return (string) $expiration;
}

$now = time();
Expand Down