HasNames   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 1
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getNames() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\Enum\Site;
8
9
/**
10
 * Trait for all repositories with a name.
11
 */
12
trait HasNames
13
{
14
    /**
15
     * Returns an array of names and their ID for all entries in the DB.
16
     */
17 6
    public function getNames(?Site $site = null): array
18
    {
19 6
        $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

19
        $connection = $this->/** @scrutinizer ignore-call */ getEntityManager()->getConnection();
Loading history...
20 6
        $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

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