From 1aac154bcb5321a1f8cdb64fd3fbc0e106f72418 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 2 Feb 2024 11:33:11 +0100 Subject: [PATCH] improve code quality --- composer.json | 8 +++++++- phpstan.neon | 14 ++++++++++++++ src/Extensions/d3FirePHP.php | 2 +- src/LoggerFactory.php | 14 ++++++++------ src/OxidEsalesDatabase.php | 19 +++++++++++-------- src/OxidSQLLogger.php | 3 ++- src/SQLQuery.php | 18 +++++++++--------- src/functions.php | 10 ++++++---- 8 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 6dd06e9..3fa6538 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,9 @@ "nilportugues/sql-query-formatter": "^1.2.2", "firephp/firephp-core": "^0.5.3" }, + "require-dev": { + "phpstan/phpstan": "^1.10" + }, "license": "GPL-3.0", "autoload": { "psr-4": { @@ -27,5 +30,8 @@ "email": "info@shopmodule.com", "homepage": "https://www.d3data.de" } - ] + ], + "scripts": { + "phpstan": "./vendor/bin/phpstan --configuration=vendor/d3/oxid-sql-logger/phpstan.neon analyse" + } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..7c47f35 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + scanFiles: + - ../../oxid-esales/oxideshop-ce/source/bootstrap.php + - ../../oxid-esales/oxideshop-ce/source/oxfunctions.php + - ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php + paths: + - ./src + - ./example + level: 9 + phpVersion: 80300 + checkMissingIterableValueType: false + treatPhpDocTypesAsCertain: false + featureToggles: + disableRuntimeReflectionProvider: true diff --git a/src/Extensions/d3FirePHP.php b/src/Extensions/d3FirePHP.php index de7251d..79ea60d 100644 --- a/src/Extensions/d3FirePHP.php +++ b/src/Extensions/d3FirePHP.php @@ -68,7 +68,7 @@ class d3FirePHP extends FirePHP * * @see FirePHP::TRACE * @param string $label - * @return true + * @return bool * @throws Exception */ public function trace($label): bool diff --git a/src/LoggerFactory.php b/src/LoggerFactory.php index 8cb8f4c..af192ec 100644 --- a/src/LoggerFactory.php +++ b/src/LoggerFactory.php @@ -10,16 +10,18 @@ declare(strict_types=1); namespace D3\OxidSqlLogger; use Monolog; +use Monolog\Logger; use OxidEsales\Eshop\Core\Registry; use Symfony\Component\Console\Formatter\OutputFormatterStyle; class LoggerFactory { /** - * @param $name - * @return Monolog\Logger + * @param string $name + * + * @return Logger */ - public function create($name): Monolog\Logger + public function create(string $name): Monolog\Logger { return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors()); } @@ -52,17 +54,17 @@ class LoggerFactory } /** - * @param array $classNames + * @param iterable $classNames * * @return array */ - private function getInstancesFromHandlerList(array $classNames): array + private function getInstancesFromHandlerList(iterable $classNames): array { return array_map( function($className){ return new $className(); }, - $classNames + (array) $classNames ); } diff --git a/src/OxidEsalesDatabase.php b/src/OxidEsalesDatabase.php index 5bd993b..acab1a3 100644 --- a/src/OxidEsalesDatabase.php +++ b/src/OxidEsalesDatabase.php @@ -13,6 +13,7 @@ use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Logging\SQLLogger; use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProvider; use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -32,10 +33,10 @@ class OxidEsalesDatabase extends Database $this->d3GetConfiguration()->setSQLLogger( new OxidSQLLogger( - $trace[1]['file'] ?? null, - (int) $trace[1]['line'] ?? null, - $trace[2]['class'] ?? null, - $trace[2]['function'] ?? null, + $trace[1]['file'] ?? '', + array_key_exists('line', $trace[1]) ? (int) $trace[1]['line'] : 0, + $trace[2]['class'] ?? '', + $trace[2]['function'] ?? '', $message ) ); @@ -61,13 +62,15 @@ class OxidEsalesDatabase extends Database } /** - * @return Configuration|null + * @return Configuration * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - protected function d3GetConfiguration(): ?Configuration + protected function d3GetConfiguration(): Configuration { - return ContainerFactory::getInstance()->getContainer() - ->get(ConnectionProviderInterface::class)->get()->getConfiguration(); + /** @var ConnectionProvider $connectionProvider */ + $connectionProvider = ContainerFactory::getInstance()->getContainer() + ->get(ConnectionProviderInterface::class); + return $connectionProvider->get()->getConfiguration(); } } diff --git a/src/OxidSQLLogger.php b/src/OxidSQLLogger.php index c6c6b69..48a3a53 100644 --- a/src/OxidSQLLogger.php +++ b/src/OxidSQLLogger.php @@ -37,7 +37,7 @@ class OxidSQLLogger implements SQLLogger Monolog\Registry::addLogger((new LoggerFactory())->create('sql')); } - $this->message = $message; + $this->message = (string) $message; $this->logStartingFile = $file; $this->logStartingLine = $line; $this->logStartingClass = $class; @@ -92,6 +92,7 @@ class OxidSQLLogger implements SQLLogger if ($this->SQLQuery) { $formatter = new Formatter(); + // @phpstan-ignore-next-line Monolog\Registry::sql()->addDebug( '['.$this->SQLQuery->getReadableElapsedTime().'] ' . ($this->message ?: $this->SQLQuery->getSql()), [ diff --git a/src/SQLQuery.php b/src/SQLQuery.php index 5866612..2ae5207 100644 --- a/src/SQLQuery.php +++ b/src/SQLQuery.php @@ -53,7 +53,7 @@ class SQLQuery /** * @param string $sql - * @return SQLQuery + * @return static */ public function setSql(string $sql): static { @@ -72,7 +72,7 @@ class SQLQuery /** * @param array|null $params * - * @return SQLQuery + * @return static */ public function setParams(array $params = null): static { @@ -91,7 +91,7 @@ class SQLQuery /** * @param array|null $types * - * @return SQLQuery + * @return static */ public function setTypes(array $types = null): static { @@ -109,7 +109,7 @@ class SQLQuery /** * @param string $file - * @return SQLQuery + * @return static */ public function setLogStartingFile(string $file): static { @@ -125,7 +125,7 @@ class SQLQuery /** * @param int $line * - * @return SQLQuery + * @return static */ public function setLogStartingLine(int $line): static { @@ -144,7 +144,7 @@ class SQLQuery /** * @param string $classname * - * @return SQLQuery + * @return static */ public function setLogStartingClass(string $classname): static { @@ -163,7 +163,7 @@ class SQLQuery /** * @param string $functionname * - * @return SQLQuery + * @return static */ public function setLogStartingFunction(string $functionname): static { @@ -174,7 +174,7 @@ class SQLQuery /** * Statement was cancelled prematurely, an error was thrown. * - * @return SQLQuery + * @return static */ public function setCanceled(): static { @@ -227,7 +227,7 @@ class SQLQuery $unit = 'ms'; $time = round($microtime*1000); - $format = preg_replace('/(%.\d+f)/', '%d', $format); + $format = (string) preg_replace('/(%.\d+f)/', '%d', $format); } return sprintf($format, $time, $unit); diff --git a/src/functions.php b/src/functions.php index 9b72d4a..4483df4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -36,15 +36,17 @@ function D3StopSQLLog(): void } /** - * @param $message + * @param string $message * * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ -function D3AddSQLLogItem($message): void +function D3AddSQLLogItem(string $message): void { /** @var OxidEsalesDatabase $database */ $database = oxNew( OxidEsalesDatabase::class); - $database->d3GetLogger()->startQuery($message); - $database->d3GetLogger()->stopQuery(); + if ($logger = $database->d3GetLogger()) { + $logger->startQuery( $message ); + $logger->stopQuery(); + } }