Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8 rector recommended changes #21

Merged
merged 4 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 27 additions & 31 deletions Classes/Database/DatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*
* The TYPO3 project - inspiring people to share!
*/

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\ConnectionException;
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
Expand Down Expand Up @@ -57,14 +59,14 @@ class DatabaseConnection
*
* @var string
*/
const AND_Constraint = 'AND';
public const AND_Constraint = 'AND';

/**
* The OR constraint in where clause
*
* @var string
*/
const OR_Constraint = 'OR';
public const OR_Constraint = 'OR';

/**
* Set "TRUE" or "1" if you want database errors outputted. Set to "2" if you also want successful database actions outputted.
Expand Down Expand Up @@ -445,7 +447,7 @@ public function exec_SELECTcountRows($field, $table, $where = '1=1')
$count = false;
$resultSet = $this->exec_SELECTquery('COUNT(' . $field . ')', $table, $where);
if ($resultSet !== false) {
list($count) = $this->sql_fetch_row($resultSet);
[$count] = $this->sql_fetch_row($resultSet);
$count = (int)$count;
$this->sql_free_result($resultSet);
}
Expand Down Expand Up @@ -749,7 +751,7 @@ public function listQuery($field, $value, $table)
{
$this->logDeprecation();
$value = (string)$value;
if (strpos($value, ',') !== false) {
if (str_contains($value, ',')) {
throw new \InvalidArgumentException('$value must not contain a comma (,) in $this->listQuery() !', 1294585862);
}
$pattern = $this->quoteStr($value, $table);
Expand All @@ -769,13 +771,10 @@ public function listQuery($field, $value, $table)
public function searchQuery($searchWords, $fields, $table, $constraint = self::AND_Constraint)
{
$this->logDeprecation();
switch ($constraint) {
case self::OR_Constraint:
$constraint = 'OR';
break;
default:
$constraint = 'AND';
}
$constraint = match ($constraint) {
self::OR_Constraint => 'OR',
default => 'AND',
};

$queryParts = [];
foreach ($searchWords as $sw) {
Expand All @@ -802,14 +801,14 @@ public function searchQuery($searchWords, $fields, $table, $constraint = self::A
* @param string $orderBy See exec_SELECTquery()
* @param string $limit See exec_SELECTquery()
* @param array $input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::PARAM_AUTOTYPE.
* @return \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement Prepared statement
* @return PreparedStatement Prepared statement
*/
public function prepare_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy = '', $limit = '', array $input_parameters = [])
{
$this->logDeprecation();
$query = $this->SELECTquery($select_fields, $from_table, $where_clause, $groupBy, $orderBy, $limit);
/** @var $preparedStatement \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement */
$preparedStatement = GeneralUtility::makeInstance(\TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::class, $query, $from_table, []);
$preparedStatement = GeneralUtility::makeInstance(PreparedStatement::class, $query, $from_table, []);
// Bind values to parameters
foreach ($input_parameters as $key => $value) {
$preparedStatement->bindValue($key, $value, PreparedStatement::PARAM_AUTOTYPE);
Expand All @@ -823,7 +822,7 @@ public function prepare_SELECTquery($select_fields, $from_table, $where_clause,
*
* @param array $queryParts Query parts array
* @param array $input_parameters An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement::PARAM_AUTOTYPE.
* @return \TYPO3\CMS\Typo3DbLegacy\Database\PreparedStatement Prepared statement
* @return PreparedStatement Prepared statement
*/
public function prepare_SELECTqueryArray(array $queryParts, array $input_parameters = [])
{
Expand Down Expand Up @@ -1289,7 +1288,7 @@ public function sql_pconnect()
// We are not using the TYPO3 CMS shim here as the database parameters in this class
// are settable externally. This requires building the connection parameter array
// just in time when establishing the connection.
$connection = \Doctrine\DBAL\DriverManager::getConnection([
$connection = DriverManager::getConnection([
'driver' => 'mysqli',
'wrapperClass' => Connection::class,
'host' => $host,
Expand All @@ -1305,7 +1304,7 @@ public function sql_pconnect()
/** @var \Doctrine\DBAL\Driver\Mysqli\MysqliConnection $mysqliConnection */
$mysqliConnection = $connection->getWrappedConnection();
$this->link = $mysqliConnection->getWrappedResourceHandle();
} catch (\Doctrine\DBAL\Exception\ConnectionException $exception) {
} catch (ConnectionException) {
return false;
}

Expand Down Expand Up @@ -1381,7 +1380,7 @@ public function admin_get_dbs()
if ($this->sql_select_db()) {
$dbArr[] = $row->SCHEMA_NAME;
}
} catch (\RuntimeException $exception) {
} catch (\RuntimeException) {
// The exception happens if we cannot connect to the database
// (usually due to missing permissions). This is ok here.
// We catch the exception, skip the database and continue.
Expand Down Expand Up @@ -1836,17 +1835,14 @@ public function debug($func, $query = '')
$this->logDeprecation();
$error = $this->sql_error();
if ($error || (int)$this->debugOutput === 2) {
\TYPO3\CMS\Core\Utility\DebugUtility::debug(
DebugUtility::debug(
[
'caller' => \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection::class . '::' . $func,
'ERROR' => $error,
'lastBuiltQuery' => $query ? $query : $this->debug_lastBuiltQuery,
'debug_backtrace' => \TYPO3\CMS\Core\Utility\DebugUtility::debugTrail()
'lastBuiltQuery' => $query ?: $this->debug_lastBuiltQuery,
'debug_backtrace' => DebugUtility::debugTrail()
],
$func,
is_object($GLOBALS['error']) && @is_callable([$GLOBALS['error'], 'debug'])
? ''
: 'DB Error'
$func
);
}
}
Expand Down Expand Up @@ -1914,10 +1910,10 @@ protected function explain($query, $from_table, $row_count)
return false;
}
$error = $this->sql_error();
$trail = \TYPO3\CMS\Core\Utility\DebugUtility::debugTrail();
$trail = DebugUtility::debugTrail();
$explain_tables = [];
$explain_output = [];
$res = $this->sql_query('EXPLAIN ' . $query, $this->link);
$res = $this->sql_query('EXPLAIN ' . $query);
if (is_a($res, '\\mysqli_result')) {
while ($tempRow = $this->sql_fetch_assoc($res)) {
$explain_output[] = $tempRow;
Expand All @@ -1934,7 +1930,7 @@ protected function explain($query, $from_table, $row_count)
$tableRes = $this->sql_query('SHOW TABLE STATUS LIKE \'' . $table . '\'');
$isTable = $this->sql_num_rows($tableRes);
if ($isTable) {
$res = $this->sql_query('SHOW INDEX FROM ' . $table, $this->link);
$res = $this->sql_query('SHOW INDEX FROM ' . $table);
if (is_a($res, '\\mysqli_result')) {
while ($tempRow = $this->sql_fetch_assoc($res)) {
$indices_output[] = $tempRow;
Expand Down Expand Up @@ -1963,7 +1959,7 @@ protected function explain($query, $from_table, $row_count)
$data['indices'] = $indices_output;
}
if ($explainMode == 1) {
\TYPO3\CMS\Core\Utility\DebugUtility::debug($data, 'Tables: ' . $from_table, 'DB SQL EXPLAIN');
DebugUtility::debug($data, 'Tables: ' . $from_table);
} elseif ($explainMode == 2) {
/** @var TimeTracker $timeTracker */
$timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
Expand Down Expand Up @@ -2021,7 +2017,7 @@ protected function logError($message)
GeneralUtility::SYSLOG_SEVERITY_ERROR
);
} else {
GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__)->error($message);
GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class)->error($message);
}
}

Expand All @@ -2034,7 +2030,7 @@ protected function logFatalError($message)
GeneralUtility::SYSLOG_SEVERITY_FATAL
);
} else {
GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__)->emergency($message);
GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class)->emergency($message);
}
}
}
18 changes: 6 additions & 12 deletions Classes/Database/PostProcessQueryHookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@ interface PostProcessQueryHookInterface
* @param string $groupBy Group by statement
* @param string $orderBy Order by statement
* @param int $limit Database return limit
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function exec_SELECTquery_postProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function exec_SELECTquery_postProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, DatabaseConnection $parentObject);

