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.

InterfaceGenerator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 7.27 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 8
loc 110
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFilename() 8 8 1
C generate() 0 71 10

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\ResourceInterface;
6
use function ApiClients\Tools\ResourceGenerator\exists;
7
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
8
use Doctrine\Common\Inflector\Inflector;
9
use PhpParser\BuilderFactory;
10
use PhpParser\Node;
11
12
final class InterfaceGenerator implements FileGeneratorInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $yaml;
18
19
    /**
20
     * @var array
21
     */
22
    protected $uses = [
23
        ResourceInterface::class => true,
24
    ];
25
26
    /**
27
     * InterfaceGenerator constructor.
28
     * @param array $yaml
29
     */
30 1
    public function __construct(array $yaml)
31
    {
32 1
        $this->yaml = $yaml;
33 1
    }
34
35
    /**
36
     * @return string
37
     */
38 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...
39
    {
40 1
        return $this->yaml['src']['path'] .
41 1
            DIRECTORY_SEPARATOR .
42 1
            str_replace('\\', DIRECTORY_SEPARATOR, $this->yaml['class']) .
43 1
            'Interface.php'
44
        ;
45
    }
46
47
    /**
48
     * @return Node
49
     */
50 1
    public function generate(): Node
51
    {
52 1
        $classChunks = explode('\\', $this->yaml['class']);
53 1
        $baseClass = array_pop($classChunks);
54 1
        $className = $baseClass . 'Interface';
55 1
        $namespace = $this->yaml['src']['namespace'];
56 1
        if (count($classChunks) > 0) {
57 1
            $namespace .= '\\' . implode('\\', $classChunks);
58 1
            $namespace = str_replace('\\\\', '\\', $namespace);
59
        }
60
61 1
        $factory = new BuilderFactory();
62
63 1
        $class = $factory->interface($className)
64 1
            ->extend('ResourceInterface');
65
66 1
        $class->addStmt(
67 1
            new Node\Stmt\ClassConst(
68
                [
69 1
                    new Node\Const_(
70 1
                        'HYDRATE_CLASS',
71 1
                        new Node\Scalar\String_(
72 1
                            $this->yaml['class']
73
                        )
74
                    ),
75
                ]
76
            )
77
        );
78
79 1
        foreach ($this->yaml['properties'] as $name => $details) {
80 1
            if (is_array($details)) {
81 1
                $types = $details['type'];
82
            } else {
83 1
                $types = $details;
84
            }
85
86 1
            $types = explode('|', $types);
87 1
            foreach ($types as $type) {
88 1
                if (exists($type)) {
89 1
                    $this->uses[$type] = true;
90
                }
91
            }
92
93 1
            $methodName = Inflector::camelize($name);
94 1
            if (is_array($details) && isset($details['method'])) {
95 1
                $methodName = $details['method'];
96
            }
97 1
            $method = $factory->method($methodName)
98 1
                ->makePublic()
99 1
                ->setDocComment(
100 1
                    "/**\r\n * @return " . implode('|', $types) . "\r\n */"
101
                );
102 1
            if (count($types) === 1) {
103 1
                $method = $method->setReturnType($types[0]);
104
            }
105 1
            $class->addStmt($method);
106
        }
107
108 1
        $stmt = $factory->namespace($namespace);
109
110 1
        ksort($this->uses);
111 1
        foreach ($this->uses as $useClass => $bool) {
112
            $stmt = $stmt
113 1
                ->addStmt($factory->use($useClass))
114
            ;
115
        }
116
117 1
        return $stmt->addStmt($class)
118 1
            ->getNode()
119
        ;
120
    }
121
}
122