From 4b126be068c02526b24dbb5ce3661584719fda55 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 13 Sep 2022 00:47:49 +0700 Subject: [PATCH] Apply PHP 7.4 syntax and typed property Signed-off-by: Abdul Malik Ikhsan --- psalm-baseline.xml | 2 +- src/Collector/ConfigCollector.php | 2 +- src/Collector/RequestCollector.php | 10 ++-- src/EventLogging/EventContextProvider.php | 3 +- .../EventLoggingListenerAggregate.php | 8 +--- src/Listener/ToolbarListener.php | 8 ++-- src/Module.php | 47 +++++++++---------- src/View/Helper/Time.php | 2 +- 8 files changed, 40 insertions(+), 42 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c28623a..ef0ab9c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -523,7 +523,7 @@ $sm - function ($sm) { + static function ($sm) { onLoadModulesPost diff --git a/src/Collector/ConfigCollector.php b/src/Collector/ConfigCollector.php index 5c5e33f..7e9cd3e 100644 --- a/src/Collector/ConfigCollector.php +++ b/src/Collector/ConfigCollector.php @@ -166,7 +166,7 @@ private function unserializeArray(array $data) } if ($value instanceof ClosureStub) { - $unserialized[$key] = function () { + $unserialized[$key] = static function (): void { }; continue; } diff --git a/src/Collector/RequestCollector.php b/src/Collector/RequestCollector.php index 6ba6400..8a42ecf 100644 --- a/src/Collector/RequestCollector.php +++ b/src/Collector/RequestCollector.php @@ -50,7 +50,7 @@ public function collect(MvcEvent $mvcEvent) $match = $mvcEvent->getRouteMatch(); $viewModel = $mvcEvent->getViewModel(); - $addToViewFromModel = function (ModelInterface $child) use (&$views) { + $addToViewFromModel = static function (ModelInterface $child) use (&$views): void { $vars = $child->getVariables(); if ($vars instanceof Variables) { @@ -79,9 +79,11 @@ public function collect(MvcEvent $mvcEvent) 'route' => $match === null ? 'N/A' : $match->getMatchedRouteName(), 'action' => $match === null ? 'N/A' : $match->getParam('action', 'N/A'), 'controller' => $match === null ? 'N/A' : $match->getParam('controller', 'N/A'), - 'other_route_parameters' => $match === null ? 'N/A' : array_filter($match->getParams(), function ($key) { - return ! in_array($key, ['action', 'controller']); - }, ARRAY_FILTER_USE_KEY), + 'other_route_parameters' => $match === null ? 'N/A' : array_filter( + $match->getParams(), + static fn($key) => ! in_array($key, ['action', 'controller']), + ARRAY_FILTER_USE_KEY + ), ]; } diff --git a/src/EventLogging/EventContextProvider.php b/src/EventLogging/EventContextProvider.php index cc15e05..6618ea7 100644 --- a/src/EventLogging/EventContextProvider.php +++ b/src/EventLogging/EventContextProvider.php @@ -28,8 +28,7 @@ class EventContextProvider implements EventContextInterface /** @var EventInterface */ protected $event; - /** @var array */ - private $debugBacktrace = []; + private array $debugBacktrace = []; /** * @param EventInterface|null $event (Optional) The event to provide context to. diff --git a/src/Listener/EventLoggingListenerAggregate.php b/src/Listener/EventLoggingListenerAggregate.php index 88d1265..c5a3180 100644 --- a/src/Listener/EventLoggingListenerAggregate.php +++ b/src/Listener/EventLoggingListenerAggregate.php @@ -34,15 +34,11 @@ class EventLoggingListenerAggregate public function __construct(array $collectors, array $identifiers) { $this->collectors = array_map( - function (CollectorInterface $collector) { - return $collector; - }, + static fn(CollectorInterface $collector) => $collector, $collectors ); $this->identifiers = array_values(array_map( - function ($identifier) { - return (string) $identifier; - }, + static fn($identifier) => (string) $identifier, $identifiers )); } diff --git a/src/Listener/ToolbarListener.php b/src/Listener/ToolbarListener.php index 4ec8523..d304c6d 100644 --- a/src/Listener/ToolbarListener.php +++ b/src/Listener/ToolbarListener.php @@ -151,9 +151,11 @@ protected function injectToolbar(ProfilerEvent $event) $injected = preg_replace('/<\/' . $prepend . '>/i', $style . "\n", $injected, 1); } else { $injected = $isHtml5 - ? (stripos($content, '') !== false - ? preg_replace('/<\/html>/i', $style . $toolbar . $script . "\n", $content, 1) - : '' . $content . $style . $toolbar . $script) + ? ( + stripos($content, '') !== false + ? preg_replace('/<\/html>/i', $style . $toolbar . $script . "\n", $content, 1) + : '' . $content . $style . $toolbar . $script + ) : $content; } diff --git a/src/Module.php b/src/Module.php index 482d6ca..e37cbbb 100644 --- a/src/Module.php +++ b/src/Module.php @@ -5,6 +5,11 @@ namespace Laminas\DeveloperTools; use BjyProfiler\Db\Adapter\ProfilingAdapter; +use Laminas\DeveloperTools\Collector\DbCollector; +use Laminas\DeveloperTools\Listener\EventLoggingListenerAggregate; +use Laminas\DeveloperTools\Options; +use Laminas\DeveloperTools\Profiler; +use Laminas\DeveloperTools\ProfilerEvent; use Laminas\EventManager\EventInterface; use Laminas\ModuleManager\Feature\BootstrapListenerInterface; use Laminas\ModuleManager\Feature\ConfigProviderInterface; @@ -101,7 +106,7 @@ public function onBootstrap(EventInterface $event) if ($options->eventCollectionEnabled()) { $sem = $em->getSharedManager(); - $eventLoggingListener = $sm->get(Listener\EventLoggingListenerAggregate::class); + $eventLoggingListener = $sm->get(EventLoggingListenerAggregate::class); $eventLoggingListener->attachShared($sem); } @@ -172,7 +177,7 @@ public function getServiceConfig() 'ZendDeveloperTools\StorageListener' => 'Laminas\DeveloperTools\StorageListener', 'ZendDeveloperTools\Listener\ToolbarListener' => Listener\ToolbarListener::class, 'ZendDeveloperTools\Listener\ProfilerListener' => Listener\ProfilerListener::class, - 'ZendDeveloperTools\Listener\EventLoggingListenerAggregate' => Listener\EventLoggingListenerAggregate::class, + 'ZendDeveloperTools\Listener\EventLoggingListenerAggregate' => EventLoggingListenerAggregate::class, 'ZendDeveloperTools\DbCollector' => 'Laminas\DeveloperTools\DbCollector', /** phpcs:enable Generic.Files.LineLength */ ], @@ -187,47 +192,41 @@ public function getServiceConfig() 'Laminas\DeveloperTools\FlushListener' => Listener\FlushListener::class, ], 'factories' => [ - Profiler::class => function ($sm) { + Profiler::class => static function ($sm): Profiler { $a = new Profiler($sm->get(Report::class)); $a->setEvent($sm->get('Laminas\DeveloperTools\Event')); return $a; }, - 'Laminas\DeveloperTools\Config' => function ($sm) { + 'Laminas\DeveloperTools\Config' => static function ($sm): Options { $config = $sm->get('Configuration'); $config = $config['laminas-developer-tools'] ?? null; - return new Options($config, $sm->get(Report::class)); }, - 'Laminas\DeveloperTools\Event' => function ($sm) { + 'Laminas\DeveloperTools\Event' => static function ($sm): ProfilerEvent { $event = new ProfilerEvent(); $event->setReport($sm->get(Report::class)); $event->setApplication($sm->get('Application')); - return $event; }, - 'Laminas\DeveloperTools\StorageListener' => function ($sm) { - return new Listener\StorageListener($sm); - }, - Listener\ToolbarListener::class => function ($sm) { - return new Listener\ToolbarListener( - $sm->get('ViewRenderer'), - $sm->get('Laminas\DeveloperTools\Config') - ); - }, - Listener\ProfilerListener::class => function ($sm) { - return new Listener\ProfilerListener($sm, $sm->get('Laminas\DeveloperTools\Config')); - }, - Listener\EventLoggingListenerAggregate::class => function ($sm) { + 'Laminas\DeveloperTools\StorageListener' => static fn($sm) => new Listener\StorageListener($sm), + Listener\ToolbarListener::class => static fn($sm) => new Listener\ToolbarListener( + $sm->get('ViewRenderer'), + $sm->get('Laminas\DeveloperTools\Config') + ), + Listener\ProfilerListener::class => static fn($sm) => new Listener\ProfilerListener( + $sm, + $sm->get('Laminas\DeveloperTools\Config') + ), + EventLoggingListenerAggregate::class => static function ($sm): EventLoggingListenerAggregate { $config = $sm->get('Laminas\DeveloperTools\Config'); - - return new Listener\EventLoggingListenerAggregate( + return new EventLoggingListenerAggregate( array_map([$sm, 'get'], $config->getEventCollectors()), $config->getEventIdentifiers() ); }, - 'Laminas\DeveloperTools\DbCollector' => function ($sm) { + 'Laminas\DeveloperTools\DbCollector' => static function ($sm) { $p = false; - $db = new Collector\DbCollector(); + $db = new DbCollector(); if ($sm->has('Laminas\Db\Adapter\Adapter') && isset($sm->get('config')['db'])) { $adapter = $sm->get('Laminas\Db\Adapter\Adapter'); diff --git a/src/View/Helper/Time.php b/src/View/Helper/Time.php index b4c74a6..04717cf 100644 --- a/src/View/Helper/Time.php +++ b/src/View/Helper/Time.php @@ -31,6 +31,6 @@ public function __invoke($time, $precision = 2) return sprintf('%.' . $precision . 'f ms', $time * 1000); } - return sprintf('%.' . $precision . 'f µs', $time * 1000000); + return sprintf('%.' . $precision . 'f µs', $time * 1_000_000); } }