improve code quality

This commit is contained in:
Daniel Seifert 2024-02-02 11:33:11 +01:00
parent e47817b95d
commit 1aac154bcb
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
8 changed files with 58 additions and 30 deletions

View File

@ -8,6 +8,9 @@
"nilportugues/sql-query-formatter": "^1.2.2", "nilportugues/sql-query-formatter": "^1.2.2",
"firephp/firephp-core": "^0.5.3" "firephp/firephp-core": "^0.5.3"
}, },
"require-dev": {
"phpstan/phpstan": "^1.10"
},
"license": "GPL-3.0", "license": "GPL-3.0",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -27,5 +30,8 @@
"email": "info@shopmodule.com", "email": "info@shopmodule.com",
"homepage": "https://www.d3data.de" "homepage": "https://www.d3data.de"
} }
] ],
"scripts": {
"phpstan": "./vendor/bin/phpstan --configuration=vendor/d3/oxid-sql-logger/phpstan.neon analyse"
}
} }

14
phpstan.neon Normal file
View File

@ -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

View File

@ -68,7 +68,7 @@ class d3FirePHP extends FirePHP
* *
* @see FirePHP::TRACE * @see FirePHP::TRACE
* @param string $label * @param string $label
* @return true * @return bool
* @throws Exception * @throws Exception
*/ */
public function trace($label): bool public function trace($label): bool

View File

@ -10,16 +10,18 @@ declare(strict_types=1);
namespace D3\OxidSqlLogger; namespace D3\OxidSqlLogger;
use Monolog; use Monolog;
use Monolog\Logger;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
class LoggerFactory class LoggerFactory
{ {
/** /**
* @param $name * @param string $name
* @return Monolog\Logger *
* @return Logger
*/ */
public function create($name): Monolog\Logger public function create(string $name): Monolog\Logger
{ {
return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors()); return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors());
} }
@ -52,17 +54,17 @@ class LoggerFactory
} }
/** /**
* @param array $classNames * @param iterable $classNames
* *
* @return array * @return array
*/ */
private function getInstancesFromHandlerList(array $classNames): array private function getInstancesFromHandlerList(iterable $classNames): array
{ {
return array_map( return array_map(
function($className){ function($className){
return new $className(); return new $className();
}, },
$classNames (array) $classNames
); );
} }

View File

@ -13,6 +13,7 @@ use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Logging\SQLLogger;
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database; use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProvider;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface; use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@ -32,10 +33,10 @@ class OxidEsalesDatabase extends Database
$this->d3GetConfiguration()->setSQLLogger( $this->d3GetConfiguration()->setSQLLogger(
new OxidSQLLogger( new OxidSQLLogger(
$trace[1]['file'] ?? null, $trace[1]['file'] ?? '',
(int) $trace[1]['line'] ?? null, array_key_exists('line', $trace[1]) ? (int) $trace[1]['line'] : 0,
$trace[2]['class'] ?? null, $trace[2]['class'] ?? '',
$trace[2]['function'] ?? null, $trace[2]['function'] ?? '',
$message $message
) )
); );
@ -61,13 +62,15 @@ class OxidEsalesDatabase extends Database
} }
/** /**
* @return Configuration|null * @return Configuration
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
protected function d3GetConfiguration(): ?Configuration protected function d3GetConfiguration(): Configuration
{ {
return ContainerFactory::getInstance()->getContainer() /** @var ConnectionProvider $connectionProvider */
->get(ConnectionProviderInterface::class)->get()->getConfiguration(); $connectionProvider = ContainerFactory::getInstance()->getContainer()
->get(ConnectionProviderInterface::class);
return $connectionProvider->get()->getConfiguration();
} }
} }

View File

