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.

BaseClassGenerator::getFilename()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 8
loc 8
ccs 5
cts 5
cp 1
crap 1
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\Hydrator\Annotation\EmptyResource;
6
use ApiClients\Foundation\Resource\AbstractResource;
7
use function ApiClients\Tools\ResourceGenerator\exists;
8
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
9
use Doctrine\Common\Inflector\Inflector;
10
use PhpParser\Builder\Method;
11
use PhpParser\Builder\Property;
12
use PhpParser\BuilderFactory;
13
use PhpParser\Node;
14
15
final class BaseClassGenerator implements FileGeneratorInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $yaml;
21
22
    /**
23
     * @var BuilderFactory
24
     */
25
    protected $factory;
26
27
    /**
28
     * @var string[]
29
     */
30
    protected $docBlock = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $uses = [
36
        AbstractResource::class => true,
37
        EmptyResource::class => true,
38
    ];
39
40
    /**
41
     * InterfaceGenerator constructor.
42
     * @param array $yaml
43
     */
44 1
    public function __construct(array $yaml)
45
    {
46 1
        $this->yaml = $yaml;
47 1
        if (isset($this->yaml['uses']) && is_array($this->yaml['uses'])) {
48 1
            $this->uses += $this->yaml['uses'];
49
        }
50 1
        $this->factory = new BuilderFactory();
51 1
    }
52
53
    /**
54
     * @return string
55
     */
56 1 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...
57
    {
58 1
        return $this->yaml['src']['path'] .
59 1
            DIRECTORY_SEPARATOR .
60 1
            str_replace('\\', DIRECTORY_SEPARATOR, $this->yaml['class']) .
61 1
            '.php'
62
        ;
63
    }
64
65
    /**
66
     * @return Node
67
     */
68 1
    public function generate(): Node
69
    {
70 1
        $classChunks = explode('\\', $this->yaml['class']);
71 1
        $className = array_pop($classChunks);
72 1
        $namespace = $this->yaml['src']['namespace'];
73 1
        if (count($classChunks) > 0) {
74 1
            $namespace .= '\\' . implode('\\', $classChunks);
75 1
            $namespace = str_replace('\\\\', '\\', $namespace);
76
        }
77
78 1
        $class = $this->factory->class($className)
79 1
            ->implement($className . 'Interface')
80 1
            ->extend('AbstractResource')
81 1
            ->makeAbstract();
82
83 1
        $stmt = $this->factory->namespace($namespace);
84 1
        foreach ($this->yaml['properties'] as $name => $details) {
85 1
            $stmt = $this->processProperty($class, $stmt, $name, $details);
86
        }
87
88 1
        ksort($this->uses);
89 1
        foreach ($this->uses as $useClass => $bool) {
90
            $stmt = $stmt
91 1
                ->addStmt($this->factory->use($useClass))
92
            ;
93
        }
94
95 1
        if (isset($this->yaml['annotations'])) {
96 1
            ksort($this->yaml['annotations']);
97 1
            foreach ($this->yaml['annotations'] as $annotation => $details) {
98 1
                $nestedResources = [];
99 1
                foreach ($details as $key => $value) {
100 1
                    $nestedResources[] = $key . '="' . $value . '"';
101
                }
102 1
                $this->docBlock[] = '@' .
103 1
                    $annotation .
104 1
                    "(\r\n *     " .
105 1
                    implode(",\r\n *     ", $nestedResources) .
106 1
                    "\r\n * )"
107
                ;
108
            }
109
        }
110
111 1
        $namespacePrefix = ltrim(implode('\\', $classChunks) . '\\', '\\');
112 1
        $this->docBlock[] = '@EmptyResource("' . $namespacePrefix . 'Empty' . $className . '")';
113
114 1
        if (count($this->docBlock) > 0) {
115 1
            $class->setDocComment("/**\r\n * " . implode("\r\n * ", $this->docBlock) . "\r\n */");
116
        }
117
118 1
        return $stmt->addStmt($class)->getNode();
119
    }
120
121
    /**
122
     * @param \PhpParser\Builder\Class_ $class
123
     * @param mixed                     $stmt
124
     * @param mixed                     $name
125
     * @param mixed                     $details
126
     */
