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 ( f62593...e992f5 )
by Cees-Jan
10s
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 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...
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 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...
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
            if (exists($details)) {
112 1
                $this->uses[$details] = true;
113
            }
114
115 1
            $methodName = Inflector::camelize($name);
116 1
            $class->addStmt($this->createMethod($details, $name, $methodName, $details));
117
118 1
            return $stmt;
119
        }
120
121 1
        if (exists($details['type'])) {
122 1
            $this->uses[$details['type']] = true;
123
        }
124
125 1
        $methodName = Inflector::camelize($name);
126 1
        if (isset($details['method'])) {
127 1
            $methodName = $details['method'];
128
        }
129 1
        $class->addStmt($this->createMethod($details['type'], $name, $methodName, $details));
130
131 1
        return $stmt;
132
    }
133
134 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...
135
    {
136
        $property = $this->factory->property($name)
137
            ->makeProtected()
138
            ->setDocComment("/**\r\n * @var " . $type . "\r\n */")
139
        ;
140
        if (isset($details['default'])) {
141
            $property->setDefault($details['default']);
142
        }
143
144
        return $property;
145
    }
146
147 1
    protected function createMethod(
148
        string $type,
149
        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...
150
        string $methodName,
151
        $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...
152
    ): Method {
153
        $stmts = [
154 1
            new Node\Stmt\Return_(
155 1
                new Node\Expr\ConstFetch(
156 1
                    new Node\Name('null')
157
                )
158
            )
159
        ];
160
161 1
        return $this->factory->method($methodName)
162 1
            ->makePublic()
163 1
            ->setReturnType($type)
164 1
            ->setDocComment('/**
165 1
                              * @return ' . $type . '
166 1
                              */')
167 1
            ->addStmts($stmts);
168
    }
169
}
170