Skip to content

Commit

Permalink
Fix almost all issues reported by PHPStan on level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
chesio committed Aug 18, 2023
1 parent 271f441 commit 2cbcda5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
42 changes: 22 additions & 20 deletions classes/BlueChip/Cache/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace BlueChip\Cache;

use WP_CLI;

/**
* Delete items from, flush or get size of BC Cache cache
*
Expand Down Expand Up @@ -45,11 +47,11 @@ public function __construct(private Core $cache, private ?Crawler $cache_crawler
public function delete(array $args, array $assoc_args): void
{
if (empty($post_id = \intval($args[0]))) {
\WP_CLI::error(\sprintf('"%s" is not a valid post ID!', $args[0]));
WP_CLI::error(\sprintf('"%s" is not a valid post ID!', $args[0]));
}

if (empty($url = get_permalink($post_id))) {
\WP_CLI::error(\sprintf('No URL could be generated for post with ID "%d"', $post_id));
WP_CLI::error(\sprintf('No URL could be generated for post with ID "%d"', $post_id));
}

// Use helper method to actually delete related cache entries.
Expand All @@ -65,12 +67,12 @@ public function delete(array $args, array $assoc_args): void
*/
public function flush(array $args, array $assoc_args): void
{
\WP_CLI::line('Flushing BC Cache cache ...');
WP_CLI::line('Flushing BC Cache cache ...');

if ($this->cache->flush()) {
\WP_CLI::success('The cache has been flushed!');
WP_CLI::success('The cache has been flushed!');
} else {
\WP_CLI::error('Failed to flush the cache!');
WP_CLI::error('Failed to flush the cache!');
}
}

Expand All @@ -94,9 +96,9 @@ public function getSize(array $args, array $assoc_args): void
$human_readable = $assoc_args['human-readable'] ?? false;

if (\is_int($size_in_bytes = $this->cache->getSize(true))) {
\WP_CLI::line($human_readable ? size_format($size_in_bytes) : $size_in_bytes);
WP_CLI::line((string) ($human_readable ? size_format($size_in_bytes) : $size_in_bytes));
} else {
\WP_CLI::error('Failed to determine cache size!');
WP_CLI::error('Failed to determine cache size!');
}
}

Expand All @@ -119,7 +121,7 @@ public function getSize(array $args, array $assoc_args): void
public function remove(array $args, array $assoc_args): void
{
if (empty($url = \filter_var($args[0], FILTER_VALIDATE_URL))) {
\WP_CLI::error(\sprintf('"%s" is not a valid URL!', $args[0]));
WP_CLI::error(\sprintf('"%s" is not a valid URL!', $args[0]));
}

// Use helper method to actually remove related cache entries.
Expand Down Expand Up @@ -148,13 +150,13 @@ private function erase(string $url, ?int $post_id = null): void
}
}

