Failed Conditions
Pull Request — master (#10)
by Adrien
02:32
created

UniqueNameFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 37
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createAliasName() 0 8 2
A createParameterName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Factory;
6
7
/**
8
 * A factory to create unique but predictable names for aliases and parameters.
9
 *
10
 * Uniqueness is guaranteed within the same instance of factory only.
11
 */
12
final class UniqueNameFactory
13
{
14
    /**
15
     * @var int[]
16
     */
17
    private $aliasCount = [];
18
19
    /**
20
     * @var int
21
     */
22
    private $parameterCount = 1;
23
24
    /**
25
     * Return a string to be used as parameter name in a query
26
     *
27
     * @return string
28
     */
29 24
    public function createParameterName(): string
30
    {
31 24
        return 'filter' . $this->parameterCount++;
32
    }
33
34
    /**
35
     * Return a string to be used as alias name in a query
36
     *
37
     * @param string $className
38
     *
39
     * @return string
40
     */
41 13
    public function createAliasName(string $className): string
42
    {
43 13
        $alias = lcfirst(preg_replace('~^.*\\\\~', '', $className));
44 13
        if (!isset($this->aliasCount[$alias])) {
45 13
            $this->aliasCount[$alias] = 1;
46
        }
47
48 13
        return $alias . $this->aliasCount[$alias]++;
49
    }
50
}
51