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, ', ') . ' )', |
|
|
|
|
104
|
48 |
|
$this->returnType->isPresent() ? ": {$this->returnType()}" : '' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.