Skip to content

Commit

Permalink
Use correct quoting (#113)
Browse files Browse the repository at this point in the history
* Add new method to check for valid connection driver

* Set classes to readonly where possible

* Update readonly of editable classes

* Use correct quoting for MySQL/MariaDB queries in LoggerStatement

* Remove unused use statements

* Update types for phpstan

* Remove unused use statements

* Set  for QueryInfoFactory test
  • Loading branch information
froemken authored Aug 9, 2024
1 parent 2d9b8bc commit e86b3d9
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 98 deletions.
25 changes: 6 additions & 19 deletions Classes/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,14 @@
/**
* Controller to show and analyze all queries of a request
*/
class ProfileController
readonly class ProfileController
{
private QueryInformationRepository $queryInformationRepository;

private ModuleTemplateFactory $moduleTemplateFactory;

private ModuleTemplateHelper $moduleTemplateHelper;

private DownloadHelper $downloadHelper;

public function __construct(
QueryInformationRepository $queryInformationRepository,
ModuleTemplateFactory $moduleTemplateFactory,
ModuleTemplateHelper $moduleTemplateHelper,
DownloadHelper $downloadHelper,
) {
$this->queryInformationRepository = $queryInformationRepository;
$this->moduleTemplateFactory = $moduleTemplateFactory;
$this->moduleTemplateHelper = $moduleTemplateHelper;
$this->downloadHelper = $downloadHelper;
}
private QueryInformationRepository $queryInformationRepository,
private ModuleTemplateFactory $moduleTemplateFactory,
private ModuleTemplateHelper $moduleTemplateHelper,
private DownloadHelper $downloadHelper,
) {}

public function listAction(ServerRequestInterface $request): ResponseInterface
{
Expand Down
25 changes: 6 additions & 19 deletions Classes/Controller/QueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,14 @@
/**
* Controller to show results of FTS and filesort
*/
class QueryController
readonly class QueryController
{
protected QueryInformationRepository $queryInformationRepository;

protected ExtConf $extConf;

private ModuleTemplateFactory $moduleTemplateFactory;

private ModuleTemplateHelper $moduleTemplateHelper;

public function __construct(
QueryInformationRepository $profileRepository,
ExtConf $extConf,
ModuleTemplateFactory $moduleTemplateFactory,
ModuleTemplateHelper $moduleTemplateHelper,
) {
$this->queryInformationRepository = $profileRepository;
$this->extConf = $extConf;
$this->moduleTemplateFactory = $moduleTemplateFactory;
$this->moduleTemplateHelper = $moduleTemplateHelper;
}
private QueryInformationRepository $queryInformationRepository,
private ExtConf $extConf,
private ModuleTemplateFactory $moduleTemplateFactory,
private ModuleTemplateHelper $moduleTemplateHelper,
) {}

public function filesortAction(ServerRequestInterface $request): ResponseInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use StefanFroemken\Mysqlreport\Domain\Repository\StatusRepository;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;

class CreatedTempTablesDataProvider implements ChartDataProviderInterface
readonly class CreatedTempTablesDataProvider implements ChartDataProviderInterface
{
private StatusValues $statusValues;

Expand Down
2 changes: 1 addition & 1 deletion Classes/Dashboard/Provider/HandlerReadNextDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use StefanFroemken\Mysqlreport\Domain\Repository\StatusRepository;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;

class HandlerReadNextDataProvider implements ChartDataProviderInterface
readonly class HandlerReadNextDataProvider implements ChartDataProviderInterface
{
private StatusValues $statusValues;

Expand Down
2 changes: 1 addition & 1 deletion Classes/Dashboard/Provider/InnoDbBufferDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use TYPO3\CMS\Dashboard\WidgetApi;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;

class InnoDbBufferDataProvider implements ChartDataProviderInterface
readonly class InnoDbBufferDataProvider implements ChartDataProviderInterface
{
private StatusValues $statusValues;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use StefanFroemken\Mysqlreport\Domain\Repository\VariablesRepository;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;

class MaxUsedConnectionsDataProvider implements ChartDataProviderInterface
readonly class MaxUsedConnectionsDataProvider implements ChartDataProviderInterface
{
private StatusValues $statusValues;

Expand Down
2 changes: 1 addition & 1 deletion Classes/Dashboard/Provider/QueryTypesDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use TYPO3\CMS\Dashboard\WidgetApi;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;

class QueryTypesDataProvider implements ChartDataProviderInterface
readonly class QueryTypesDataProvider implements ChartDataProviderInterface
{
private StatusValues $statusValues;

Expand Down
2 changes: 1 addition & 1 deletion Classes/DependencyInjection/DashboardPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* As there is no possibility in Services.yaml to check for activated/installed extensions
* I have moved that check into this CompilerPass.
*/
class DashboardPass implements CompilerPassInterface
readonly class DashboardPass implements CompilerPassInterface
{
private string $tagName;

Expand Down
24 changes: 22 additions & 2 deletions Classes/Doctrine/Middleware/LoggerStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LoggerStatement implements Statement
private array $params = [];

/**
* @var array<int, ParameterType>
* @var array<int, string>
*/
private array $types = [];

Expand All @@ -46,7 +46,7 @@ public function __construct(
public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void
{
$this->params[$param] = $value;
$this->types[$param] = $type;
$this->types[$param] = $this->convertParameterType($type);

$this->wrappedStatement->bindValue($param, $value, $type);
}
Expand All @@ -68,4 +68,24 @@ public function execute(): ResultInterface

return $result;
}

/**
* LARGE_OBJECT is not mapped to a Type object by default.
* In case of Mysqli it will be mapped to PARAMETER_TYPE_BINARY.
* As convertParameterType of MysqliStatement is private I have to map such types manually here.
* As long as this logger works for MySQL and MariaDB only, we should be safe here.
* Adding further DB types may need some modification here.
*/
private function convertParameterType(ParameterType $type): string
{
return match ($type) {
ParameterType::NULL,
ParameterType::STRING,
ParameterType::ASCII,
ParameterType::BINARY => 'string',
ParameterType::INTEGER,
ParameterType::BOOLEAN => 'integer',
ParameterType::LARGE_OBJECT => 'binary',
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Driver;
use StefanFroemken\Mysqlreport\Configuration\ExtConf;
use StefanFroemken\Mysqlreport\Traits\DatabaseConnectionTrait;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Middleware\UsableForConnectionInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -22,6 +23,8 @@
*/
readonly class LoggerWithQueryTimeMiddleware implements UsableForConnectionInterface
{
use DatabaseConnectionTrait;

/**
* @param array<string, string> $connectionParams
*/
Expand All @@ -31,7 +34,7 @@ public function canBeUsedForConnection(string $identifier, array $connectionPara
return false;
}

if (in_array($connectionParams['driver'] ?? '', ['mysqli', 'pdo_mysql'], true)) {
if (!$this->isValidConnectionDriver($connectionParams['driver'] ?? '')) {
return false;
}

Expand Down
16 changes: 9 additions & 7 deletions Classes/Domain/Factory/QueryInformationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* A factory to build new Profile objects.
* It should prevent calling methods to retrieve environment variables multiple times
*/
class QueryInformationFactory
readonly class QueryInformationFactory
{
use Typo3RequestTrait;

Expand Down Expand Up @@ -67,17 +67,19 @@ public function createNewQueryInformation(): QueryInformation

private function getPageUid(): int
{
$serverRequest = $GLOBALS['TYPO3_REQUEST'];
if ($serverRequest instanceof ServerRequestInterface) {
if (
($serverRequest = $GLOBALS['TYPO3_REQUEST'] ?? null)
&& $serverRequest instanceof ServerRequestInterface
) {
$pageArguments = $serverRequest->getAttribute('routing');
if ($pageArguments instanceof PageArguments) {
return $pageArguments->getPageId();
}
}

$backendPageUid = (int)($serverRequest->getQueryParams()['id'] ?? 0);
if ($backendPageUid !== 0) {
return $backendPageUid;
}
$backendPageUid = (int)($_GET['id'] ?? 0);
if ($backendPageUid !== 0) {
return $backendPageUid;
}

return 0;
Expand Down
1 change: 0 additions & 1 deletion Classes/Event/ModifyQueryInformationRecordsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function getQueryInformationRecords(): array
}

/**
* @param int $key
* @param array<string, mixed> $queryInformationRecord
*/
public function updateQueryInformationRecord(int $key, array $queryInformationRecord): void
Expand Down
15 changes: 5 additions & 10 deletions Classes/EventListener/CacheAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

namespace StefanFroemken\Mysqlreport\EventListener;

use StefanFroemken\Mysqlreport\Traits\DatabaseConnectionTrait;
use TYPO3\CMS\Backend\Backend\Event\ModifyClearCacheActionsEvent;
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Create ClearCache entry and process Cache Clearing of mysqlreport
*/
class CacheAction
readonly class CacheAction
{
use DatabaseConnectionTrait;

/**
* Add clear cache menu entry
*
Expand Down Expand Up @@ -52,14 +54,7 @@ public function clearProfiles(array $params = []): void
isset($params['cacheCmd'])
&& $params['cacheCmd'] === 'mysqlprofiles'
) {
$this->getConnectionPool()
->getConnectionForTable('tx_mysqlreport_query_information')
->truncate('tx_mysqlreport_query_information');
$this->getDefaultConnection()->truncate('tx_mysqlreport_query_information');
}
}

private function getConnectionPool(): ConnectionPool
{
return GeneralUtility::makeInstance(ConnectionPool::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* EventListener to reduce precision of duration column to 6.
*/
class RoundDurationOfQueryInformationRecordsEventListener
readonly class RoundDurationOfQueryInformationRecordsEventListener
{
public function __invoke(ModifyQueryInformationRecordsEvent $event): void
{
Expand Down
14 changes: 7 additions & 7 deletions Classes/Helper/DownloadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
/**
* Helper to download records as CSV or JSON
*/
class DownloadHelper
readonly class DownloadHelper
{
private string $csvDelimiter = ';';
private const CSV_DELIMITER = ';';

private string $csvQuote = '"';
private const CSV_QUOTE = '"';

public function __construct(
readonly private ResponseFactoryInterface $responseFactory,
readonly private LoggerInterface $logger,
private ResponseFactoryInterface $responseFactory,
private LoggerInterface $logger,
) {}

/**
Expand All @@ -37,9 +37,9 @@ public function __construct(
public function asCSV(array $headerRow, array $records): ResponseInterface
{
// Create result
$result[] = CsvUtility::csvValues($headerRow, $this->csvDelimiter, $this->csvQuote);
$result[] = CsvUtility::csvValues($headerRow, self::CSV_DELIMITER, self::CSV_QUOTE);
foreach ($records as $record) {
$result[] = CsvUtility::csvValues($record, $this->csvDelimiter, $this->csvQuote);
$result[] = CsvUtility::csvValues($record, self::CSV_DELIMITER, self::CSV_QUOTE);
}

return $this->generateDownloadResponse(implode(CRLF, $result), 'csv');
Expand Down
12 changes: 2 additions & 10 deletions Classes/Helper/ModuleTemplateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@
/**
* Helper to add buttons to button bar
*/
class ModuleTemplateHelper
readonly class ModuleTemplateHelper
{
private UriBuilder $uriBuilder;

public function __construct(UriBuilder $uriBuilder)
{
$this->uriBuilder = $uriBuilder;
}
public function __construct(private UriBuilder $uriBuilder) {}

public function addOverviewButton(ButtonBar $buttonBar): void
{
Expand All @@ -44,9 +39,6 @@ public function addOverviewButton(ButtonBar $buttonBar): void
}

/**
* @param ButtonBar $buttonBar
* @param string $routeIdentifier
* @param string $displayName
* @param array<string, string> $arguments
*/
public function addShortcutButton(
Expand Down
2 changes: 1 addition & 1 deletion Classes/Helper/QueryCacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Helper with useful methods for Query Cache
*/
class QueryCacheHelper
readonly class QueryCacheHelper
{
/**
* Returns true, if Query Cache is activated
Expand Down
5 changes: 2 additions & 3 deletions Classes/Helper/QueryParamsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace StefanFroemken\Mysqlreport\Helper;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Psr\Log\LoggerInterface;
Expand All @@ -30,7 +29,7 @@ public function __construct(

/**
* @param array<int, string> $params
* @param array<int, ParameterType> $types
* @param array<int, string> $types
*/
public function getQueryWithReplacedParams(
string $sql,
Expand All @@ -44,7 +43,7 @@ public function getQueryWithReplacedParams(
)->getDatabasePlatform();

foreach ($params as $key => $param) {
$type = Type::getType(strtolower($types[$key]->name));
$type = Type::getType($types[$key]);
$value = $type->convertToDatabaseValue($param, $dbPlatform);
$sql = implode(
var_export($value, true),
Expand Down
3 changes: 1 addition & 2 deletions Classes/Logger/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace StefanFroemken\Mysqlreport\Logger;

use Doctrine\DBAL\ParameterType;
use StefanFroemken\Mysqlreport\Domain\Model\QueryInformation;

interface LoggerInterface
Expand All @@ -21,7 +20,7 @@ interface LoggerInterface
* Start collecting duration and other stuff.
*
* @param array<int, string> $params
* @param array<int, ParameterType> $types
* @param array<int, string> $types
*/
public function stopQuery(string $query, float $duration, array $params = [], array $types = []): ?QueryInformation;
}
Loading

0 comments on commit e86b3d9

Please sign in to comment.