@ -37,7 +37,7 @@ class OxidSQLLogger implements SQLLogger
Monolog\Registry::addLogger((new LoggerFactory())->create('sql')); Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
} }
$this->message = $message; $this->message = (string) $message;
$this->logStartingFile = $file; $this->logStartingFile = $file;
$this->logStartingLine = $line; $this->logStartingLine = $line;
$this->logStartingClass = $class; $this->logStartingClass = $class;
@ -92,6 +92,7 @@ class OxidSQLLogger implements SQLLogger
if ($this->SQLQuery) { if ($this->SQLQuery) {
$formatter = new Formatter(); $formatter = new Formatter();
// @phpstan-ignore-next-line
Monolog\Registry::sql()->addDebug( Monolog\Registry::sql()->addDebug(
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ($this->message ?: $this->SQLQuery->getSql()), '['.$this->SQLQuery->getReadableElapsedTime().'] ' . ($this->message ?: $this->SQLQuery->getSql()),
[ [

View File

@ -53,7 +53,7 @@ class SQLQuery
/** /**
* @param string $sql * @param string $sql
* @return SQLQuery * @return static
*/ */
public function setSql(string $sql): static public function setSql(string $sql): static
{ {
@ -72,7 +72,7 @@ class SQLQuery
/** /**
* @param array|null $params * @param array|null $params
* *
* @return SQLQuery * @return static
*/ */
public function setParams(array $params = null): static public function setParams(array $params = null): static
{ {
@ -91,7 +91,7 @@ class SQLQuery
/** /**
* @param array|null $types * @param array|null $types
* *
* @return SQLQuery * @return static
*/ */
public function setTypes(array $types = null): static public function setTypes(array $types = null): static
{ {
@ -109,7 +109,7 @@ class SQLQuery
/** /**
* @param string $file * @param string $file
* @return SQLQuery * @return static
*/ */
public function setLogStartingFile(string $file): static public function setLogStartingFile(string $file): static
{ {
@ -125,7 +125,7 @@ class SQLQuery
/** /**
* @param int $line * @param int $line
* *
* @return SQLQuery * @return static
*/ */
public function setLogStartingLine(int $line): static public function setLogStartingLine(int $line): static
{ {
@ -144,7 +144,7 @@ class SQLQuery
/** /**
* @param string $classname * @param string $classname
* *
* @return SQLQuery * @return static
*/ */
public function setLogStartingClass(string $classname): static public function setLogStartingClass(string $classname): static
{ {
@ -163,7 +163,7 @@ class SQLQuery
/** /**
* @param string $functionname * @param string $functionname
* *
* @return SQLQuery * @return static
*/ */
public function setLogStartingFunction(string $functionname): static public function setLogStartingFunction(string $functionname): static
{ {
@ -174,7 +174,7 @@ class SQLQuery
/** /**
* Statement was cancelled prematurely, an error was thrown. * Statement was cancelled prematurely, an error was thrown.
* *
* @return SQLQuery * @return static
*/ */
public function setCanceled(): static public function setCanceled(): static
{ {
@ -227,7 +227,7 @@ class SQLQuery
$unit = 'ms'; $unit = 'ms';
$time = round($microtime*1000); $time = round($microtime*1000);
$format = preg_replace('/(%.\d+f)/', '%d', $format); $format = (string) preg_replace('/(%.\d+f)/', '%d', $format);
} }
return sprintf($format, $time, $unit); return sprintf($format, $time, $unit);

View File

@ -36,15 +36,17 @@ function D3StopSQLLog(): void
} }
/** /**
* @param $message * @param string $message
* *
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
function D3AddSQLLogItem($message): void function D3AddSQLLogItem(string $message): void
{ {
/** @var OxidEsalesDatabase $database */ /** @var OxidEsalesDatabase $database */
$database = oxNew( OxidEsalesDatabase::class); $database = oxNew( OxidEsalesDatabase::class);
$database->d3GetLogger()->startQuery($message); if ($logger = $database->d3GetLogger()) {
$database->d3GetLogger()->stopQuery(); $logger->startQuery( $message );
$logger->stopQuery();
}
} }