Skip to content

Commit

Permalink
[TASK] Make DatabaseTreeDataProvider strictly typed
Browse files Browse the repository at this point in the history
Used command:

> ./Build/Scripts/runTests.sh -s phpstanGenerateBaseline

Resolves: #98006
Releases: main
Change-Id: If809683db556c2c91ae5f560a4a796c9477d3631
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75268
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Simon Schaufelberger <simonschaufi+typo3@gmail.com>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
Crell authored and lolli42 committed Jul 31, 2022
1 parent be32f41 commit 7512af3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 107 deletions.
5 changes: 0 additions & 5 deletions Build/phpstan/phpstan-baseline.neon
Expand Up @@ -1165,11 +1165,6 @@ parameters:
count: 1
path: ../../typo3/sysext/core/Classes/Tree/TableConfiguration/ArrayTreeRenderer.php

-
message: "#^Parameter \\#1 \\$id of method TYPO3\\\\CMS\\\\Backend\\\\Tree\\\\TreeNode\\:\\:setId\\(\\) expects string, int given\\.$#"
count: 3
path: ../../typo3/sysext/core/Classes/Tree/TableConfiguration/DatabaseTreeDataProvider.php

-
message: "#^Call to an undefined method TYPO3\\\\CMS\\\\Backend\\\\Tree\\\\TreeNode\\:\\:getSortValue\\(\\)\\.$#"
count: 1
Expand Down
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
Expand Down Expand Up @@ -37,170 +39,133 @@ class DatabaseTreeDataProvider extends AbstractTableConfigurationTreeDataProvide
const MODE_CHILDREN = 1;
const MODE_PARENT = 2;

/**
* @var string
*/
protected $tableName = '';
protected string $tableName = '';

/**
* @var string
*/
protected $treeId = '';

/**
* @var string
*/
protected $labelField = '';
protected string $labelField = '';

/**
* @var string
*/
protected $tableWhere = '';
protected string $tableWhere = '';

/**
* @var int
* @var self::MODE_*
*/
protected $lookupMode = self::MODE_CHILDREN;
protected int $lookupMode = self::MODE_CHILDREN;

/**
* @var string
*/
protected $lookupField = '';
protected string $lookupField = '';

/**
* @var int[]
*/
protected array $startingPoints = [0];

/**
* @var array
*/
protected $idCache = [];
protected array $idCache = [];

/**
* Stores TCA-Configuration of the LookUpField in tableName
*
* @var array
* @var array<string, mixed>
*/
protected $columnConfiguration;
protected array $columnConfiguration;

/**
* node sort values (the orderings from foreign_Table_where evaluation)
*
* @var array
* @var array<string, mixed>
*/
protected $nodeSortValues = [];
protected array $nodeSortValues = [];

