Completed
Push — master ( 2a5486...81459b )
by Vitaly
02:39
created

GenericGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A defFunction() 0 4 1
A defStaticFunction() 0 4 1
A defClass() 0 4 1
A code() 0 12 3
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:37
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Class GenericGenerator
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class GenericGenerator extends AbstractGenerator
14
{
15
    /** @var ClassGenerator[] Collection of classes */
16
    protected $classes = [];
17
18
    /** @var FunctionGenerator[] Collection of functions */
19
    protected $functions = [];
20
21
    /**
22
     * Set function.
23
     *
24
     * @param string $name Function
25
     * @param bool   $isStatic Flag that function is static
26
     *
27
     * @return FunctionGenerator New function generator instance
28
     */
29
    public function defFunction(string $name, bool $isStatic = false) : FunctionGenerator
30
    {
31
        return $this->functions[] = new FunctionGenerator($this, $name, $isStatic);
0 ignored issues
show
Documentation introduced by
$this is of type this<samsonphp\generator\GenericGenerator>, but the function expects a string.

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...
Documentation introduced by
$name is of type string, but the function expects a null|object<samsonphp\generator\GenericGenerator>.

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...
Unused Code introduced by
The call to FunctionGenerator::__construct() has too many arguments starting with $isStatic.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
32
    }
33
34
    /**
35
     * Set static function.
36
     *
37
     * @param string $name Function
38
     *
39
     * @return FunctionGenerator New function generator instance
40
     */
41
    public function defStaticFunction(string $name) : FunctionGenerator
42
    {
43
        return $this->defFunction($name, false);
44
    }
45
46
    /**
47
     * Set class.
48
     *
49
     * @param string $name Class name
50
     *
51
     * @return ClassGenerator
52
     */
53
    public function defClass(string $name) : ClassGenerator
54
    {
55
        return $this->classes[] = new ClassGenerator($this, $name);
56
    }
57
58
    /**
59
     * Generate code.
60
     *
61
     * @param int $indentation Code level
62
     *
63
     * @return string Generated code
64
     */
65
    public function code($indentation = 0)
66
    {
67
        foreach ($this->classes as $classGenerator) {
68
            $this->code[] = $classGenerator->code($indentation);
69
        }
70
71
        foreach ($this->functions as $functionGenerator) {
72
            $this->code[] = $functionGenerator->code($indentation);
73
        }
74
75
        return implode("\n".$this->indentation($indentation), $this->code);
76
    }
77
}
78