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 — migrate-to-friendsofphp/php-cs... ( 0b5a57...4a9bcb )
by Cees-Jan
02:20
created

BaseClassGenerator   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 233
Duplicated Lines 8.58 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 20
loc 233
ccs 111
cts 111
cp 1
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getFilename() 8 8 1
C generate() 0 52 8
D processProperty() 0 39 10
A createProperty() 12 12 2
A createMethod() 0 70 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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
use function ApiClients\Tools\ResourceGenerator\exists;
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
     */
124 1
    protected function processProperty($class, $stmt, $name, $details)
125
    {
126 1
        if (is_string($details)) {
127 1
            $types = explode('|', $details);
128 1
            foreach ($types as $type) {
129 1
                if (exists($type)) {
130 1
                    $this->uses[$type] = true;
131
                }
132
            }
133
134 1
            $class->addStmt($this->createProperty($details, $name, $details));
135 1
            $methodName = Inflector::camelize($name);
136 1
            $class->addStmt($this->createMethod($types, $name, $methodName, $details));
137 1
            return $stmt;
138
        }
139
140 1
        $types = explode('|', $details['type']);
141 1
        foreach ($types as $type) {
142 1
            if (exists($type)) {
143 1
                $this->uses[$type] = true;
144
            }
145
        }
146 1
        if (isset($details['wrap']) && exists($details['wrap'])) {
147 1
            $this->uses[$details['wrap']] = true;
148
        }
149
150 1
        $class->addStmt($this->createProperty($details['type'], $name, $details));
151 1
        if (isset($details['wrap'])) {
152 1
            $class->addStmt($this->createProperty($details['wrap'], $name . '_wrapped', $details));
153
        }
154
155 1
        $methodName = Inflector::camelize($name);
156 1
        if (isset($details['method'])) {
157 1
            $methodName = $details['method'];
158
        }
159 1
        $class->addStmt($this->createMethod($types, $name, $methodName, $details));
160
161 1
        return $stmt;
162
    }
163
164 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...
165
    {
166 1
        $property = $this->factory->property($name)
167 1
            ->makeProtected()
168 1
            ->setDocComment("/**\r\n * @var " . $type . "\r\n */")
169
        ;
170 1
        if (isset($details['default'])) {
171 1
            $property->setDefault($details['default']);
172
        }
173
174 1
        return $property;
175
    }
176
177 1
    protected function createMethod(
178
        array $types,
179
        string $name,
180
        string $methodName,
181
        $details
182
    ): Method {
183
        $stmts = [
184 1
            new Node\Stmt\Return_(
185 1
                new Node\Expr\PropertyFetch(
186 1
                    new Node\Expr\Variable('this'),
187
                    $name
188
                )
189
            )
190
        ];
191
192 1
        if (isset($details['wrap'])) {
193 1
            $stmts = [];
194 1
            $stmts[] = new Node\Stmt\If_(
195 1
                new Node\Expr\Instanceof_(
196 1
                    new Node\Expr\PropertyFetch(
197 1
                        new Node\Expr\Variable('this'),
198 1
                        $name . '_wrapped'
199
                    ),
200 1
                    new Node\Name($details['wrap'])
201
                ),
202
                [
203
                    'stmts' => [
204 1
                        new Node\Stmt\Return_(
205 1
                            new Node\Expr\PropertyFetch(
206 1
                                new Node\Expr\Variable('this'),
207 1
                                $name . '_wrapped'
208
                            )
209
                        ),
210
                    ],
211
                ]
212
            );
213 1
            $stmts[] = new Node\Expr\Assign(
214 1
                new Node\Expr\PropertyFetch(
215 1
                    new Node\Expr\Variable('this'),
216 1
                    $name . '_wrapped'
217
                ),
218 1
                new Node\Expr\New_(
219 1
                    new Node\Name($details['wrap']),
220
                    [
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...
221 1
                        new Node\Expr\PropertyFetch(
222 1
                            new Node\Expr\Variable('this'),
223
                            $name
224
                        ),
225
                    ]
226
                )
227
            );
228 1
            $stmts[] = new Node\Stmt\Return_(
229 1
                new Node\Expr\PropertyFetch(
230 1
                    new Node\Expr\Variable('this'),
231 1
                    $name . '_wrapped'
232
                )
233
            );
234
        }
235
236 1
        $method = $this->factory->method($methodName)
237 1
            ->makePublic()
238 1
            ->setDocComment('/**
239 1
                              * @return ' . implode('|', $types) . '
240
                              */')
241 1
            ->addStmts($stmts);
242 1
        if (count($types) === 1) {
243 1
            $method = $method->setReturnType($types[0]);
244
        }
245 1
        return $method;
246
    }
247
}
248