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 3, 2023
1 parent e7c6953 commit 2fe7a39
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 21 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 @@ -262,7 +262,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 @@ -277,7 +277,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 @@ -292,7 +295,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 @@ -387,7 +390,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
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 2fe7a39

Please sign in to comment.