Psalm cleanup pass.

This commit is contained in:
Andrey Savchenko committed 2019-06-15 10:11:50 +03:00
1 parent cc1cdc577a
commit 1e9a600096
10 files changed
+58 -37

No files matched your search

+20 -9
View File
@@ -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<Priority, array<Hook>>
*
* @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();
-2
View File
@@ -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 {
+1
View File
@@ -20,6 +20,7 @@ class Plugin extends Container {
/**
* @param array $values Configuration values to apply.
* @psalm-param array<string, mixed> $values
*
* @psalm-suppress MixedArgument
* @psalm-suppress MixedAssignment
+7
View File
@@ -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();
};
}
+15 -3
View File
@@ -20,13 +20,19 @@ class Hook_Collector extends Stopwatch_Collector {
/** @var array $events */
protected $events = [];
/** @var array */
/**
* @var array $callbacks
* @psalm-var array<string, \WP_Hook> $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<string, int> $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<string, \WP_Hook> $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;
}
+5 -2
View File
@@ -16,7 +16,10 @@ class Http_Collector extends Stopwatch_Collector {
/** @var Backtrace_Formatter */
private $formatter;
/** @var array */
/**
* @var array $callers
* @psalm-var array<string, string> $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 ) {
@@ -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'],
+4
View File
@@ -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<int, QueryData> $wpdb->queries */
$records = array_filter( array_map( [ $this, 'transform' ], array_keys( $wpdb->queries ), $wpdb->queries ) );
return $records;
@@ -51,6 +51,7 @@ class Stopwatch_Collector implements Record_Collector_Interface {
*/
public function get_records(): array {
/** @psalm-var array<string, StopwatchEvent> $events */
$events = $this->stopwatch->getSectionEvents( '__root__' );
/** @var string $name */