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 ( 65e9a9...017ab2 )
by Cees-Jan
08:12
created

EmptyBaseClassGenerator::getFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 20
rs 9.4285
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\EmptyResourceInterface;
6
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
7
use Doctrine\Common\Inflector\Inflector;
8
use PhpParser\Builder\Method;
9
use PhpParser\Builder\Property;
10
use PhpParser\BuilderFactory;
11
use PhpParser\Node;
12
use function ApiClients\Tools\ResourceGenerator\exists;
13
14
final class EmptyBaseClassGenerator implements FileGeneratorInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $yaml;
20
21
    /**
22
     * @var BuilderFactory
23
     */
24
    protected $factory;
25
26
    /**
27
     * @var string[]
28
     */
29
    protected $docBlock = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $uses = [
35
        EmptyResourceInterface::class => true,
36
    ];
37
38
    /**
39
     * InterfaceGenerator constructor.
40
     * @param array $yaml
41
     */
42
    public function __construct(array $yaml)
43
    {
44
        $this->yaml = $yaml;
45
        $this->factory = new BuilderFactory();
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getFilename(): string
52
    {
53
        $classChunks = explode('\\', $this->yaml['class']);
54
        $className = array_pop($classChunks);
55
        $className = 'Empty' . $className;
56
        $namespace = '';
57
        if (count($classChunks) > 0) {
58
            $namespace .= '\\' . implode('\\', $classChunks);
59
            $namespace = str_replace('\\\\', '\\', $namespace);
60
        }
61
        return $this->yaml['src']['path'] .
62
            DIRECTORY_SEPARATOR .
63
            str_replace(
64
                '\\',
65
                DIRECTORY_SEPARATOR,
66
                $namespace . '\\' . $className
67
            ) .
68
            '.php'
69
        ;
70
    }
71
72
    /**
73
     * @return Node
74
     */
75
    public function generate(): Node
76
    {
77
        $classChunks = explode('\\', $this->yaml['class']);
78
        $className = array_pop($classChunks);
79
        $namespace = $this->yaml['src']['namespace'];
80
        if (count($classChunks) > 0) {
81
            $namespace .= '\\' . implode('\\', $classChunks);
82
            $namespace = str_replace('\\\\', '\\', $namespace);
83
        }
84
85
        $class = $this->factory->class('Empty' . $className)
86
            ->implement($className . 'Interface')
87
            ->implement('EmptyResourceInterface')
88
            ->makeAbstract();
89
90
        $stmt = $this->factory->namespace($namespace);
91
        foreach ($this->yaml['properties'] as $name => $details) {
92
            $stmt = $this->processProperty($class, $stmt, $name, $details);
93
        }
94
95
        ksort($this->uses);
96
        foreach ($this->uses as $useClass => $bool) {
97
            $stmt = $stmt
98
                ->addStmt($this->factory->use($useClass))
99
            ;
100
        }
101
102
        return $stmt->addStmt($class)->getNode();
103
    }
104
105
    /**
106
     * @param \PhpParser\Builder\Class_ $class
107
     */
108
    protected function processProperty($class, $stmt, $name, $details)
109
    {
110
        if (is_string($details)) {
111
            $types = explode('|', $details);
112
            foreach ($types as $type) {
113
                if (exists($type)) {
114
                    $this->uses[$type] = true;
115
                }
116
            }
117
118
            $methodName = Inflector::camelize($name);
119
            $class->addStmt($this->createMethod($types, $name, $methodName, $details));
120
121
            return $stmt;
122
        }
123
124
        $types = explode('|', $details['type']);
125
        foreach ($types as $type) {
126
            if (exists($type)) {
127
                $this->uses[$type] = true;
128
            }
129
        }
130
131
        $methodName = Inflector::camelize($name);
132
        if (isset($details['method'])) {
133
            $methodName = $details['method'];
134
        }
135
        $class->addStmt($this->createMethod($types, $name, $methodName, $details));
136
137
        return $stmt;
138
    }
139
140 View Code Duplication
    protected function createProperty(string $type, string $name, $details): Property
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...
141
    {
142
        $property = $this->factory->property($name)
143
            ->makeProtected()
144
            ->setDocComment("/**\r\n * @var " . $type . "\r\n */")
145
        ;
146
        if (isset($details['default'])) {
147
            $property->setDefault($details['default']);
148
        }
149
150
        return $property;
151
    }
152
153
    protected function createMethod(
154
        array $types,
155
        string $name,
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
        string $methodName,
157
        $details
0 ignored issues
show
Unused Code introduced by
The parameter $details is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
    ): Method {
159
        $stmts = [
160
            new Node\Stmt\Return_(
161
                new Node\Expr\ConstFetch(
162
                    new Node\Name('null')
163
                )
164
            )
165
        ];
166
167
        $method = $this->factory->method($methodName)
168
            ->makePublic()
169
            ->setDocComment('/**
170
                              * @return ' . implode('|', $types) . '
171
                              */')
172
            ->addStmts($stmts);
173
        if (count($types) === 1) {
174
            $method = $method->setReturnType($types[0]);
175
        }
176
        return $method;
177
    }
178
}
179