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
Pull Request — master (#7)
by Cees-Jan
05:05 queued 02:28
created

BaseClassGenerator::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
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\AbstractResource;
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 BaseClassGenerator 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
        AbstractResource::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
        if (isset($this->yaml['uses']) && is_array($this->yaml['uses'])) {
46 1
            $this->uses += $this->yaml['uses'];
47
        }
48 1
        $this->factory = new BuilderFactory();
49 1
    }
50
51
    /**
52
     * @return string
53
     */
54 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...
55
    {
56 1
        return $this->yaml['src']['path'] .
57 1
            DIRECTORY_SEPARATOR .
58 1
            str_replace('\\', DIRECTORY_SEPARATOR, $this->yaml['class']) .
59 1
            '.php'
60
        ;
61
    }
62
63
    /**
64
     * @return Node
65
     */
66 1
    public function generate(): Node
67
    {
68 1
        $classChunks = explode('\\', $this->yaml['class']);
69 1
        $className = array_pop($classChunks);
70 1
        $namespace = $this->yaml['src']['namespace'];
71 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...
72 1
            $namespace .= '\\' . implode('\\', $classChunks);
73 1
            $namespace = str_replace('\\\\', '\\', $namespace);
74
        }
75
76 1
        $class = $this->factory->class($className)
77 1
            ->implement($className . 'Interface')
78 1
            ->extend('AbstractResource')
79 1
            ->makeAbstract();
80
81 1
        $stmt = $this->factory->namespace($namespace);
82 1
        foreach ($this->yaml['properties'] as $name => $details) {
83 1
            $stmt = $this->processProperty($class, $stmt, $name, $details);
84
        }
85
86 1
        ksort($this->uses);
87 1
        foreach ($this->uses as $useClass => $bool) {
88
            $stmt = $stmt
89 1
                ->addStmt($this->factory->use($useClass))
90
            ;
91
        }
92
93 1
        if (isset($this->yaml['annotations'])) {
94 1
            ksort($this->yaml['annotations']);
95 1
            foreach ($this->yaml['annotations'] as $annotation => $details) {
96 1
                $nestedResources = [];
97 1
                foreach ($details as $key => $value) {
98 1
                    $nestedResources[] = $key . '="' . $value . '"';
99
                }
100 1
                $this->docBlock[] = '@' . $annotation . '(' . implode(', ', $nestedResources) . ')';
101
            }
102
        }
103
104 1
        if (count($this->docBlock) > 0) {
105 1
            $class->setDocComment("/**\r\n * " . implode("\r\n * ", $this->docBlock) . "\r\n */");
106
        }
107
108 1
        return $stmt->addStmt($class)->getNode();
109
    }
110
111
    /**
112
     * @param \PhpParser\Builder\Class_ $class
113
     */
114 1
    protected function processProperty($class, $stmt, $name, $details)
115
    {
116 1
        if (is_string($details)) {
117 1
            if (exists($details)) {
118 1
                $this->uses[$details] = true;
119
            }
120
121 1
            $class->addStmt($this->createProperty($details, $name, $details));
122 1
            $methodName = Inflector::camelize($name);
123 1
            $class->addStmt($this->createMethod($details, $name, $methodName, $details));
124 1
            return $stmt;
125
        }
126
127 1
        if (exists($details['type'])) {
128 1
            $this->uses[$details['type']] = true;
129
        }
130 1
        if (isset($details['wrap']) && exists($details['wrap'])) {
131 1
            $this->uses[$details['wrap']] = true;
132
        }
133
134 1
        $class->addStmt($this->createProperty($details['type'], $name, $details));
135 1
        if (isset($details['wrap'])) {
136 1
            $class->addStmt($this->createProperty($details['wrap'], $name . '_wrapped', $details));
137
        }
138
139 1
        $methodName = Inflector::camelize($name);
140 1
        if (isset($details['method'])) {
141 1
            $methodName = $details['method'];
142
        }
143 1
        $class->addStmt($this->createMethod($details['type'], $name, $methodName, $details));
144
145 1
        return $stmt;
146
    }
147
148 1
    protected function createProperty(string $type, string $name, $details): Property
149
    {
150 1
        $property = $this->factory->property($name)
151 1
            ->makeProtected()
152 1
            ->setDocComment("/**\r\n * @var " . $type . "\r\n */")
153
        ;
154 1
        if (isset($details['default'])) {
155 1
            $property->setDefault($details['default']);
156
        }
157
158 1
        return $property;
159
    }
160
161 1
    protected function createMethod(
162
        string $type,
163
        string $name,
164
        string $methodName,
165
        $details
166
    ): Method {
167
        $stmts = [
168 1
            new Node\Stmt\Return_(
169 1
                new Node\Expr\PropertyFetch(
170 1
                    new Node\Expr\Variable('this'),
171
                    $name
172
                )
173
            )
174
        ];
175
176 1
        if (isset($details['wrap'])) {
177 1
            $stmts = [];
178 1
            $stmts[] = new Node\Stmt\If_(
179 1
                new Node\Expr\Instanceof_(
180 1
                    new Node\Expr\PropertyFetch(
181 1
                        new Node\Expr\Variable('this'),
182 1
                        $name . '_wrapped'
183
                    ),
184 1
                    new Node\Name($details['wrap'])
185
                ),
186
                [
187
                    'stmts' => [
188 1
                        new Node\Stmt\Return_(
189 1
                            new Node\Expr\PropertyFetch(
190 1
                                new Node\Expr\Variable('this'),
191 1
                                $name . '_wrapped'
192
                            )
193
                        ),
194
                    ],
195
                ]
196
            );
197 1
            $stmts[] = new Node\Expr\Assign(
198 1
                new Node\Expr\PropertyFetch(
199 1
                    new Node\Expr\Variable('this'),
200 1
                    $name . '_wrapped'
201
                ),
202 1
                new Node\Expr\New_(
203 1
                    new Node\Name($details['wrap']),
204
                    [
0 ignored issues
show
Documentation introduced by
array(new \PhpParser\Nod...riable('this'), $name)) is of type array<integer,object<Php...Expr\\PropertyFetch>"}>, but the function expects a array<integer,object<PhpParser\Node\Arg>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
205 1
                        new Node\Expr\PropertyFetch(
206 1
                            new Node\Expr\Variable('this'),
207
                            $name
208
                        ),
209
                    ]
210
                )
211
            );
212 1
            $stmts[] = new Node\Stmt\Return_(
213 1
                new Node\Expr\PropertyFetch(
214 1
                    new Node\Expr\Variable('this'),
215 1
                    $name . '_wrapped'
216
                )
217
            );
218
        }
219
220 1
        return $this->factory->method($methodName)
221 1
            ->makePublic()
222 1
            ->setReturnType($type)
223 1
            ->setDocComment('/**
224 1
                              * @return ' . $type . '
225 1
                              */')
226 1
            ->addStmts($stmts);
227
    }
228
}
229