From 204c1df755b01950427e6fa3469a97965653967e Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Fri, 30 Nov 2018 13:58:53 +0200 Subject: [PATCH] Raised PHP requirement to 7.1 and updated types. Fixes #25 --- composer.json | 2 +- src/Event/Core_Events.php | 3 ++- src/Event/Genesis_Events.php | 5 +++-- src/Event/Hook_Event_Config_Interface.php | 6 +++++- src/Event/Hybrid_Events.php | 5 +++-- src/Event/THA_Events.php | 5 +++-- src/Event/Yoast_Events.php | 3 ++- src/Formatter/Backtrace_Formatter.php | 9 +++++---- src/Manager/Asset_Manager.php | 5 +++-- src/Manager/Load_Order_Manager.php | 5 +++-- src/Manager/Toolbar_Manager.php | 3 ++- src/Mustache_Cache_FrozenCache.php | 4 +++- src/Plugin.php | 7 ++++--- src/Provider/Bootable_Provider_Interface.php | 6 +++++- src/Provider/Hook_Event_Provider.php | 3 ++- src/Provider/Manager_Provider.php | 5 +++-- src/Provider/Record_Provider.php | 5 +++-- src/Record/Core_Load_Collector.php | 5 +++-- src/Record/Hook_Record_Collector.php | 9 +++++---- src/Record/Http_Record_Collector.php | 5 +++-- src/Record/Record.php | 13 +++++++------ src/Record/Record_Collector_Interface.php | 6 +++++- src/Record/Record_Interface.php | 11 ++++++----- src/Record/Recursive_Record_Iterator.php | 7 ++++--- src/Record/Sql_Record_Collector.php | 15 ++++++++------- src/Record/Stopwatch_Record.php | 13 +++++++------ src/Record/Stopwatch_Record_Collector.php | 7 ++++--- src/Record/Timeline_Iterator.php | 15 ++++++++------- tests/Record/CoreTest.php | 3 +++ 29 files changed, 115 insertions(+), 75 deletions(-) diff --git a/composer.json b/composer.json index b4d15b3..8d51c63 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } }, "require" : { - "php" : ">=5.6", + "php" : "^7.1.3", "composer/installers": "~1.0", "symfony/stopwatch" : "^3.4.4", "mustache/mustache" : "~2.4", diff --git a/src/Event/Core_Events.php b/src/Event/Core_Events.php index 04c5889..9830b86 100644 --- a/src/Event/Core_Events.php +++ b/src/Event/Core_Events.php @@ -1,4 +1,5 @@ 0 ) { diff --git a/src/Manager/Toolbar_Manager.php b/src/Manager/Toolbar_Manager.php index 49def5f..073630c 100644 --- a/src/Manager/Toolbar_Manager.php +++ b/src/Manager/Toolbar_Manager.php @@ -1,4 +1,5 @@ log( \Mustache_Logger::WARNING, diff --git a/src/Plugin.php b/src/Plugin.php index b638fd2..48c874d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,4 +1,5 @@ providers[] = $provider; @@ -60,9 +61,9 @@ class Plugin extends Container { /** * Start Stopwatch and timing plugin load immediately, then set up core events and needed hooks. */ - public function run() { + public function run(): void { - if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { + if ( \defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; } diff --git a/src/Provider/Bootable_Provider_Interface.php b/src/Provider/Bootable_Provider_Interface.php index f0e99cc..22b419b 100644 --- a/src/Provider/Bootable_Provider_Interface.php +++ b/src/Provider/Bootable_Provider_Interface.php @@ -1,9 +1,13 @@ factory( function () { return new Stopwatch( true ); @@ -51,7 +52,7 @@ class Record_Provider implements ServiceProviderInterface, Bootable_Provider_Int /** * @param Plugin $laps Container instance. */ - public function boot( Plugin $laps ) { + public function boot( Plugin $laps ): void { $laps['collectors']; } } diff --git a/src/Record/Core_Load_Collector.php b/src/Record/Core_Load_Collector.php index 12bab35..e39c41c 100644 --- a/src/Record/Core_Load_Collector.php +++ b/src/Record/Core_Load_Collector.php @@ -1,4 +1,5 @@ events = array_merge( $this->events, $stops ); @@ -51,7 +52,7 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector { /** * When theme is done possibly add vendor-specific events. */ - public function after_setup_theme() { + public function after_setup_theme(): void { foreach ( $this->event_configs as $config ) { $this->add_events( $config->get_events() ); @@ -78,7 +79,7 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector { return $input; } - $event = wp_parse_args( $this->events[ $filter_name ][ $priority ], [ + $event = \wp_parse_args( $this->events[ $filter_name ][ $priority ], [ 'action' => 'start', 'category' => null, ] ); @@ -95,7 +96,7 @@ class Hook_Record_Collector extends Stopwatch_Record_Collector { /** * @return Stopwatch_Record[] */ - public function get_records() { + public function get_records(): array { $this->stopwatch->stop( 'Toolbar' ); diff --git a/src/Record/Http_Record_Collector.php b/src/Record/Http_Record_Collector.php index d8c81b3..6176013 100644 --- a/src/Record/Http_Record_Collector.php +++ b/src/Record/Http_Record_Collector.php @@ -1,4 +1,5 @@ start( $url, 'http' ); @@ -49,7 +50,7 @@ class Http_Record_Collector extends Stopwatch_Record_Collector { * * @noinspection MoreThanThreeArgumentsInspection */ - public function http_api_debug( $response, $type, $class, $args, $url ) { + public function http_api_debug( $response, string $type, $class, array $args, string $url ) { $this->stop( $url ); diff --git a/src/Record/Record.php b/src/Record/Record.php index 0dd58cb..4f3c575 100644 --- a/src/Record/Record.php +++ b/src/Record/Record.php @@ -1,4 +1,5 @@ name = $name; $this->origin = $origin; $this->duration = $duration; @@ -40,14 +41,14 @@ class Record implements Record_Interface { /** * @return string */ - public function get_name() { + public function get_name(): string { return $this->name; } /** * @return string */ - public function get_description() { + public function get_description(): string { if ( empty( $this->description ) ) { $duration = round( $this->get_duration() * 1000 ); @@ -61,21 +62,21 @@ class Record implements Record_Interface { /** * @return float */ - public function get_origin() { + public function get_origin(): float { return $this->origin; } /** * @return float */ - public function get_duration() { + public function get_duration(): float { return $this->duration; } /** * @return string */ - public function get_category() { + public function get_category(): string { return $this->category; } } diff --git a/src/Record/Record_Collector_Interface.php b/src/Record/Record_Collector_Interface.php index 03dfaa7..f4854cb 100644 --- a/src/Record/Record_Collector_Interface.php +++ b/src/Record/Record_Collector_Interface.php @@ -1,11 +1,15 @@ get_origin(); $origin_b = $record_b->get_origin(); @@ -58,7 +59,7 @@ class Recursive_Record_Iterator extends \ArrayIterator implements \RecursiveIter /** * @return bool */ - public function hasChildren() { + public function hasChildren(): bool { return ! empty( $this->children ); } diff --git a/src/Record/Sql_Record_Collector.php b/src/Record/Sql_Record_Collector.php index 9312471..2ba0fa6 100644 --- a/src/Record/Sql_Record_Collector.php +++ b/src/Record/Sql_Record_Collector.php @@ -1,4 +1,5 @@ is_savequeries() ) { return []; @@ -64,9 +65,9 @@ class Sql_Record_Collector implements Record_Collector_Interface { /** * @return bool */ - protected function is_savequeries() { + protected function is_savequeries(): bool { - return defined( 'SAVEQUERIES' ) && SAVEQUERIES; + return \defined( 'SAVEQUERIES' ) && SAVEQUERIES; } /** @@ -77,13 +78,13 @@ class Sql_Record_Collector implements Record_Collector_Interface { * * @return Record */ - protected function transform( $key, $query_data ) { + protected function transform( int $key, array $query_data ): Record { static $last_query_end = 0; - list( $sql, $duration, $caller ) = $query_data; + [ $sql, $duration, $caller ] = $query_data; - $query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end; + $query_start = $this->query_starts[ $key ] ?? $last_query_end; $sql = trim( $sql ); $category = 'sql-read'; if ( 0 === stripos( $sql, 'INSERT' ) || 0 === stripos( $sql, 'UPDATE' ) ) { diff --git a/src/Record/Stopwatch_Record.php b/src/Record/Stopwatch_Record.php index d954ac9..5c18e4b 100644 --- a/src/Record/Stopwatch_Record.php +++ b/src/Record/Stopwatch_Record.php @@ -1,4 +1,5 @@ name = $name; $this->stopwatch_event = $stopwatch_event; } @@ -27,14 +28,14 @@ class Stopwatch_Record implements Record_Interface { /** * @return string */ - public function get_name() { + public function get_name(): string { return $this->name; } /** * @return string */ - public function get_description() { + public function get_description(): string { $duration = round( $this->stopwatch_event->getDuration() ); $memory = $this->stopwatch_event->getMemory() / 1024 / 1024; @@ -45,21 +46,21 @@ class Stopwatch_Record implements Record_Interface { /** * @return float Timestamp of record start. */ - public function get_origin() { + public function get_origin(): float { return $this->stopwatch_event->getOrigin() / 1000; // ms to s. } /** * @return float Record duration in seconds. */ - public function get_duration() { + public function get_duration(): float { return $this->stopwatch_event->getDuration() / 1000; // ms to s. } /** * @return string */ - public function get_category() { + public function get_category(): string { return $this->stopwatch_event->getCategory(); } } diff --git a/src/Record/Stopwatch_Record_Collector.php b/src/Record/Stopwatch_Record_Collector.php index 94a6d75..91ac9e2 100644 --- a/src/Record/Stopwatch_Record_Collector.php +++ b/src/Record/Stopwatch_Record_Collector.php @@ -1,4 +1,5 @@ stopwatch->start( $name, $category ); } @@ -47,7 +48,7 @@ class Stopwatch_Record_Collector implements Record_Collector_Interface { /** * @return Stopwatch_Record[] */ - public function get_records() { + public function get_records(): array { $events = $this->stopwatch->getSectionEvents( '__root__' ); @@ -66,7 +67,7 @@ class Stopwatch_Record_Collector implements Record_Collector_Interface { * * @return Stopwatch_Record */ - protected function transform( $name, StopwatchEvent $event ) { + protected function transform( string $name, StopwatchEvent $event ): Stopwatch_Record { return new Stopwatch_Record( $name, $event ); } diff --git a/src/Record/Timeline_Iterator.php b/src/Record/Timeline_Iterator.php index 143ddd9..213f2c7 100644 --- a/src/Record/Timeline_Iterator.php +++ b/src/Record/Timeline_Iterator.php @@ -1,4 +1,5 @@ origin = $_SERVER['REQUEST_TIME_FLOAT']; + $this->origin = filter_var( $_SERVER['REQUEST_TIME_FLOAT'], FILTER_VALIDATE_FLOAT ); $this->iterator = $iterator; } /** * @return array */ - public function current() { + public function current(): array { $data = []; @@ -45,7 +46,7 @@ class Timeline_Iterator implements \Iterator { /** * Sets current context to the next set of nested records. */ - public function next() { + public function next(): void { $this->current = $this->current->getChildren(); } @@ -54,21 +55,21 @@ class Timeline_Iterator implements \Iterator { * * @return void */ - public function key() { + public function key(): void { } /** * @return bool If current set contains records. */ - public function valid() { + public function valid(): bool { return (bool) count( $this->current ); } /** * Rewind to a topmost record set. */ - public function rewind() { + public function rewind(): void { $this->total = microtime( true ) - $this->origin; $this->current = $this->iterator; } @@ -78,7 +79,7 @@ class Timeline_Iterator implements \Iterator { * * @return array Record data for display by the template. */ - protected function prepare( Record_Interface $record ) { + protected function prepare( Record_Interface $record ): array { $data = [ 'description' => $record->get_description(), diff --git a/tests/Record/CoreTest.php b/tests/Record/CoreTest.php index b99f141..d549467 100644 --- a/tests/Record/CoreTest.php +++ b/tests/Record/CoreTest.php @@ -10,6 +10,9 @@ class CoreTest extends LapsTestCase { public function testCollector() { + global $timestart; + $timestart = microtime( true );; + $collector = new Core_Load_Collector(); $records = $collector->get_records();