Skip to content

Commit

Permalink
Remove calls to compact()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Sep 4, 2023
1 parent 1695af6 commit 2e0ab7a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 22 deletions.
11 changes: 9 additions & 2 deletions src/Loggable/Entity/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public function getLogEntriesQuery($entity)

$objectId = (string) $wrapped->getIdentifier(false, true);
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('objectId', 'objectClass'));
$q->setParameters([
'objectId' => $objectId,
'objectClass' => $objectClass,
]);

return $q;
}
Expand Down Expand Up @@ -113,7 +116,11 @@ public function revert($entity, $version = 1)

$objectId = (string) $wrapped->getIdentifier(false, true);
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('objectId', 'objectClass', 'version'));
$q->setParameters([
'objectId' => $objectId,
'objectClass' => $objectClass,
'version' => $version,
]);
$logs = $q->getResult();

if ([] === $logs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ public function translate($document, $field, $locale, $value)
$foreignKey = $meta->getReflectionProperty($meta->getIdentifier()[0])->getValue($document);
$objectClass = $config['useObjectClass'];
$transMeta = $this->dm->getClassMetadata($class);
$trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
$trans = $this->findOneBy([
'locale' => $locale,
'field' => $field,
'objectClass' => $objectClass,
'foreignKey' => $foreignKey,
]);
if (!$trans) {
$trans = $transMeta->newInstance();
$transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
Expand Down
13 changes: 11 additions & 2 deletions src/Translatable/Entity/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ public function translate($entity, $field, $locale, $value)
$foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity);
$objectClass = $config['useObjectClass'];
$transMeta = $this->_em->getClassMetadata($class);
$trans = $this->findOneBy(compact('locale', 'objectClass', 'field', 'foreignKey'));
$trans = $this->findOneBy([
'locale' => $locale,
'objectClass' => $objectClass,
'field' => $field,
'foreignKey' => $foreignKey,
]);
if (!$trans) {
$trans = $transMeta->newInstance();
$transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
Expand Down Expand Up @@ -180,7 +185,11 @@ public function findObjectByTranslatedField($field, $value, $class)
$dql .= ' AND trans.field = :field';
$dql .= ' AND trans.content = :value';
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('class', 'field', 'value'));
$q->setParameters([
'class' => $class,
'field' => $field,
'value' => $value,
]);
$q->setMaxResults(1);
$result = $q->getArrayResult();
$id = $result[0]['foreignKey'] ?? null;
Expand Down
16 changes: 13 additions & 3 deletions src/Translatable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public function loadTranslations($object, $translationClass, $locale, $objectCla
$dql .= ' AND t.object = :object';

$q = $em->createQuery($dql);
$q->setParameters(compact('object', 'locale'));
$q->setParameters([
'object' => $object,
'locale' => $locale,
]);

Check warning on line 86 in src/Translatable/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/Translatable/Mapping/Event/Adapter/ORM.php#L83-L86

Added lines #L83 - L86 were not covered by tests
$result = $q->getArrayResult();
}
} else {
Expand All @@ -93,7 +96,11 @@ public function loadTranslations($object, $translationClass, $locale, $objectCla
$dql .= ' AND t.objectClass = :objectClass';
// fetch results
$q = $em->createQuery($dql);
$q->setParameters(compact('objectId', 'locale', 'objectClass'));
$q->setParameters([
'objectId' => $objectId,
'locale' => $locale,
'objectClass' => $objectClass,
]);
$result = $q->getArrayResult();
}

Expand Down Expand Up @@ -136,7 +143,10 @@ public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $tran
'trans.field = :field'
)
;
$qb->setParameters(compact('locale', 'field'));
$qb->setParameters([
'locale' => $locale,
'field' => $field,
]);
if ($this->usesPersonalTranslation($translationClass)) {
$qb->andWhere('trans.object = :object');
if ($wrapped->getIdentifier()) {
Expand Down
13 changes: 8 additions & 5 deletions src/Tree/Entity/Repository/ClosureTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getPathQuery($node)
$dql .= ' WHERE c.descendant = :node';
$dql .= ' ORDER BY c.depth DESC';
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('node'));
$q->setParameter('node', $node);

return $q;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public function removeFromTree($node)
$dql = "SELECT node FROM {$config['useObjectClass']} node";
$dql .= " WHERE node.{$config['parent']} = :node";
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('node'));
$q->setParameter('node', $node);
$nodesToReparent = $q->getResult();
// process updates in transaction
$this->_em->getConnection()->beginTransaction();
Expand All @@ -286,7 +286,10 @@ public function removeFromTree($node)
$dql .= " WHERE node.{$pk} = :id";

$q = $this->_em->createQuery($dql);
$q->setParameters(compact('parent', 'id'));
$q->setParameters([
'parent' => $parent,
'id' => $id,
]);
$q->getSingleScalarResult();

$this->listener
Expand All @@ -301,7 +304,7 @@ public function removeFromTree($node)
$dql .= " WHERE node.{$pk} = :nodeId";

