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::generate()   C
last analyzed

Complexity

Conditions 10
Paths 100

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
nc 100
nop 0
dl 0
loc 71
ccs 41
cts 41
cp 1
crap 10
rs 6.766
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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