Completed
Push — master ( 2ce63d...389baa )
by Luis
11:05 queued 03:24
created

Method::public()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Code\Methods;
9
10
use PhUml\Code\CanBeAbstract;
11
use PhUml\Code\CanBeStatic;
12
use PhUml\Code\HasVisibility;
13
use PhUml\Code\TypeDeclaration;
14
use PhUml\Code\Variable;
15
use PhUml\Code\Visibility;
16
use PhUml\Code\WithAbstractModifier;
17
use PhUml\Code\WithStaticModifier;
18
use PhUml\Code\WithVisibility;
19
20
/**
21
 * It represents a class or interface method
22
 *
23
 * It doesn't distinguish neither static methods nor return types yet
24
 */
25
class Method implements HasVisibility, CanBeAbstract, CanBeStatic
26
{
27
    use WithVisibility, WithAbstractModifier, WithStaticModifier;
28
29
    /** @var string */
30
    private $name;
31
32
    /** @var Variable[] */
33
    private $parameters;
34
35
    /** @var TypeDeclaration */
36
    private $returnType;
37
38 132
    protected function __construct(
39
        string $name,
40
        Visibility $modifier,
41
        array $parameters = [],
42
        TypeDeclaration $returnType = null
43
    ) {
44 132
        $this->name = $name;
45 132
        $this->modifier = $modifier;
46 132
        $this->parameters = $parameters;
47 132
        $this->isAbstract = false;
48 132
        $this->isStatic = false;
49 132
        $this->returnType = $returnType;
50 132
    }
51
52
    /** @param Variable[] $parameters */
53 123
    public static function public(
54
        string $name,
55
        array $parameters = [],
56
        TypeDeclaration $returnType = null
57
    ): Method
58
    {
59 123
        return new static($name, Visibility::public(), $parameters, $returnType ?? TypeDeclaration::absent());
60
    }
61
62
    /** @param Variable[] $parameters */
63 15
    public static function protected(
64
        string $name,
65
        array $parameters = [],
66
        TypeDeclaration $returnType = null
67
    ): Method
68
    {
69 15
        return new static($name, Visibility::protected(), $parameters, $returnType ?? TypeDeclaration::absent());
70
    }
71
72
    /** @param Variable[] $parameters */
73 45
    public static function private(
74
        string $name,
75
        array $parameters = [],
76
        TypeDeclaration $returnType = null
77
    ): Method
78
    {
79 45
        return new static($name, Visibility::private(), $parameters, $returnType ?? TypeDeclaration::absent());
80
    }
81
82 36
    public function isConstructor(): bool
83
    {
84 36
        return $this->name === '__construct';
85
    }
86
87 30
    public function parameters(): array
88
    {
89 30
        return $this->parameters;
90
    }
91
92 36
    public function returnType(): TypeDeclaration
93
    {
94 36
        return $this->returnType;
95
    }
96
97 48
    public function __toString()
98
    {
99 48
        return sprintf(
100 48
            '%s%s%s%s',
101 48
            $this->modifier,
102 48
            $this->name,
103 48
            empty($this->parameters) ? '()' : '( ' . implode($this->parameters, ', ') . ' )',
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with ', '. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            empty($this->parameters) ? '()' : '( ' . /** @scrutinizer ignore-call */ implode($this->parameters, ', ') . ' )',

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. Please note the @ignore annotation hint above.

Loading history...
104 48
            $this->returnType->isPresent() ? ": {$this->returnType()}" : ''
105
        );
106
    }
107
}
108