$q = $this->_em->createQuery($dql);
$q->setParameters(compact('nodeId'));
$q->setParameter('nodeId', $nodeId);
$q->getSingleScalarResult();
$this->_em->getConnection()->commit();
} catch (\Exception $e) {
Expand Down Expand Up @@ -388,7 +391,7 @@ public function getNodesHierarchyQueryBuilder($node = null, $direct = false, arr

if (null !== $node) {
$q->where('c.ancestor = :node');
$q->setParameters(compact('node'));
$q->setParameter('node', $node);
} else {
$q->groupBy('c.descendant');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tree/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getNodesHierarchy($node = null, $direct = false, array $options
* @param string|string[] $direction Sort order ('asc'|'desc'|'ASC'|'DESC'). If $sortByField is an array, this may also be an array with matching number of elements
* @param bool $includeNode Include the root node in results?
*
* @return iterable|null List of children
* @return iterable<int|string, object> List of children
*
* @phpstan-param 'asc'|'desc'|'ASC'|'DESC'|array<int, 'asc'|'desc'|'ASC'|'DESC'> $direction
*/
Expand Down
11 changes: 7 additions & 4 deletions src/Tree/Strategy/ORM/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function processPostPersist($em, $entity, AdapterInterface $ea)
$dql .= ' JOIN c.ancestor a';
$dql .= ' WHERE c.descendant = :parent';
$q = $em->createQuery($dql);
$q->setParameters(compact('parent'));
$q->setParameter('parent', $parent);
$ancestors = $q->getArrayResult();

if ([] === $ancestors) {
Expand Down Expand Up @@ -400,7 +400,10 @@ public function updateNode(EntityManagerInterface $em, $node, $oldParent)
$dql .= ' WHERE c.ancestor = :node';
$dql .= ' AND c.descendant = :parent';
$q = $em->createQuery($dql);
$q->setParameters(compact('node', 'parent'));
$q->setParameters([
'node' => $node,
'parent' => $parent,
]);
if ($q->getSingleScalarResult()) {
throw new UnexpectedValueException("Cannot set child as parent to node: {$nodeId}");
}
Expand All @@ -411,7 +414,7 @@ public function updateNode(EntityManagerInterface $em, $node, $oldParent)
$subQuery .= " JOIN {$table} c2 ON c1.descendant = c2.descendant";
$subQuery .= ' WHERE c1.ancestor = :nodeId AND c2.depth > c1.depth';

$ids = $conn->executeQuery($subQuery, compact('nodeId'))->fetchFirstColumn();
$ids = $conn->executeQuery($subQuery, ['nodeId' => $nodeId])->fetchFirstColumn();
if ([] !== $ids) {
// using subquery directly, sqlite acts unfriendly
$query = "DELETE FROM {$table} WHERE id IN (".implode(', ', $ids).')';
Expand All @@ -429,7 +432,7 @@ public function updateNode(EntityManagerInterface $em, $node, $oldParent)
$query .= ' WHERE c1.descendant = :parentId';
$query .= ' AND c2.ancestor = :nodeId';

$closures = $conn->executeQuery($query, compact('nodeId', 'parentId'))->fetchAllAssociative();
$closures = $conn->executeQuery($query, ['nodeId' => $nodeId, 'parentId' => $parentId])->fetchAllAssociative();

foreach ($closures as $closure) {
if (!$conn->insert($table, $closure)) {
Expand Down
24 changes: 20 additions & 4 deletions tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testShouldSupportChildrenHierarchyAsHtml(): void
$repo = $this->em->getRepository(self::CATEGORY);
$food = $repo->findOneBy(['title' => 'Food']);
$decorate = true;
$defaultHtmlTree = $repo->childrenHierarchy($food, false, compact('decorate'));
$defaultHtmlTree = $repo->childrenHierarchy($food, false, ['decorate' => $decorate]);

static::assertSame(
'<ul><li>Fruits</li><li>Vegitables<ul><li>Carrots</li><li>Potatoes</li></ul></li></ul>',
Expand All @@ -146,7 +146,10 @@ public function testShouldSupportChildrenHierarchyAsHtml(): void
$decoratedHtmlTree = $repo->childrenHierarchy(
$food,
false,
compact('decorate', 'nodeDecorator')
[
'decorate' => $decorate,
'nodeDecorator' => $nodeDecorator,
]
);

static::assertSame(
Expand All @@ -165,7 +168,14 @@ public function testShouldSupportChildrenHierarchyAsHtml(): void
$decoratedCliTree = $repo->childrenHierarchy(
$food,
false,
compact('decorate', 'nodeDecorator', 'rootOpen', 'rootClose', 'childOpen', 'childClose')
[
'decorate' => $decorate,
'nodeDecorator' => $nodeDecorator,
'rootOpen' => $rootOpen,
'rootClose' => $rootClose,
'childOpen' => $childOpen,
'childClose' => $childClose,
]
);
static::assertSame(
"-Fruits\n-Vegitables\n--Carrots\n--Potatoes\n",
Expand All @@ -185,7 +195,13 @@ public function testShouldSupportChildrenHierarchyAsHtml(): void
$decoratedHtmlTree = $repo->childrenHierarchy(
$food,
false,
compact('decorate', 'rootOpen', 'rootClose', 'childOpen', 'childClose')
[
'decorate' => $decorate,
'rootOpen' => $rootOpen,
'rootClose' => $rootClose,
'childOpen' => $childOpen,
'childClose' => $childClose,
]
);

static::assertSame(
Expand Down

0 comments on commit 2e0ab7a

Please sign in to comment.