GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 32d13b...cb99ed )
by Cees-Jan
9s
created

InterfaceGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Tools\ResourceGenerator\FileGenerators;
4
5
use ApiClients\Foundation\Resource\ResourceInterface;
6
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
7
use Doctrine\Common\Inflector\Inflector;
8
use PhpParser\BuilderFactory;
9
use PhpParser\Node;
10
use function ApiClients\Tools\ResourceGenerator\exists;
11
12
final class InterfaceGenerator implements FileGeneratorInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $yaml;
18
19
    /**
20
     * @var array
21
     */
22
    protected $uses = [
23
        ResourceInterface::class => true,
24
    ];
25
26
    /**
27
     * InterfaceGenerator constructor.
28
     * @param array $yaml
29
     */
30
    public function __construct(array $yaml)
31
    {
32
        $this->yaml = $yaml;
33
    }
34
35
    /**
36
     * @return string
37
     */
38 View Code Duplication
    public function getFilename(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        return $this->yaml['src']['path'] .
41
            DIRECTORY_SEPARATOR .
42
            str_replace('\\', DIRECTORY_SEPARATOR, $this->yaml['class']) .
43
            'Interface.php'
44
        ;
45
    }
46
47
    /**
48
     * @return Node
49
     */
50
    public function generate(): Node
51
    {
52
        $classChunks = explode('\\', $this->yaml['class']);
53
        $baseClass = array_pop($classChunks);
54
        $className = $baseClass . 'Interface';
55
        $namespace = $this->yaml['src']['namespace'];
56 View Code Duplication
        if (count($classChunks) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            $namespace .= '\\' . implode('\\', $classChunks);
58
            $namespace = str_replace('\\\\', '\\', $namespace);
59
        }
60
61
        $factory = new BuilderFactory();
62
63
        $class = $factory->interface($className)
64
            ->extend('ResourceInterface');
65
66
        $class->addStmt(
67
            new Node\Stmt\ClassConst(
68
                [
69
                    new Node\Const_(
70
                        'HYDRATE_CLASS',
71
                        new Node\Scalar\String_(
72
                            $this->yaml['class']
73
                        )
74
                    )
75
                ]
76
            )
77
        );
78
79
        foreach ($this->yaml['properties'] as $name => $details) {
80
            $type = $details;
81
            if (is_array($details)) {
82
                $type = $details['type'];
83
            }
84
85
            if (exists($type)) {
86
                $this->uses[$type] = true;
87
            }
88
89
            $methodName = Inflector::camelize($name);
90
            if (is_array($details) && isset($details['method'])) {
91
                $methodName = $details['method'];
92
            }
93
            $class->addStmt(
94
                $factory->method($methodName)
95
                    ->makePublic()
96
                    ->setReturnType($type)
97
                    ->setDocComment(
98
                        "/**\r\n * @return " . $type . "\r\n */"
99
                    )
100
            );
101
        }
102
103
        $stmt = $factory->namespace($namespace);
104
105
        ksort($this->uses);
106
        foreach ($this->uses as $useClass => $bool) {
107
            $stmt = $stmt
108
                ->addStmt($factory->use($useClass))
109
            ;
110
        }
111
112
        return $stmt->addStmt($class)
113
            ->getNode()
114
        ;
115
    }
116
}
117