127 1
    protected function processProperty($class, $stmt, $name, $details)
128
    {
129 1
        if (is_string($details)) {
130 1
            $types = explode('|', $details);
131 1
            foreach ($types as $type) {
132 1
                if (exists($type)) {
133 1
                    $this->uses[$type] = true;
134
                }
135
            }
136
137 1
            $class->addStmt($this->createProperty($details, $name, $details));
138 1
            $methodName = Inflector::camelize($name);
139 1
            $class->addStmt($this->createMethod($types, $name, $methodName, $details));
140
141 1
            return $stmt;
142
        }
143
144 1
        $types = explode('|', $details['type']);
145 1
        foreach ($types as $type) {
146 1
            if (exists($type)) {
147 1
                $this->uses[$type] = true;
148
            }
149
        }
150 1
        if (isset($details['wrap']) && exists($details['wrap'])) {
151 1
            $this->uses[$details['wrap']] = true;
152
        }
153
154 1
        $class->addStmt($this->createProperty($details['type'], $name, $details));
155 1
        if (isset($details['wrap'])) {
156 1
            $class->addStmt($this->createProperty($details['wrap'], $name . '_wrapped', $details));
157
        }
158
159 1
        $methodName = Inflector::camelize($name);
160 1
        if (isset($details['method'])) {
161 1
            $methodName = $details['method'];
162
        }
163 1
        $class->addStmt($this->createMethod($types, $name, $methodName, $details));
164
165 1
        return $stmt;
166
    }
167
168 1 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...
169
    {
170 1
        $property = $this->factory->property($name)
171 1
            ->makeProtected()
172 1
            ->setDocComment("/**\r\n * @var " . $type . "\r\n */")
173
        ;
174 1
        if (isset($details['default'])) {
175 1
            $property->setDefault($details['default']);
176
        }
177
178 1
        return $property;
179
    }
180
181 1
    protected function createMethod(
182
        array $types,
183
        string $name,
184
        string $methodName,
185
        $details
186
    ): Method {
187
        $stmts = [
188 1
            new Node\Stmt\Return_(
189 1
                new Node\Expr\PropertyFetch(
190 1
                    new Node\Expr\Variable('this'),
191 1
                    $name
192
                )
193
            ),
194
        ];
195
196 1
        if (isset($details['wrap'])) {
197 1
            $stmts = [];
198 1
            $stmts[] = new Node\Stmt\If_(
199 1
                new Node\Expr\Instanceof_(
200 1
                    new Node\Expr\PropertyFetch(
201 1
                        new Node\Expr\Variable('this'),
202 1
                        $name . '_wrapped'
203
                    ),
204 1
                    new Node\Name($details['wrap'])
205
                ),
206
                [
207
                    'stmts' => [
208 1
                        new Node\Stmt\Return_(
209 1
                            new Node\Expr\PropertyFetch(
210 1
                                new Node\Expr\Variable('this'),
211 1
                                $name . '_wrapped'
212
                            )
213
                        ),
214
                    ],
215
                ]
216
            );
217 1
            $stmts[] = new Node\Expr\Assign(
218 1
                new Node\Expr\PropertyFetch(
219 1
                    new Node\Expr\Variable('this'),
220 1
                    $name . '_wrapped'
221
                ),
222 1
                new Node\Expr\New_(
223 1
                    new Node\Name($details['wrap']),
224
                    [
225 1
                        new Node\Expr\PropertyFetch(
226 1
                            new Node\Expr\Variable('this'),
227 1
                            $name
228
                        ),
229
                    ]
230
                )
231
            );
232 1
            $stmts[] = new Node\Stmt\Return_(
233 1
                new Node\Expr\PropertyFetch(
234 1
                    new Node\Expr\Variable('this'),
235 1
                    $name . '_wrapped'
236
                )
237
            );
238
        }
239
240 1
        $method = $this->factory->method($methodName)
241 1
            ->makePublic()
242 1
            ->setDocComment('/**
243 1
                              * @return ' . implode('|', $types) . '
244
                              */')
245 1
            ->addStmts($stmts);
246 1
        if (count($types) === 1) {
247 1
            $method = $method->setReturnType($types[0]);
248
        }
249
250 1
        return $method;
251
    }
252
}
253