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