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

HasNames   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 1
b 0
f 0
dl 0
loc 22
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
/**
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