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 ( 7547f4...ddfb6d )
by Cees-Jan
02:26
created

EmptyBaseClassGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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 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 1
        return $this->yaml['src']['path'] .
62 1
            DIRECTORY_SEPARATOR .
63 1
            str_replace(
64 1
                '\\',
65 1
                DIRECTORY_SEPARATOR,
66 1
                $namespace . '\\' . $className
67
            ) .
68 1
            '.php'
69
        ;
70
    }
71
72
    /**
73
     * @return Node
74
     */
75 1
    public function generate(): Node
76
    {
77 1
        $classChunks = explode('\\', $this->yaml['class']);
78 1
        $className = array_pop($classChunks);
79 1
        $namespace = $this->yaml['src']['namespace'];
80 1
        if (count($classChunks) > 0) {
81 1
            $namespace .= '\\' . implode('\\', $classChunks);
82 1
            $namespace = str_replace('\\\\', '\\', $namespace);
83
        }
84
85 1
        $class = $this->factory->class('Empty' . $className)
86 1
            ->implement($className . 'Interface')
87 1
            ->implement('EmptyResourceInterface')
88 1
            ->makeAbstract();
89
90 1
        $stmt = $this->factory->namespace($namespace);
91 1
        foreach ($this->yaml['properties'] as $name => $details) {
92 1
            $stmt = $this->processProperty($class, $stmt, $name, $details);
93
        }
94
95 1
        ksort($this->uses);
96 1
        foreach ($this->uses as $useClass => $bool) {
97
            $stmt = $stmt
98 1
                ->addStmt($this->factory->use($useClass))
99
            ;
100
        }
101
102 1
        return $stmt->addStmt($class)->getNode();
103
    }
104
105
    /**
106
     * @param \PhpParser\Builder\Class_ $class
107
     */
108 1
    protected function processProperty($class, $stmt, $name, $details)
109
    {
110 1
        if (is_string($details)) {
111 1
            $types = explode('|', $details);
112 1
            foreach ($types as $type) {
113 1
                if (exists($type)) {
114 1
                    $this->uses[$type] = true;
115
                }
116
            }
117
118 1
            $methodName = Inflector::camelize($name);
119 1
            $class->addStmt($this->createMethod($types, $name, $methodName, $details));
120
121 1
            return $stmt;
122
        }
123
124 1
        $types = explode('|', $details['type']);
125 1
        foreach ($types as $type) {
126 1
            if (exists($type)) {
127 1
                $this->uses[$type] = true;
128
            }
129
        }
130
131 1
        $methodName = Inflector::camelize($name);
132 1
        if (isset($details['method'])) {
133 1
            $methodName = $details['method'];
134
        }
135 1
        $class->addStmt($this->createMethod($types, $name, $methodName, $details));
136
137 1
        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 1
    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 1
            new Node\Stmt\Return_(
161 1
                new Node\Expr\ConstFetch(
162 1
                    new Node\Name('null')
163
                )
164
            )
165
        ];
166
167 1
        $method = $this->factory->method($methodName)
168 1
            ->makePublic()
169 1
            ->setDocComment('/**
170 1
                              * @return ' . implode('|', $types) . '
171
                              */')
172 1
            ->addStmts($stmts);
173 1
        if (count($types) === 1) {
174 1
            $method = $method->setReturnType($types[0]);
175
        }
176 1
        return $method;
177
    }
178
}
179