Completed
Pull Request — master (#389)
by Grégoire
02:48 queued 01:23
created

ClassCodeGenerator::generateArguments()   D

Complexity

Conditions 9
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Doubler\Generator;
13
14
/**
15
 * Class code creator.
16
 * Generates PHP code for specific class node tree.
17
 *
18
 * @author Konstantin Kudryashov <[email protected]>
19
 */
20
class ClassCodeGenerator
21
{
22
    /**
23
     * @var TypeHintReference
24
     */
25
    private $typeHintReference;
26
27
    public function __construct(TypeHintReference $typeHintReference = null)
28
    {
29
        $this->typeHintReference = $typeHintReference;
30
    }
31
32
    /**
33
     * Generates PHP code for class node.
34
     *
35
     * @param string         $classname
36
     * @param Node\ClassNode $class
37
     *
38
     * @return string
39
     */
40
    public function generate($classname, Node\ClassNode $class)
41
    {
42
        $parts     = explode('\\', $classname);
43
        $classname = array_pop($parts);
44
        $namespace = implode('\\', $parts);
45
46
        $code = sprintf("class %s extends \%s implements %s {\n",
47
            $classname, $class->getParentClass(), implode(', ',
48
                array_map(function ($interface) {return '\\'.$interface;}, $class->getInterfaces())
49
            )
50
        );
51
52
        foreach ($class->getProperties() as $name => $visibility) {
53
            $code .= sprintf("%s \$%s;\n", $visibility, $name);
54
        }
55
        $code .= "\n";
56
57
        foreach ($class->getMethods() as $method) {
58
            $code .= $this->generateMethod($method)."\n";
59
        }
60
        $code .= "\n}";
61
62
        return sprintf("namespace %s {\n%s\n}", $namespace, $code);
63
    }
64
65
    private function generateMethod(Node\MethodNode $method)
66
    {
67
        $php = sprintf("%s %s function %s%s(%s)%s {\n",
68
            $method->getVisibility(),
69
            $method->isStatic() ? 'static' : '',
70
            $method->returnsReference() ? '&':'',
71
            $method->getName(),
72
            implode(', ', $this->generateArguments($method->getArguments())),
73
            $this->getReturnType($method)
74
        );
75
        $php .= $method->getCode()."\n";
76
77
        return $php.'}';
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    private function getReturnType(Node\MethodNode $method)
84
    {
85 View Code Duplication
        if (version_compare(PHP_VERSION, '7.1', '>=')) {
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...
86
            if ($method->hasReturnType()) {
87
                return $method->hasNullableReturnType()
88
                    ? sprintf(': ?%s', $method->getReturnType())
89
                    : sprintf(': %s', $method->getReturnType());
90
            }
91
        }
92
93 View Code Duplication
        if (version_compare(PHP_VERSION, '7.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...
94
            return $method->hasReturnType() && $method->getReturnType() !== 'void'
95
                ? sprintf(': %s', $method->getReturnType())
96
                : '';
97
        }
98
99
        return '';
100
    }
101
102
    private function generateArguments(array $arguments)
103
    {
104
        return array_map(function (Node\ArgumentNode $argument) {
105
            $php = '';
106
107
            if (version_compare(PHP_VERSION, '7.1', '>=')) {
108
                $php .= $argument->isNullable() ? '?' : '';
109
            }
110
111
            if ($hint = $argument->getTypeHint()) {
112
                if ($this->typeHintReference->isBuiltInParamTypeHint($hint)) {
113
                    $php .= $hint;
114
                } else {
115
                    $php .= '\\'.$hint;
116
                }
117
            }
118
119
            $php .= ' '.($argument->isPassedByReference() ? '&' : '');
120
121
            $php .= $argument->isVariadic() ? '...' : '';
122
123
            $php .= '$'.$argument->getName();
124
125
            if ($argument->isOptional() && !$argument->isVariadic()) {
126
                $php .= ' = '.var_export($argument->getDefault(), true);
127
            }
128
129
            return $php;
130
        }, $arguments);
131
    }
132
}
133