1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
namespace gossi\codegen\model; |
18
|
|
|
|
19
|
|
|
use gossi\codegen\model\parts\AbstractPart; |
20
|
|
|
use gossi\codegen\model\parts\BodyPart; |
21
|
|
|
use gossi\codegen\model\parts\FinalPart; |
22
|
|
|
use gossi\codegen\model\parts\ParametersPart; |
23
|
|
|
use gossi\codegen\model\parts\ReferenceReturnPart; |
24
|
|
|
use gossi\codegen\model\parts\TypeDocblockGeneratorPart; |
25
|
|
|
use gossi\codegen\utils\ReflectionUtils; |
26
|
|
|
use gossi\docblock\Docblock; |
27
|
|
|
use gossi\docblock\tags\ReturnTag; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Represents a PHP method. |
31
|
|
|
* |
32
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
33
|
|
|
* @author Thomas Gossmann |
34
|
|
|
*/ |
35
|
|
|
class PhpMethod extends AbstractPhpMember { |
36
|
|
|
|
37
|
|
|
use AbstractPart; |
38
|
|
|
use BodyPart; |
39
|
|
|
use FinalPart; |
40
|
|
|
use ParametersPart; |
41
|
|
|
use ReferenceReturnPart; |
42
|
|
|
use TypeDocblockGeneratorPart; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Creates a new PHP method. |
46
|
|
|
* |
47
|
|
|
* @param string $name the method name |
48
|
|
|
*/ |
49
|
16 |
|
public static function create($name) { |
50
|
16 |
|
return new static($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates a PHP method from reflection |
55
|
|
|
* |
56
|
|
|
* @param \ReflectionMethod $ref |
57
|
|
|
* @return PhpMethod |
58
|
|
|
*/ |
59
|
2 |
|
public static function fromReflection(\ReflectionMethod $ref) { |
60
|
2 |
|
$method = new static($ref->name); |
61
|
2 |
|
$method->setFinal($ref->isFinal()) |
62
|
2 |
|
->setAbstract($ref->isAbstract()) |
63
|
2 |
|
->setStatic($ref->isStatic()) |
64
|
2 |
|
->setVisibility($ref->isPublic() |
65
|
2 |
|
? self::VISIBILITY_PUBLIC |
66
|
|
|
: ($ref->isProtected() |
67
|
|
|
? self::VISIBILITY_PROTECTED |
68
|
2 |
|
: self::VISIBILITY_PRIVATE)) |
69
|
2 |
|
->setReferenceReturned($ref->returnsReference()) |
70
|
2 |
|
->setBody(ReflectionUtils::getFunctionBody($ref)); |
71
|
|
|
|
72
|
2 |
|
$docblock = new Docblock($ref); |
73
|
2 |
|
$method->setDocblock($docblock); |
74
|
2 |
|
$method->setDescription($docblock->getShortDescription()); |
75
|
2 |
|
$method->setLongDescription($docblock->getLongDescription()); |
76
|
|
|
|
77
|
|
|
// return type and description |
78
|
2 |
|
$returns = $method->getDocblock()->getTags('return'); |
79
|
2 |
|
if ($returns->size() > 0) { |
80
|
|
|
$return = $returns->get(0); |
81
|
|
|
$method->setType($return->getType(), $return->getDescription()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// params |
85
|
2 |
|
foreach ($ref->getParameters() as $param) { |
86
|
|
|
$method->addParameter(static::createParameter($param)); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return $method; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Creates a parameter from reflection |
94
|
|
|
* |
95
|
|
|
* @return PhpParameter |
96
|
|
|
*/ |
97
|
|
|
protected static function createParameter(\ReflectionParameter $parameter) { |
98
|
|
|
return PhpParameter::fromReflection($parameter); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Generates docblock based on provided information |
103
|
|
|
*/ |
104
|
11 |
|
public function generateDocblock() { |
105
|
11 |
|
$docblock = $this->getDocblock(); |
106
|
11 |
|
$docblock->setShortDescription($this->getDescription()); |
107
|
11 |
|
$docblock->setLongDescription($this->getLongDescription()); |
108
|
|
|
|
109
|
|
|
// return tag |
110
|
11 |
|
$this->generateTypeTag(new ReturnTag()); |
111
|
|
|
|
112
|
|
|
// param tags |
113
|
11 |
|
$this->generateParamDocblock(); |
114
|
11 |
|
} |
115
|
|
|
} |
116
|
|
|
|