/**
* @var array TCEforms compiled TSConfig array
* TCEforms compiled TSConfig array
*/
protected $generatedTSConfig = [];
protected array $generatedTSConfig = [];

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
public function __construct(protected EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

/**
* Sets the label field
*
* @param string $labelField
*/
public function setLabelField($labelField)
public function setLabelField(string $labelField): void
{
$this->labelField = $labelField;
}

/**
* Gets the label field
*
* @return string
*/
public function getLabelField()
public function getLabelField(): string
{
return $this->labelField;
}

/**
* Sets the table name
*
* @param string $tableName
*/
public function setTableName($tableName)
public function setTableName(string $tableName): void
{
$this->tableName = $tableName;
}

/**
* Gets the table name
*
* @return string
*/
public function getTableName()
public function getTableName(): string
{
return $this->tableName;
}

/**
* Sets the lookup field
*
* @param string $lookupField
*/
public function setLookupField($lookupField)
public function setLookupField(string $lookupField): void
{
$this->lookupField = $lookupField;
}

/**
* Gets the lookup field
*
* @return string
*/
public function getLookupField()
public function getLookupField(): string
{
return $this->lookupField;
}

/**
* Sets the lookup mode
*
* @param int $lookupMode
* @param self::MODE_* $lookupMode
*/
public function setLookupMode($lookupMode)
public function setLookupMode(int $lookupMode): void
{
$this->lookupMode = $lookupMode;
}

/**
* Gets the lookup mode
*
* @return int
* @return self::MODE_*
*/
public function getLookupMode()
public function getLookupMode(): int
{
return $this->lookupMode;
}

/**
* Gets the nodes
*
* @param TreeNode $node
*/
public function getNodes(TreeNode $node)
public function getNodes(TreeNode $node): void
{
}

/**
* Gets the root node
*
* @return DatabaseTreeNode
*/
public function getRoot()
public function getRoot(): DatabaseTreeNode
{
return $this->buildRepresentationForNode($this->treeData);
}
Expand All @@ -227,40 +192,31 @@ public function getStartingPoints(): array

/**
* Sets the tableWhere clause
*
* @param string $tableWhere
*/
public function setTableWhere($tableWhere)
public function setTableWhere(string $tableWhere): void
{
$this->tableWhere = $tableWhere;
}

/**
* Gets the tableWhere clause
*
* @return string
*/
public function getTableWhere()
public function getTableWhere(): string
{
return $this->tableWhere;
}

/**
* Builds a complete node including children
*
* @param TreeNode $basicNode
* @param DatabaseTreeNode|null $parent
* @param int $level
* @return DatabaseTreeNode Node object
*/
protected function buildRepresentationForNode(TreeNode $basicNode, DatabaseTreeNode $parent = null, $level = 0)
protected function buildRepresentationForNode(TreeNode $basicNode, ?DatabaseTreeNode $parent = null, $level = 0): DatabaseTreeNode
{
$node = GeneralUtility::makeInstance(DatabaseTreeNode::class);
$row = [];
if ($basicNode->getId() == 0) {
$node->setSelected(false);
$node->setExpanded(true);
$node->setLabel($this->getLanguageService()->sL($GLOBALS['TCA'][$this->tableName]['ctrl']['title']));
$node->setLabel($this->getLanguageService()?->sL($GLOBALS['TCA'][$this->tableName]['ctrl']['title']));
} else {
$row = BackendUtility::getRecordWSOL($this->tableName, (int)$basicNode->getId(), '*', '', false) ?? [];
$node->setLabel(BackendUtility::getRecordTitle($this->tableName, $row) ?: $basicNode->getId());
Expand Down Expand Up @@ -290,7 +246,7 @@ protected function buildRepresentationForNode(TreeNode $basicNode, DatabaseTreeN
/**
* Init the tree data
*/
public function initializeTreeData()
public function initializeTreeData(): void
{
parent::initializeTreeData();
$this->nodeSortValues = array_flip($this->itemWhiteList);
Expand All @@ -308,7 +264,7 @@ public function initializeTreeData()
/**
* Loads the tree data (all possible children)
*/
protected function loadTreeData()
protected function loadTreeData(): void
{
if ($this->getStartingPoints()) {
$startingPoints = $this->getStartingPoints();
Expand All @@ -319,7 +275,7 @@ protected function loadTreeData()
if (count($startingPoints) === 1) {
// Only one starting point is available, grab it and set it as root node
$startingPoint = current($startingPoints);
$this->treeData->setId($startingPoint);
$this->treeData->setId((string)$startingPoint);
$this->treeData->setParentNode(null);

if ($this->levelMaximum >= 1) {
Expand All @@ -336,7 +292,7 @@ protected function loadTreeData()
$treeNodeCollection = GeneralUtility::makeInstance(TreeNodeCollection::class);
foreach ($startingPoints as $startingPoint) {
$treeData = GeneralUtility::makeInstance(TreeNode::class);
$treeData->setId($startingPoint);
$treeData->setId((string)$startingPoint);

if ($this->levelMaximum >= 1) {
$childNodes = $this->getChildrenOf($treeData, 1);
Expand All @@ -346,19 +302,15 @@ protected function loadTreeData()
}
$treeNodeCollection->append($treeData);
}
$this->treeData->setId(0);
$this->treeData->setId('0');
$this->treeData->setChildNodes($treeNodeCollection);
}
}

/**
* Gets node children
*
* @param TreeNode $node
* @param int $level
* @return TreeNodeCollection|null
*/
protected function getChildrenOf(TreeNode $node, $level): ?TreeNodeCollection
protected function getChildrenOf(TreeNode $node, int $level): ?TreeNodeCollection
{
$nodeData = null;
if ($node->getId() !== 0) {
Expand Down Expand Up @@ -404,20 +356,17 @@ protected function getChildrenOf(TreeNode $node, $level): ?TreeNodeCollection

/**
* Gets related records depending on TCA configuration
*
* @param array $row
* @return array
*/
protected function getRelatedRecords(array $row)
protected function getRelatedRecords(array $row): array
{
if ($this->getLookupMode() == self::MODE_PARENT) {
if ($this->getLookupMode() === self::MODE_PARENT) {
$children = $this->getChildrenUidsFromParentRelation($row);
} else {
$children = $this->getChildrenUidsFromChildrenRelation($row);
}
$allowedArray = [];
foreach ($children as $child) {
if (!in_array($child, $this->idCache) && in_array($child, $this->itemWhiteList)) {
if (!in_array($child, $this->idCache, true) && in_array($child, $this->itemWhiteList, true)) {
$allowedArray[] = $child;
}
}
Expand All @@ -427,11 +376,8 @@ protected function getRelatedRecords(array $row)

/**
* Gets related records depending on TCA configuration
*
* @param array $row
* @return array
*/
protected function getChildrenUidsFromParentRelation(array $row)
protected function getChildrenUidsFromParentRelation(array $row): array
{
$uid = $row['uid'];
if (in_array($this->columnConfiguration['type'] ?? '', ['select', 'category', 'inline'], true)) {
Expand All @@ -455,11 +401,8 @@ protected function getChildrenUidsFromParentRelation(array $row)

/**
* Gets related children records depending on TCA configuration
*
* @param array $row
* @return array
*/
protected function getChildrenUidsFromChildrenRelation(array $row)
protected function getChildrenUidsFromChildrenRelation(array $row): array
{
$relatedUids = [];
$uid = $row['uid'];
Expand Down Expand Up @@ -515,9 +458,8 @@ protected function getChildrenUidsFromChildrenRelation(array $row)
* @param int $queryId the uid to search for
* @return int[] all uids found
*/
protected function listFieldQuery($fieldName, $queryId)
protected function listFieldQuery(string $fieldName, int $queryId): array
{
$queryId = (int)$queryId;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($this->getTableName());
$queryBuilder->getRestrictions()->removeAll();
Expand Down
Expand Up @@ -14,6 +14,7 @@ all of their method signatures. The types are consistent with existing
docblock-documented type expectations and existing behavior.

- :php:`\TYPO3\CMS\Core\LinkHandling\FileLinkHandler`
- :php:`\TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider`
- :php:`\TYPO3\CMS\Core\Utility\ArrayUtility`
- :php:`\TYPO3\CMS\Core\Utility\ClassNamingUtility`
- :php:`\TYPO3\CMS\Core\Utility\CsvUtility`
Expand Down

0 comments on commit 7512af3

Please sign in to comment.