Passed
Pull Request — master (#62)
by David
01:52
created

NamingStrategy::getOutputTypeName()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 4
nc 6
nop 2
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
7
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
8
9
class NamingStrategy implements NamingStrategyInterface
10
{
11
    /**
12
     * Returns the name of the GraphQL interface from a name of a concrete class (when the interface is created
13
     * automatically to manage inheritance)
14
     */
15
    public function getInterfaceNameFromConcreteName(string $concreteType): string
16
    {
17
        return $concreteType.'Interface';
18
    }
19
20
    /**
21
     * Returns the GraphQL output object type name based on the type className and the Type annotation.
22
     */
23
    public function getOutputTypeName(string $typeClassName, Type $type): string
24
    {
25
        if ($prevPos = strrpos($typeClassName, '\\')) {
26
            $typeClassName = substr($typeClassName, $prevPos + 1);
27
        }
28
        // By default, if the class name ends with Type, let's take the name of the class for the type
29
        if (substr($typeClassName, -4) === 'Type') {
30
            return substr($typeClassName, 0, -4);
31
        }
32
        // Else, let's take the name of the targeted class
33
        $typeClassName = $type->getClass();
34
        if ($prevPos = strrpos($typeClassName, '\\')) {
35
            $typeClassName = substr($typeClassName, $prevPos + 1);
36
        }
37
        return $typeClassName;
38
    }
39
}
40