From 1e9a6000965579b2842d4c20c5a66a3f02fe14a9 Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Sat, 15 Jun 2019 10:11:50 +0300 Subject: [PATCH] Psalm cleanup pass. --- psalm.xml | 22 +++----------- src/Formatter/Hook_Formatter.php | 29 +++++++++++++------ src/Manager/Load_Order_Manager.php | 2 -- src/Plugin.php | 1 + src/Provider/Record_Provider.php | 7 +++++ src/Record/Collector/Hook_Collector.php | 18 ++++++++++-- src/Record/Collector/Http_Collector.php | 7 +++-- .../Collector/Plugin_Load_Collector.php | 4 +-- src/Record/Collector/Sql_Collector.php | 4 +++ src/Record/Collector/Stopwatch_Collector.php | 1 + 10 files changed, 58 insertions(+), 37 deletions(-) diff --git a/psalm.xml b/psalm.xml index b4ba3cf..dc113ad 100644 --- a/psalm.xml +++ b/psalm.xml @@ -16,28 +16,14 @@ - - - - - - - + + - + + - - - - - - - - - - diff --git a/src/Formatter/Hook_Formatter.php b/src/Formatter/Hook_Formatter.php index 6051e2e..0eb65b4 100644 --- a/src/Formatter/Hook_Formatter.php +++ b/src/Formatter/Hook_Formatter.php @@ -25,7 +25,14 @@ class Hook_Formatter { } /** + * @template Callback as string|object|array + * @template AcceptedArgs as int + * @template Hook as array{function: Callback, accepted_args: AcceptedArgs} + * @template Priority as array-key + * @template Hooks as array> + * * @param \WP_Hook|array $hook Hook instance. + * @psalm-param \WP_Hook|Hooks $hook * * @return array */ @@ -34,6 +41,7 @@ class Hook_Formatter { $callbacks = []; if ( $hook instanceof \WP_Hook ) { + /** @psalm-var Hooks $hook->callbacks */ $hook = $hook->callbacks; } @@ -43,9 +51,12 @@ class Hook_Formatter { ksort( $hook ); + /** @psalm-suppress InvalidArgument */ $functions = array_merge( ...$hook ); + /** @psalm-var Hook[] $functions */ foreach ( $functions as $function ) { + /** @psalm-var Hook $function */ $callback = $this->get_callback_name( $function['function'], $function['accepted_args'] ); if ( false !== strpos( $callback, 'Hook_Collector' ) ) { @@ -61,27 +72,27 @@ class Hook_Formatter { /** * @param string|object|array $callback Hook callback. * @param int $args Number of accepted arguments. - * - * @return string */ protected function get_callback_name( $callback, int $args ): string { switch ( gettype( $callback ) ) { case 'object': - $callback = $this->get_class_name( $callback ); + $name = $this->get_class_name( $callback ); break; case 'array': - $class = is_string( $callback[0] ) + /** @psalm-var array{0: object|string, 1: string} $callback */ + $class = is_string( $callback[0] ) ? $callback[0] . '::' : $this->get_class_name( $callback[0] ) . '->'; - $callback = $class . $callback[1]; + $name = $class . $callback[1]; break; + + default: + $name = $callback; } - $callback .= ( 1 === $args ) ? '' : "({$args})"; - - return $callback; + return $name . ( ( 1 === $args ) ? '' : "({$args})" ); } /** @@ -91,7 +102,7 @@ class Hook_Formatter { */ protected function get_class_name( $object ): string { - if ( is_a( $object, 'CLosure' ) ) { + if ( $object instanceof \Closure ) { $class = new \ReflectionFunction( $object ); return 'closure from ' . $this->shorten_path( $class->getFileName() ) . ':' . $class->getStartLine(); diff --git a/src/Manager/Load_Order_Manager.php b/src/Manager/Load_Order_Manager.php index f87c6cd..78d4f8d 100644 --- a/src/Manager/Load_Order_Manager.php +++ b/src/Manager/Load_Order_Manager.php @@ -21,8 +21,6 @@ class Load_Order_Manager { * Reorder active plugins so Laps is first and starts timing load early. * * @param array $plugins Filtered array of active plugins’ basenames. - * - * @return array */ public function pre_update_option_active_plugins( array $plugins ): array { diff --git a/src/Plugin.php b/src/Plugin.php index c392b85..5280cdf 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -20,6 +20,7 @@ class Plugin extends Container { /** * @param array $values Configuration values to apply. + * @psalm-param array $values * * @psalm-suppress MixedArgument * @psalm-suppress MixedAssignment diff --git a/src/Provider/Record_Provider.php b/src/Provider/Record_Provider.php index fbaeb32..c29eaeb 100644 --- a/src/Provider/Record_Provider.php +++ b/src/Provider/Record_Provider.php @@ -42,7 +42,14 @@ class Record_Provider implements ServiceProviderInterface, Bootable_Provider_Int return new Lazy_Proxy_Collector( $laps['collectors'] ); }; + /** + * @psalm-suppress MixedInferredReturnType + */ $pimple['records'] = function ( Plugin $laps ): array { + /** + * @psalm-suppress MixedMethodCall + * @psalm-suppress MixedReturnStatement + */ return $laps['records.lazy']->get_records(); }; } diff --git a/src/Record/Collector/Hook_Collector.php b/src/Record/Collector/Hook_Collector.php index 4c04ebb..8d0ac43 100644 --- a/src/Record/Collector/Hook_Collector.php +++ b/src/Record/Collector/Hook_Collector.php @@ -20,13 +20,19 @@ class Hook_Collector extends Stopwatch_Collector { /** @var array $events */ protected $events = []; - /** @var array */ + /** + * @var array $callbacks + * @psalm-var array $callbacks + */ protected $callbacks = []; /** @var Hook_Formatter */ protected $formatter; - /** @var array $calls Tracks number of calls for recurrent events. */ + /** + * @var array $calls Tracks number of calls for recurrent events. + * @psalm-var array $calls + */ private $calls = []; /** @@ -55,7 +61,7 @@ class Hook_Collector extends Stopwatch_Collector { /** * Time the point between network/MU plugins and regular if network activated. */ - public function muplugins_loaded() { + public function muplugins_loaded(): void { $this->stop( 'Network & MU Plugins Load' ); $this->start( 'Plugins Load', 'plugin' ); } @@ -97,6 +103,9 @@ class Hook_Collector extends Stopwatch_Collector { * @param string|null $stop Stop hook name (defaults to start name). Pass empty string to ignore. * @param int $start_priority Start hook priority (defaults to -1). * @param int $stop_priority Stop hook priority (defaults to max int). + * + * @psalm-suppress MissingClosureReturnType + * @psalm-suppress MissingClosureParamType */ private function add_event( string $event, @@ -116,11 +125,13 @@ class Hook_Collector extends Stopwatch_Collector { $event .= $this->get_count_suffix( $event, true ); if ( 'Sidebar' === $event ) { + /** @var string $input */ $event = $input; } if ( $start === $stop ) { global $wp_filter; + /** @psalm-var array $wp_filter */ $this->callbacks[ $event ] = $wp_filter[ $start ]; } @@ -136,6 +147,7 @@ class Hook_Collector extends Stopwatch_Collector { $event .= $this->get_count_suffix( $event ); if ( 'Sidebar' === $event ) { + /** @var string $input */ $event = $input; } diff --git a/src/Record/Collector/Http_Collector.php b/src/Record/Collector/Http_Collector.php index 3374f56..cc31f58 100644 --- a/src/Record/Collector/Http_Collector.php +++ b/src/Record/Collector/Http_Collector.php @@ -16,7 +16,10 @@ class Http_Collector extends Stopwatch_Collector { /** @var Backtrace_Formatter */ private $formatter; - /** @var array */ + /** + * @var array $callers + * @psalm-var array $callers + */ private $callers = []; /** @@ -39,7 +42,7 @@ class Http_Collector extends Stopwatch_Collector { * @param array $args HTTP request arguments. * @param string $url The request URL. * - * @return boolean + * @return false|array|\WP_Error */ public function pre_http_request( $false, array $args, string $url ) { diff --git a/src/Record/Collector/Plugin_Load_Collector.php b/src/Record/Collector/Plugin_Load_Collector.php index d2cd8ff..60fca42 100644 --- a/src/Record/Collector/Plugin_Load_Collector.php +++ b/src/Record/Collector/Plugin_Load_Collector.php @@ -54,10 +54,8 @@ class Plugin_Load_Collector implements Record_Collector_Interface { * Converts recorded information into a Record instance. * * @param array $entry Data entry for the plugin. - * - * @return Record_Interface */ - private function create_record( array $entry ): Record_Interface { + private function create_record( array $entry ): Record { return new Record( plugin_basename( $entry['name'] ), $entry['origin'], diff --git a/src/Record/Collector/Sql_Collector.php b/src/Record/Collector/Sql_Collector.php index 511ff4d..3d6293a 100644 --- a/src/Record/Collector/Sql_Collector.php +++ b/src/Record/Collector/Sql_Collector.php @@ -8,6 +8,8 @@ use Rarst\Laps\Record\Record; /** * Processes SQL events from data logged by wpdb. + * + * @template QueryData as array{0: string, 1: float, 2: string, 3: float} */ class Sql_Collector implements Record_Collector_Interface { @@ -59,11 +61,13 @@ class Sql_Collector implements Record_Collector_Interface { global $wpdb; + /** @psalm-suppress MixedPropertyFetch */ if ( empty( $wpdb->queries ) ) { return []; } /** @var array $wpdb->queries */ + /** @psalm-var array $wpdb->queries */ $records = array_filter( array_map( [ $this, 'transform' ], array_keys( $wpdb->queries ), $wpdb->queries ) ); return $records; diff --git a/src/Record/Collector/Stopwatch_Collector.php b/src/Record/Collector/Stopwatch_Collector.php index cbebdc0..7f4a240 100644 --- a/src/Record/Collector/Stopwatch_Collector.php +++ b/src/Record/Collector/Stopwatch_Collector.php @@ -51,6 +51,7 @@ class Stopwatch_Collector implements Record_Collector_Interface { */ public function get_records(): array { + /** @psalm-var array $events */ $events = $this->stopwatch->getSectionEvents( '__root__' ); /** @var string $name */