From 1ff5ec3f39b79ff5aa3716c60660270b2e2b23ab Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 2 Mar 2024 22:02:46 +0100 Subject: [PATCH 1/2] Fix PHPStan L7 --- src/Controller/ComposerController.php | 2 +- src/Controller/MailPreviewController.php | 5 ++++- src/DebugInclude.php | 2 +- src/DebugSql.php | 1 + src/Mailer/Transport/DebugKitTransport.php | 6 +++++- src/Model/Behavior/TimedBehavior.php | 12 +++++++++--- src/Model/Entity/Panel.php | 4 ++-- src/Panel/SessionPanel.php | 14 +++++++------- src/Panel/SqlLogPanel.php | 3 ++- src/ToolbarService.php | 2 +- 10 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Controller/ComposerController.php b/src/Controller/ComposerController.php index b5cae8e6..edca252c 100644 --- a/src/Controller/ComposerController.php +++ b/src/Controller/ComposerController.php @@ -85,7 +85,7 @@ private function executeComposerCommand(ArrayInput $input): BufferedOutput putenv('COMPOSER_HOME=' . $bin); putenv('COMPOSER_CACHE_DIR=' . CACHE); - $dir = getcwd(); + $dir = (string)getcwd(); chdir(ROOT); $timeLimit = ini_get('max_execution_time'); set_time_limit(300); diff --git a/src/Controller/MailPreviewController.php b/src/Controller/MailPreviewController.php index 8f7116c6..4f00e595 100644 --- a/src/Controller/MailPreviewController.php +++ b/src/Controller/MailPreviewController.php @@ -203,7 +203,9 @@ protected function getMailPreviewClasses(): CollectionInterface } }) ->unfold(function ($path, $plugin) { - foreach (glob($path . '*Preview.php') as $file) { + /** @var list $files */ + $files = glob($path . '*Preview.php'); + foreach ($files as $file) { $base = str_replace('.php', '', basename($file)); $class = App::className($plugin . $base, 'Mailer/Preview'); if ($class) { @@ -271,6 +273,7 @@ protected function findPreview(string $previewName, string $emailName, string $p $plugin = "$plugin."; } + /** @var \DebugKit\Mailer\MailPreview $realClass */ $realClass = App::className($plugin . $previewName, 'Mailer/Preview'); if (!$realClass) { throw new NotFoundException("Mailer preview $previewName not found"); diff --git a/src/DebugInclude.php b/src/DebugInclude.php index a546f561..944c5308 100644 --- a/src/DebugInclude.php +++ b/src/DebugInclude.php @@ -82,7 +82,7 @@ public function __construct() */ public function includePaths(): array { - $paths = explode(PATH_SEPARATOR, get_include_path()); + $paths = explode(PATH_SEPARATOR, (string)get_include_path()); $paths = array_filter($paths, function ($path) { if ($path === '.' || strlen($path) === 0) { return false; diff --git a/src/DebugSql.php b/src/DebugSql.php index 60d3b96f..29bf4a4b 100644 --- a/src/DebugSql.php +++ b/src/DebugSql.php @@ -96,6 +96,7 @@ public static function sql( if (defined('CAKE_CORE_INCLUDE_PATH')) { array_unshift($search, CAKE_CORE_INCLUDE_PATH); } + /** @var string $file */ $file = str_replace($search, '', $file); } diff --git a/src/Mailer/Transport/DebugKitTransport.php b/src/Mailer/Transport/DebugKitTransport.php index 2de8dbc2..6a00151c 100644 --- a/src/Mailer/Transport/DebugKitTransport.php +++ b/src/Mailer/Transport/DebugKitTransport.php @@ -47,6 +47,7 @@ public function __construct(array $config = [], ?AbstractTransport $originalTran $className = false; if (!empty($config['originalClassName'])) { + /** @var class-string<\Cake\Mailer\AbstractTransport> $className */ $className = App::className( $config['originalClassName'], 'Mailer/Transport', @@ -91,7 +92,10 @@ public function send(Message $message): array */ public function __call(string $method, array $args): mixed { - return call_user_func_array([$this->originalTransport, $method], $args); + /** @var callable $callable */ + $callable = [$this->originalTransport, $method]; + + return call_user_func_array($callable, $args); } /** diff --git a/src/Model/Behavior/TimedBehavior.php b/src/Model/Behavior/TimedBehavior.php index f425b2b1..43e64223 100644 --- a/src/Model/Behavior/TimedBehavior.php +++ b/src/Model/Behavior/TimedBehavior.php @@ -34,7 +34,9 @@ class TimedBehavior extends Behavior */ public function beforeFind(EventInterface $event, SelectQuery $query): SelectQuery { - $alias = $event->getSubject()->getAlias(); + /** @var \Cake\Datasource\RepositoryInterface $table */ + $table = $event->getSubject(); + $alias = $table->getAlias(); DebugTimer::start($alias . '_find', $alias . '->find()'); return $query->formatResults(function ($results) use ($alias) { @@ -52,7 +54,9 @@ public function beforeFind(EventInterface $event, SelectQuery $query): SelectQue */ public function beforeSave(EventInterface $event): void { - $alias = $event->getSubject()->getAlias(); + /** @var \Cake\Datasource\RepositoryInterface $table */ + $table = $event->getSubject(); + $alias = $table->getAlias(); DebugTimer::start($alias . '_save', $alias . '->save()'); } @@ -64,7 +68,9 @@ public function beforeSave(EventInterface $event): void */ public function afterSave(EventInterface $event): void { - $alias = $event->getSubject()->getAlias(); + /** @var \Cake\Datasource\RepositoryInterface $table */ + $table = $event->getSubject(); + $alias = $table->getAlias(); DebugTimer::stop($alias . '_save'); } } diff --git a/src/Model/Entity/Panel.php b/src/Model/Entity/Panel.php index 67631492..cae68512 100644 --- a/src/Model/Entity/Panel.php +++ b/src/Model/Entity/Panel.php @@ -31,7 +31,7 @@ class Panel extends Entity /** * Some fields should not be in JSON/array exports. * - * @var array + * @var list */ protected array $_hidden = ['content']; @@ -47,7 +47,7 @@ class Panel extends Entity protected function _getContent(mixed $content): string { if (is_resource($content)) { - return stream_get_contents($content); + return (string)stream_get_contents($content); } return $content; diff --git a/src/Panel/SessionPanel.php b/src/Panel/SessionPanel.php index cb6147f9..ae4a1879 100644 --- a/src/Panel/SessionPanel.php +++ b/src/Panel/SessionPanel.php @@ -32,12 +32,12 @@ class SessionPanel extends DebugPanel */ public function shutdown(EventInterface $event): void { - /** @var \Cake\Http\ServerRequest|null $request */ - $request = $event->getSubject()->getRequest(); - if ($request) { - $maxDepth = Configure::read('DebugKit.maxDepth', 5); - $content = Debugger::exportVarAsNodes($request->getSession()->read(), $maxDepth); - $this->_data = compact('content'); - } + /** @var \Cake\Controller\Controller $controller */ + $controller = $event->getSubject(); + $request = $controller->getRequest(); + + $maxDepth = Configure::read('DebugKit.maxDepth', 5); + $content = Debugger::exportVarAsNodes($request->getSession()->read(), $maxDepth); + $this->_data = compact('content'); } } diff --git a/src/Panel/SqlLogPanel.php b/src/Panel/SqlLogPanel.php index 1d0b2194..4eab8a39 100644 --- a/src/Panel/SqlLogPanel.php +++ b/src/Panel/SqlLogPanel.php @@ -74,7 +74,8 @@ public function initialize(): void } $logger = new DebugLog($logger, $name, $includeSchemaReflection); - $connection->getDriver()->setLogger($logger); + /** @var \Cake\Database\Driver $driver */ + $driver->setLogger($logger); $this->_loggers[] = $logger; } diff --git a/src/ToolbarService.php b/src/ToolbarService.php index 578f22b7..fdb6ca93 100644 --- a/src/ToolbarService.php +++ b/src/ToolbarService.php @@ -143,7 +143,7 @@ public function isEnabled(): bool protected function isSuspiciouslyProduction(): bool { $host = parse_url('http://' . env('HTTP_HOST'), PHP_URL_HOST); - if ($host === false) { + if ($host === false || $host === null) { return false; } From cea45636ddd5f31b155e60be82c82c1aa0fc63ef Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 2 Mar 2024 22:31:30 +0100 Subject: [PATCH 2/2] Fix up psalm --- psalm-baseline.xml | 3 --- src/Controller/MailPreviewController.php | 4 ++-- src/DebugInclude.php | 1 + src/Mailer/Transport/DebugKitTransport.php | 1 + 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 594f1aa3..33f87ec6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -68,9 +68,6 @@ emailLog]]> - - new $className($config) - diff --git a/src/Controller/MailPreviewController.php b/src/Controller/MailPreviewController.php index 4f00e595..5ef5b5d6 100644 --- a/src/Controller/MailPreviewController.php +++ b/src/Controller/MailPreviewController.php @@ -281,9 +281,9 @@ protected function findPreview(string $previewName, string $emailName, string $p $mailPreview = new $realClass(); $email = $mailPreview->find($emailName); - if (!$email) { + if ($email === null) { throw new NotFoundException(sprintf( - 'Mailer preview %s::%s not found', + 'Mailer preview `%s::%s` not found', $previewName, $emailName )); diff --git a/src/DebugInclude.php b/src/DebugInclude.php index 944c5308..1c369eba 100644 --- a/src/DebugInclude.php +++ b/src/DebugInclude.php @@ -82,6 +82,7 @@ public function __construct() */ public function includePaths(): array { + /** @psalm-suppress RedundantCast */ $paths = explode(PATH_SEPARATOR, (string)get_include_path()); $paths = array_filter($paths, function ($path) { if ($path === '.' || strlen($path) === 0) { diff --git a/src/Mailer/Transport/DebugKitTransport.php b/src/Mailer/Transport/DebugKitTransport.php index 6a00151c..5b1d14b4 100644 --- a/src/Mailer/Transport/DebugKitTransport.php +++ b/src/Mailer/Transport/DebugKitTransport.php @@ -57,6 +57,7 @@ public function __construct(array $config = [], ?AbstractTransport $originalTran if ($className) { unset($config['originalClassName'], $config['debugKitLog']); + /** @psalm-suppress UnsafeInstantiation */ $this->originalTransport = new $className($config); } }