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.

EmptyBaseClassGenerator::getFilename()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 21
ccs 15
cts 15
cp 1
crap 2
rs 9.584
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 function ApiClients\Tools\ResourceGenerator\exists;
7
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
8
use Doctrine\Common\Inflector\Inflector;
9
use PhpParser\Builder\Method;
10
use PhpParser\Builder\Property;
11
use PhpParser\BuilderFactory;
12
use PhpParser\Node;
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 1
    public function __construct(array $yaml)
43
    {
44 1
        $this->yaml = $yaml;
45 1
        $this->factory = new BuilderFactory();
46 1
    }
47
48
    /**
49
     * @return string
50
     */
51 1
    public function getFilename(): string
52
    {
53 1
        $classChunks = explode('\\', $this->yaml['class']);
54 1
        $className = array_pop($classChunks);
55 1
        $className = 'Empty' . $className;
56 1
        $namespace = '';
57 1
        if (count($classChunks) > 0) {
58 1
            $namespace .= '\\' . implode('\\', $classChunks);
59 1
            $namespace = str_replace('\\\\', '\\', $namespace);
60
        }
61
62 1
        return $this->yaml['src']['path'] .
63 1
            DIRECTORY_SEPARATOR .
64 1
            str_replace(
65 1
                '\\',
66 1
                DIRECTORY_SEPARATOR,
67 1
                $namespace . '\\' . $className
68
            ) .
69 1
            '.php'
70
        ;
71
    }
72
73
    /**
74
     * @return Node
75
     */
76 1
    public function generate(): Node
77
    {
78 1
        $classChunks = explode('\\', $this->yaml['class']);
79 1
        $className = array_pop($classChunks);
80 1
        $namespace = $this->yaml['src']['namespace'];
81 1
        if (count($classChunks) > 0) {
82 1
            $namespace .= '\\' . implode('\\', $classChunks);
83 1
            $namespace = str_replace('\\\\', '\\', $namespace);
84
        }
85
86 1
        $class = $this->factory->class('Empty' . $className)
87 1
            ->implement($className . 'Interface')
88 1
            ->implement('EmptyResourceInterface')
89 1
            ->makeAbstract();
90
91 1
        $stmt = $this->factory->namespace($namespace);
92 1
        foreach ($this->yaml['properties'] as $name => $details) {
93 1
            $stmt = $this->processProperty($class, $stmt, $name, $details);
94
        }
95
96 1
        ksort($this->uses);
97 1
        foreach ($this->uses as $useClass => $bool) {
98
            $stmt = $stmt
99 1
                ->addStmt($this->factory->use($useClass))
100
            ;
101
        }
102
103 1
        return $stmt->addStmt($class)->getNode();
104
    }
105
106
    /**
107
     * @param \PhpParser\Builder\Class_ $class
108
     * @param mixed                     $stmt
109
     * @param mixed                     $name
110
     * @param mixed                     $details
111
     */
112 1
    protected function processProperty($class, $stmt, $name, $details)
113
    {
114 1
        if (is_string($details)) {
115 1
            $types = explode('|', $details);
116 1
            foreach ($types as $type) {
117 1
                if (exists($type)) {
118 1
                    $this->uses[$type] = true;
119
                }
120
            }
121
122 1
            $methodName = Inflector::camelize($name);
123 1
            $class->addStmt($this->createMethod($types, $name, $methodName, $details));
124
125 1
            return $stmt;
126
        }
127
128 1
        $types = explode('|', $details['type']);
129 1
        foreach ($types as $type) {
130 1
            if (exists($type)) {
131 1
                $this->uses[$type] = true;
132
            }
133
        }
134
135 1
        $methodName = Inflector::camelize($name);
136 1
        if (isset($details['method'])) {
137 1
            $methodName = $details['method'];
138
        }
139 1
        $class->addStmt($this->createMethod($types, $name, $methodName, $details));
140
141 1
        return $stmt;
142
    }
143
144 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...
145
    {
146
        $property = $this->factory->property($name)
147
            ->makeProtected()
148
            ->setDocComment("/**\r\n * @var " . $type . "\r\n */")
149
        ;
150
        if (isset($details['default'])) {
151
            $property->setDefault($details['default']);
152
        }
153
154
        return $property;
155
    }
156
157 1
    protected function createMethod(
158
        array $types,
159
        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...
160
        string $methodName,
161
        $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...
162
    ): Method {
163
        $stmts = [
164 1
            new Node\Stmt\Return_(
165 1
                new Node\Expr\ConstFetch(
166 1
                    new Node\Name('null')
167
                )
168
            ),
169
        ];
170
171 1
        $method = $this->factory->method($methodName)
172 1
            ->makePublic()
173 1
            ->setDocComment('/**
174 1
                              * @return ' . implode('|', $types) . '
175
                              */')
176 1
            ->addStmts($stmts);
177 1
        if (count($types) === 1) {
178 1
            $method = $method->setReturnType($types[0]);
179
        }
180
181 1
        return $method;
182
    }
183
}
184