Skip to content

Commit

Permalink
[TASK] Harden \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbB…
Browse files Browse the repository at this point in the history
…ackend

- Use strict type mode
- Use type hints whereever possible
- Use type casts according to phpstan results

Releases: master
Resolves: #88666
Change-Id: I4d93c3e3c75f5f6ca6b53bd73acffe73d7b6a58e
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61205
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
  • Loading branch information
alexanderschnitzler authored and NeoBlack committed Jul 5, 2019
1 parent 66302a1 commit 15932f9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 69 deletions.
36 changes: 17 additions & 19 deletions typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
Expand Up @@ -619,8 +619,8 @@ protected function insertObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInte
{
if ($object instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
$result = $this->getUidOfAlreadyPersistedValueObject($object);
if ($result !== false) {
$object->_setProperty('uid', (int)$result);
if ($result !== null) {
$object->_setProperty('uid', $result);
return;
}
}
Expand Down Expand Up @@ -708,7 +708,7 @@ protected function emitEndInsertObjectSignal(DomainObjectInterface $object)
* Tests, if the given Value Object already exists in the storage backend and if so, it returns the uid.
*
* @param \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object The object to be tested
* @return mixed The matching uid if an object was found, else FALSE
* @return int|null The matching uid if an object was found, else null
*/
protected function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object)
{
Expand Down Expand Up @@ -776,11 +776,11 @@ protected function updateRelationInRelationTable(\TYPO3\CMS\Extbase\DomainObject
if (is_array($relationTableMatchFields)) {
$row = array_merge($relationTableMatchFields, $row);
}
$res = $this->storageBackend->updateRelationTableRow(
$this->storageBackend->updateRelationTableRow(
$relationTableName,
$row
);
return $res;
return true;
}

/**
Expand All @@ -802,8 +802,8 @@ protected function deleteAllRelationsFromRelationtable(\TYPO3\CMS\Extbase\Domain
if (is_array($relationTableMatchFields)) {
$relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
}
$res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
return $res;
$this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
return true;
}

/**
Expand All @@ -827,8 +827,8 @@ protected function deleteRelationFromRelationtable(\TYPO3\CMS\Extbase\DomainObje
if (is_array($relationTableMatchFields)) {
$relationMatchFields = array_merge($relationTableMatchFields, $relationMatchFields);
}
$res = $this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
return $res;
$this->storageBackend->removeRow($relationTableName, $relationMatchFields, false);
return true;
}

/**
Expand Down Expand Up @@ -905,15 +905,14 @@ protected function updateObject(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInte
$row['uid'] = $object->_getProperty('_localizedUid');
}
}
$res = $this->storageBackend->updateRow($dataMap->getTableName(), $row);
if ($res === true) {
$this->emitAfterUpdateObjectSignal($object);
}
$this->storageBackend->updateRow($dataMap->getTableName(), $row);
$this->emitAfterUpdateObjectSignal($object);

$frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
$this->referenceIndex->updateRefIndexTable($dataMap->getTableName(), $row['uid']);
}
return $res;
return true;
}

/**
Expand Down Expand Up @@ -1003,13 +1002,12 @@ protected function removeEntity(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInte
$deletedColumnName => 1
];
$this->addCommonDateFieldsToRow($object, $row);
$res = $this->storageBackend->updateRow($tableName, $row);
$this->storageBackend->updateRow($tableName, $row);
} else {
$res = $this->storageBackend->removeRow($tableName, ['uid' => $object->getUid()]);
}
if ($res === true) {
$this->emitAfterRemoveObjectSignal($object);
$this->storageBackend->removeRow($tableName, ['uid' => $object->getUid()]);
}
$this->emitAfterRemoveObjectSignal($object);

$this->removeRelatedObjects($object);
$frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
if ($frameworkConfiguration['persistence']['updateReferenceIndex'] === '1') {
Expand Down
@@ -1,4 +1,6 @@
<?php
declare(strict_types = 1);

namespace TYPO3\CMS\Extbase\Persistence\Generic\Storage;

/*
Expand Down Expand Up @@ -27,36 +29,33 @@ interface BackendInterface
* @param bool $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
* @return int the UID of the inserted row
*/
public function addRow($tableName, array $fieldValues, $isRelation = false);
public function addRow(string $tableName, array $fieldValues, bool $isRelation = false): int;

/**
* Updates a row in the storage
*
* @param string $tableName The database table name
* @param array $fieldValues The fieldValues to update
* @param bool $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
* @return mixed|void
*/
public function updateRow($tableName, array $fieldValues, $isRelation = false);
public function updateRow(string $tableName, array $fieldValues, bool $isRelation = false): void;

/**
* Updates a relation row in the storage
*
* @param string $tableName The database relation table name
* @param array $fieldValues The fieldValues to be updated
* @return bool
*/
public function updateRelationTableRow($tableName, array $fieldValues);
public function updateRelationTableRow(string $tableName, array $fieldValues): void;

/**
* Deletes a row in the storage
*
* @param string $tableName The database table name
* @param array $where An array of where array('fieldname' => value). This array will be transformed to a WHERE clause
* @param bool $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
* @return mixed|void
*/
public function removeRow($tableName, array $where, $isRelation = false);
public function removeRow(string $tableName, array $where, bool $isRelation = false): void;

/**
* Fetches maximal value for given table column
Expand All @@ -66,30 +65,30 @@ public function removeRow($tableName, array $where, $isRelation = false);
* @param string $columnName column name to get the max value from
* @return mixed the max value
*/
public function getMaxValueFromTable($tableName, array $where, $columnName);
public function getMaxValueFromTable(string $tableName, array $where, string $columnName);

/**
* Returns the number of items matching the query.
*
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
* @return int
*/
public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query);
public function getObjectCountByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query): int;

/**
* Returns the object data matching the $query.
*
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
* @return array
*/
public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query);
public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query): array;

/**
* Checks if a Value Object equal to the given Object exists in the data base
*
* @param \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object The Value Object
* @return mixed The matching uid if an object was found, else FALSE
* @return int|null The matching uid if an object was found, else null
* @todo this is the last monster in this persistence series. refactor!
*/
public function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object);
public function getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object): ?int;
}

0 comments on commit 15932f9

Please sign in to comment.