Passed
Push — master ( 4aff5b...31f13e )
by David
13:07
created

HasNames::getNames()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
rs 9.9332
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
/**
8
 * Trait for all repositories with a name.
9
 */
10
trait HasNames
11
{
12
    /**
13
     * Returns an array of names and their ID for all entries in the DB.
14
     */
15
    public function getNames(?string $site = null): array
16
    {
17
        $connection = $this->getEntityManager()->getConnection();
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        $connection = $this->/** @scrutinizer ignore-call */ getEntityManager()->getConnection();
Loading history...
18
        $table = $this->getClassMetadata()->getTableName();
0 ignored issues
show
Bug introduced by
It seems like getClassMetadata() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        $table = $this->/** @scrutinizer ignore-call */ getClassMetadata()->getTableName();
Loading history...
19
        $table = $connection->quoteIdentifier($table);
20
21
        $where = $site ? ' WHERE site = ' . $connection->quote($site) : '';
22
        $sql = 'SELECT id, name FROM ' . $table . $where . ' ORDER BY name ASC';
23
24
        $records = $connection->executeQuery($sql)->fetchAllAssociative();
25
26
        $result = [];
27
        foreach ($records as $r) {
28
            $result[$r['name']] = $r['id'];
29
        }
30
31
        return $result;
32
    }
33
}
34