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 — feature-annotation-empty-resou... ( c9f254 )
by Cees-Jan
07:04
created

EmptyBaseClassGenerator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 12.58 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 20
loc 159
ccs 63
cts 70
cp 0.9
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getFilename() 4 20 2
B generate() 4 29 4
B processProperty() 0 25 5
A createProperty() 12 12 2
A createMethod() 0 22 1

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\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
        if (isset($this->yaml['uses']) && is_array($this->yaml['uses'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
46
            //$this->uses += $this->yaml['uses'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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