From 3a1d3c8c66227cf72a19abc5cb136b7d20dc0676 Mon Sep 17 00:00:00 2001 From: Aleksei Khudiakov Date: Tue, 5 Dec 2023 10:46:35 +1000 Subject: [PATCH] Fix type errors reported by psalm. Fix some psalm issues and Update baselined Signed-off-by: Aleksei Khudiakov --- psalm-baseline.xml | 68 +++++++++++--------------- src/Collector/AbstractCollector.php | 15 ++++-- src/Collector/ConfigCollector.php | 21 +++++--- src/Collector/DbCollector.php | 32 +++++++----- src/Options.php | 1 + test/Collector/ConfigCollectorTest.php | 62 ++++++++++++++++++++--- 6 files changed, 130 insertions(+), 69 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 988b6a6..5e09beb 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,6 +1,10 @@ + + serialize + unserialize + $data $data @@ -9,7 +13,7 @@ $data - data]]> + $data @@ -21,6 +25,10 @@ getApplication()]]> + + serialize + unserialize + collect @@ -28,21 +36,14 @@ get('ApplicationConfig')]]> get('config')]]> - - - - $serializable[$key] $serializable[$key] $serializable[$key] - $data $key $serializable[$key] - applicationConfig]]> - config]]> $unserialized[$key] $value $value @@ -58,21 +59,12 @@ $value - - getApplicationConfig - getConfig - - - profiler->getQueryProfiles($mode))]]> - - - self - - - $profiler - + + serialize + unserialize + collect @@ -81,7 +73,6 @@ $query - profiler]]> $time @@ -97,6 +88,9 @@ $time + + getQueryProfiles + getProfiler getQueryTime @@ -106,17 +100,15 @@ $profiler - - profiler)]]> - Profiler + Profiler profiler]]> profiler]]> - Profiler - Profiler + null|Profiler + null|Profiler @@ -257,7 +249,6 @@ collect - $child $controller $controller data['action']]]> @@ -266,7 +257,6 @@ $views[] - $child $controller $var @@ -406,6 +396,10 @@ + + serialize + unserialize + @@ -734,6 +728,9 @@ setProfiler setToolbar + + Options + @@ -741,6 +738,9 @@ $value $value + + $options + events['collectors'][$name]]]> events['identifiers'][$name]]]> @@ -796,9 +796,6 @@ toolbar[$key][$collector]]]> - - $options - canFlushEarly eventCollectionEnabled @@ -807,10 +804,6 @@ setEvents setProfiler - - $options - $report - is_array($options) is_array($options) @@ -825,9 +818,6 @@ TimeCollector TimeCollector - - array|Traversable|null - diff --git a/src/Collector/AbstractCollector.php b/src/Collector/AbstractCollector.php index 0883f75..0f8d13c 100644 --- a/src/Collector/AbstractCollector.php +++ b/src/Collector/AbstractCollector.php @@ -6,6 +6,8 @@ use Serializable; +use function assert; +use function is_array; use function serialize; use function unserialize; @@ -22,11 +24,11 @@ abstract class AbstractCollector implements CollectorInterface, Serializable protected $data; /** - * @return string + * @return array */ public function __serialize() { - return serialize($this->data); + return ['data' => $this->data]; } /** @@ -37,16 +39,17 @@ public function __serialize() */ public function serialize() { - return $this->__serialize(); + return serialize($this->__serialize()); } /** - * @param string $data + * @param array $data * @return void */ public function __unserialize($data) { - $this->data = unserialize($data); + assert(isset($data['data']) && is_array($data['data'])); + $this->data = $data['data']; } /** @@ -57,6 +60,8 @@ public function __unserialize($data) */ public function unserialize($data) { + $data = unserialize($data); + assert(is_array($data)); $this->__unserialize($data); } } diff --git a/src/Collector/ConfigCollector.php b/src/Collector/ConfigCollector.php index 7e9cd3e..c6a50ef 100644 --- a/src/Collector/ConfigCollector.php +++ b/src/Collector/ConfigCollector.php @@ -11,6 +11,8 @@ use Serializable; use Traversable; +use function array_key_exists; +use function assert; use function is_array; use function serialize; use function unserialize; @@ -82,11 +84,11 @@ public function getApplicationConfig() } /** - * @return string + * @return array */ public function __serialize() { - return serialize(['config' => $this->config, 'applicationConfig' => $this->applicationConfig]); + return ['config' => $this->config, 'applicationConfig' => $this->applicationConfig]; } /** @@ -97,16 +99,19 @@ public function __serialize() */ public function serialize() { - return $this->__serialize(); + return serialize($this->__serialize()); } /** - * @param string $serialized + * @param array $data * @return void */ - public function __unserialize($serialized) + public function __unserialize($data) { - $data = unserialize($serialized); + assert(array_key_exists('config', $data)); + assert($data['config'] === null || is_array($data['config'])); + assert(array_key_exists('applicationConfig', $data)); + assert($data['applicationConfig'] === null || is_array($data['applicationConfig'])); $this->config = $data['config']; $this->applicationConfig = $data['applicationConfig']; } @@ -119,7 +124,9 @@ public function __unserialize($serialized) */ public function unserialize($serialized) { - $this->__unserialize($serialized); + $data = unserialize($serialized); + assert(is_array($data)); + $this->__unserialize($data); } /** diff --git a/src/Collector/DbCollector.php b/src/Collector/DbCollector.php index 7c0e8c4..05d676f 100644 --- a/src/Collector/DbCollector.php +++ b/src/Collector/DbCollector.php @@ -8,7 +8,10 @@ use Laminas\Mvc\MvcEvent; use Serializable; +use function array_key_exists; +use function assert; use function count; +use function is_array; use function serialize; use function unserialize; @@ -17,7 +20,7 @@ */ class DbCollector implements CollectorInterface, AutoHideInterface, Serializable { - /** @var Profiler */ + /** @var null|Profiler */ protected $profiler; /** @@ -72,7 +75,7 @@ public function hasProfiler() /** * Returns Bjy's Db Profiler * - * @return Profiler + * @return null|Profiler */ public function getProfiler() { @@ -97,10 +100,13 @@ public function setProfiler(Profiler $profiler) * what kind of queries you want to get, e.g. Profiler::INSERT. * * @param integer|null $mode - * @return self + * @return null|integer */ public function getQueryCount($mode = null) { + if ($this->profiler === null) { + return null; + } return count($this->profiler->getQueryProfiles($mode)); } @@ -125,11 +131,11 @@ public function getQueryTime($mode = null) } /** - * @return string + * @return array */ public function __serialize() { - return serialize($this->profiler); + return ['profiler' => $this->profiler]; } /** @@ -140,16 +146,18 @@ public function __serialize() */ public function serialize() { - return $this->__serialize(); + return serialize($this->__serialize()); } /** - * @param string $profiler + * @param array $data * @return void */ - public function __unserialize($profiler) + public function __unserialize($data) { - $this->profiler = unserialize($profiler); + assert(array_key_exists('profiler', $data)); + assert($data['profiler'] === null || $data['profiler'] instanceof Profiler); + $this->profiler = $data['profiler']; } /** @@ -158,8 +166,10 @@ public function __unserialize($profiler) * * @inheritDoc */ - public function unserialize($profiler) + public function unserialize($serialized) { - $this->__unserialize($profiler); + $data = unserialize($serialized); + assert(is_array($data)); + $this->__unserialize($data); } } diff --git a/src/Options.php b/src/Options.php index 0527d4c..129779d 100644 --- a/src/Options.php +++ b/src/Options.php @@ -6,6 +6,7 @@ use Laminas\Stdlib\AbstractOptions; use Laminas\Stdlib\Exception\InvalidArgumentException; +use Traversable; use function gettype; use function is_array; diff --git a/test/Collector/ConfigCollectorTest.php b/test/Collector/ConfigCollectorTest.php index fdf8a11..56efa35 100644 --- a/test/Collector/ConfigCollectorTest.php +++ b/test/Collector/ConfigCollectorTest.php @@ -5,32 +5,80 @@ namespace LaminasTest\DeveloperTools\Collector; use Laminas\DeveloperTools\Collector\ConfigCollector; -use Laminas\Mvc; -use Laminas\ServiceManager; +use Laminas\Mvc\Application; +use Laminas\Mvc\MvcEvent; +use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\TestCase; +use function serialize; +use function unserialize; + class ConfigCollectorTest extends TestCase { public function testCollect(): void { $collector = new ConfigCollector(); - $application = $this->getMockBuilder(Mvc\Application::class) + $config = ['main' => 'config']; + $applicationConfig = ['main' => 'config']; + + $serviceManager = new ServiceManager([ + 'services' => [ + 'config' => $config, + 'ApplicationConfig' => $applicationConfig, + ], + ]); + + $application = $this->getMockBuilder(Application::class) ->disableOriginalConstructor() ->getMock(); - $serviceManager = $this->getMockBuilder(ServiceManager\ServiceManager::class) - ->disableOriginalConstructor() + $application + ->expects($this->once()) + ->method("getServiceManager") + ->willReturn($serviceManager); + $mvcEvent = $this->getMockBuilder(MvcEvent::class) ->getMock(); + $mvcEvent->method("getApplication")->willReturn($application); + + $collector->collect($mvcEvent); + + self::assertEqualsCanonicalizing($config, $collector->getConfig()); + self::assertEqualsCanonicalizing($applicationConfig, $collector->getApplicationConfig()); + } + + public function testSerialize(): void + { + $collector = new ConfigCollector(); + + $config = ['main' => 'config']; + $applicationConfig = ['main' => 'config']; + + $serviceManager = new ServiceManager([ + 'services' => [ + 'config' => $config, + 'ApplicationConfig' => $applicationConfig, + ], + ]); + $application = $this->getMockBuilder(Application::class) + ->disableOriginalConstructor() + ->getMock(); $application ->expects($this->once()) ->method("getServiceManager") ->willReturn($serviceManager); - $mvcEvent = $this->getMockBuilder(Mvc\MvcEvent::class) + $mvcEvent = $this->getMockBuilder(MvcEvent::class) ->getMock(); - $mvcEvent->method("getApplication")->willReturn($application); $collector->collect($mvcEvent); + + $serialized = serialize($collector); + $unserialized = unserialize($serialized); + + self::assertInstanceOf(ConfigCollector::class, $unserialized); + + self::assertEqualsCanonicalizing($config, $collector->getConfig()); + self::assertEqualsCanonicalizing($applicationConfig, $collector->getApplicationConfig()); } }