/**
* Post-processor for the exec_INSERTquery method.
*
* @param string $table Database table name
* @param array $fieldsValues Field values as key => value pairs
* @param string|array $noQuoteFields List/array of keys NOT to quote
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function exec_INSERTquery_postProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function exec_INSERTquery_postProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject);

/**
* Post-processor for the exec_INSERTmultipleRows method.
Expand All @@ -50,9 +48,8 @@ public function exec_INSERTquery_postProcessAction(&$table, array &$fieldsValues
* @param array $fields Field names
* @param array $rows Table rows
* @param string|array $noQuoteFields List/array of keys NOT to quote
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function exec_INSERTmultipleRows_postProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function exec_INSERTmultipleRows_postProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, DatabaseConnection $parentObject);

/**
* Post-processor for the exec_UPDATEquery method.
Expand All @@ -61,24 +58,21 @@ public function exec_INSERTmultipleRows_postProcessAction(&$table, array &$field
* @param string $where WHERE clause
* @param array $fieldsValues Field values as key => value pairs
* @param string|array $noQuoteFields List/array of keys NOT to quote
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function exec_UPDATEquery_postProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function exec_UPDATEquery_postProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject);

/**
* Post-processor for the exec_DELETEquery method.
*
* @param string $table Database table name
* @param string $where WHERE clause
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function exec_DELETEquery_postProcessAction(&$table, &$where, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function exec_DELETEquery_postProcessAction(&$table, &$where, DatabaseConnection $parentObject);

/**
* Post-processor for the exec_TRUNCATEquery method.
*
* @param string $table Database table name
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function exec_TRUNCATEquery_postProcessAction(&$table, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function exec_TRUNCATEquery_postProcessAction(&$table, DatabaseConnection $parentObject);
}
18 changes: 6 additions & 12 deletions Classes/Database/PreProcessQueryHookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@ interface PreProcessQueryHookInterface
* @param string $groupBy Group by statement
* @param string $orderBy Order by statement
* @param int $limit Database return limit
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function SELECTquery_preProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function SELECTquery_preProcessAction(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy, &$limit, DatabaseConnection $parentObject);

/**
* Pre-processor for the INSERTquery method.
*
* @param string $table Database table name
* @param array $fieldsValues Field values as key => value pairs
* @param string|array $noQuoteFields List/array of keys NOT to quote
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function INSERTquery_preProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function INSERTquery_preProcessAction(&$table, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject);

/**
* Pre-processor for the INSERTmultipleRows method.
Expand All @@ -52,9 +50,8 @@ public function INSERTquery_preProcessAction(&$table, array &$fieldsValues, &$no
* @param array $fields Field names
* @param array $rows Table rows
* @param string|array $noQuoteFields List/array of keys NOT to quote
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function INSERTmultipleRows_preProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function INSERTmultipleRows_preProcessAction(&$table, array &$fields, array &$rows, &$noQuoteFields, DatabaseConnection $parentObject);

/**
* Pre-processor for the UPDATEquery method.
Expand All @@ -63,24 +60,21 @@ public function INSERTmultipleRows_preProcessAction(&$table, array &$fields, arr
* @param string $where WHERE clause
* @param array $fieldsValues Field values as key => value pairs
* @param string|array $noQuoteFields List/array of keys NOT to quote
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function UPDATEquery_preProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function UPDATEquery_preProcessAction(&$table, &$where, array &$fieldsValues, &$noQuoteFields, DatabaseConnection $parentObject);

/**
* Pre-processor for the DELETEquery method.
*
* @param string $table Database table name
* @param string $where WHERE clause
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function DELETEquery_preProcessAction(&$table, &$where, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function DELETEquery_preProcessAction(&$table, &$where, DatabaseConnection $parentObject);

/**
* Pre-processor for the TRUNCATEquery method.
*
* @param string $table Database table name
* @param \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject
*/
public function TRUNCATEquery_preProcessAction(&$table, \TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection $parentObject);
public function TRUNCATEquery_preProcessAction(&$table, DatabaseConnection $parentObject);
}
Loading