\WP_CLI::success(
WP_CLI::success(
$post_id === null
? \sprintf('Cache data for URL "%s" and request variant "%s" has been deleted!', $url, $request_variant_name)
: \sprintf('Cache data for post with ID %d and request variant "%s" has been deleted!', $post_id, $request_variant_name)
);
} else {
\WP_CLI::error(
WP_CLI::error(
$post_id === null
? \sprintf('Failed to delete cache data for URL "%s" and request variant "%s"!', $url, $request_variant_name)
: \sprintf('Failed to delete cache data for post with ID %d and request variant "%s"!', $post_id, $request_variant_name)
Expand Down Expand Up @@ -216,7 +218,7 @@ public function list(array $args, array $assoc_args): void
$columns_to_display = [];
foreach ($args as $arg) {
if (\array_search($arg, $available_columns, true) === false) {
\WP_CLI::error(\sprintf('Unknown column key given: "%s". Exiting ...', $arg));
WP_CLI::error(\sprintf('Unknown column key given: "%s". Exiting ...', $arg));
}

if (!\in_array($arg, $columns_to_display, true)) {
Expand All @@ -232,7 +234,7 @@ public function list(array $args, array $assoc_args): void
// Validate sort by value.
if ($sort_by) {
if (\array_search($sort_by, $available_columns, true) === false) {
\WP_CLI::error(\sprintf('Unknown column key given for --sort-by argument: "%s". Exiting ...', $sort_by));
WP_CLI::error(\sprintf('Unknown column key given for --sort-by argument: "%s". Exiting ...', $sort_by));
}
}

Expand All @@ -252,7 +254,7 @@ public function list(array $args, array $assoc_args): void
$cache_items = $this->cache->inspect();

if ($cache_items === null) {
\WP_CLI::error('Cache items could not be fetched due to I/O error. Exiting ...');
WP_CLI::error('Cache items could not be fetched due to I/O error. Exiting ...');
}

// Prepare items.
Expand Down Expand Up @@ -289,7 +291,7 @@ function (array $a, array $b) use ($sort_by): int {
);
}

\WP_CLI\Utils\format_items($format, $items, $columns_to_display);
WP_CLI\Utils\format_items($format, $items, $columns_to_display);
}


Expand All @@ -304,31 +306,31 @@ function (array $a, array $b) use ($sort_by): int {
public function warmUp(array $args, array $assoc_args): void
{
if (($this->cache_crawler === null) || ($this->cache_feeder === null)) {
\WP_CLI::error('Cache warm up is disabled. Exiting ...');
WP_CLI::error('Cache warm up is disabled. Exiting ...');
}

// Synchronize state of warm up queue with state of cache to get precise warm up queue size.
if (!$this->cache_feeder->synchronize()) {
\WP_CLI::error('Synchronizing state of warm up queue with state of cache failed. Exiting ...');
WP_CLI::error('Synchronizing state of warm up queue with state of cache failed. Exiting ...');
}

\WP_CLI::line('Warming up BC Cache cache ...');
WP_CLI::line('Warming up BC Cache cache ...');

$warm_up_queue_size = $this->cache_feeder->getSize();

if ($warm_up_queue_size === 0) {
\WP_CLI::success('Warm up queue is empty.');
WP_CLI::success('Warm up queue is empty.');
return; // !
}

$progress = \WP_CLI\Utils\make_progress_bar('Progress', $warm_up_queue_size);
$progress = WP_CLI\Utils\make_progress_bar('Progress', $warm_up_queue_size);

while ($this->cache_crawler->step() !== null) {
$progress->tick();
}

$progress->finish();

\WP_CLI::success('Cache warm up finished.');
WP_CLI::success('Cache warm up finished.');
}
}
8 changes: 8 additions & 0 deletions classes/BlueChip/Cache/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ private function getPath(string $url): string
{
$url_parts = \parse_url($url);

if (!\is_array($url_parts)) {
throw new Exception("Cannot parse URL {$url}");
}

if (!\array_key_exists('scheme', $url_parts) || !\array_key_exists('host', $url_parts)) {
throw new Exception("URL {$url} is missing scheme or host part!");
}

$url_path = $url_parts['path'] ?? '';

$path = \implode([
Expand Down
12 changes: 6 additions & 6 deletions classes/BlueChip/Cache/ListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public function column_size(ListTableItem $item): string // phpcs:ignore
{
return \sprintf(
'%s | %s | %s',
esc_html(size_format($item->getTotalSize())),
esc_html(size_format($item->getPlainFileSize())),
esc_html(size_format($item->getGzipFileSize()))
esc_html(size_format($item->getTotalSize()) ?: self::UNKNOWN_VALUE),
esc_html(size_format($item->getPlainFileSize()) ?: self::UNKNOWN_VALUE),
esc_html(size_format($item->getGzipFileSize()) ?: self::UNKNOWN_VALUE)
);
}

Expand Down Expand Up @@ -317,12 +317,12 @@ public function processActions(): void
}

$request_variant = \filter_input(INPUT_GET, 'request_variant');
if (!isset($this->request_variants[$request_variant])) {
if (!\is_string($request_variant) || !\array_key_exists($request_variant, $this->request_variants)) {
return;
}

$nonce = \filter_input(INPUT_GET, self::NONCE_NAME);
if (!wp_verify_nonce($nonce, \sprintf('%s:%s', $action, $url))) {
if (!\is_string($nonce) || !wp_verify_nonce($nonce, \sprintf('%s:%s', $action, $url))) {
// Nonce check failed
return;
}
Expand Down Expand Up @@ -359,7 +359,7 @@ public function processActions(): void
);

// Get URLs.
$urls = $sanitized['urls'];
$urls = $sanitized['urls'] ?? [];

// Number of entries really deleted.
$items_deleted = 0;
Expand Down
4 changes: 2 additions & 2 deletions classes/BlueChip/Cache/WarmUpQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function pull(Item $item): bool
// Remove item from waiting items list if present.
$index = \array_search($item, $this->waiting, false);
if ($index !== false) {
\array_splice($this->waiting, $index, 1);
\array_splice($this->waiting, (int) $index, 1); // PHPStan fails to recognize we are searching int-indexed array.
$dirty = true; // !
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public function push(Item $item): bool
// Remove item from processed items list if present.
$index = \array_search($item, $this->processed, false);
if ($index !== false) {
\array_splice($this->processed, $index, 1);
\array_splice($this->processed, (int) $index, 1); // PHPStan fails to recognize we are searching int-indexed array.
$dirty = true; // !
}

Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ parameters:
scanFiles:
- tests/static-analysis/stubs/php-cli-tools.php
- vendor/php-stubs/wp-cli-stubs/wp-cli-stubs.php
ignoreErrors:
# We are fine with \WP_CLI\NoOp doing nothing.
- '#^Call to an undefined method cli\\progress\\Bar\|WP_CLI\\NoOp::#'

0 comments on commit 2cbcda5

Please sign in to comment.