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\BodyPart; |
21
|
|
|
use gossi\codegen\model\parts\DocblockPart; |
22
|
|
|
use gossi\codegen\model\parts\LongDescriptionPart; |
23
|
|
|
use gossi\codegen\model\parts\ParametersPart; |
24
|
|
|
use gossi\codegen\model\parts\QualifiedNamePart; |
25
|
|
|
use gossi\codegen\model\parts\ReferenceReturnPart; |
26
|
|
|
use gossi\codegen\model\parts\TypeDocblockGeneratorPart; |
27
|
|
|
use gossi\codegen\model\parts\TypePart; |
28
|
|
|
use gossi\docblock\Docblock; |
29
|
|
|
use gossi\docblock\tags\ReturnTag; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Represents a PHP function. |
33
|
|
|
* |
34
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
35
|
|
|
* @author Thomas Gossmann |
36
|
|
|
*/ |
37
|
|
|
class PhpFunction extends AbstractModel implements GenerateableInterface, NamespaceInterface, DocblockInterface, RoutineInterface { |
38
|
|
|
|
39
|
|
|
use BodyPart; |
40
|
|
|
use DocblockPart; |
41
|
|
|
use LongDescriptionPart; |
42
|
|
|
use ParametersPart; |
43
|
|
|
use QualifiedNamePart; |
44
|
|
|
use ReferenceReturnPart; |
45
|
|
|
use TypeDocblockGeneratorPart; |
46
|
|
|
use TypePart; |
47
|
|
|
|
48
|
|
|
// /** |
49
|
|
|
// * Creates a PHP function from reflection |
50
|
|
|
// * |
51
|
|
|
// * @deprecated will be removed in version 0.5 |
52
|
|
|
// * @param \ReflectionFunction $ref |
53
|
|
|
// * @return PhpFunction |
54
|
|
|
// */ |
55
|
|
|
// public static function fromReflection(\ReflectionFunction $ref) { |
56
|
|
|
// $function = self::create($ref->name) |
57
|
|
|
// ->setReferenceReturned($ref->returnsReference()) |
58
|
|
|
// ->setBody(ReflectionUtils::getFunctionBody($ref)); |
59
|
|
|
|
60
|
|
|
// $docblock = new Docblock($ref); |
|
|
|
|
61
|
|
|
// $function->setDocblock($docblock); |
62
|
|
|
// $function->setDescription($docblock->getShortDescription()); |
63
|
|
|
// $function->setLongDescription($docblock->getLongDescription()); |
64
|
|
|
|
65
|
|
|
// foreach ($ref->getParameters() as $refParam) { |
|
|
|
|
66
|
|
|
// assert($refParam instanceof \ReflectionParameter); // hmm - assert here? |
67
|
|
|
|
68
|
|
|
// $param = PhpParameter::fromReflection($refParam); |
|
|
|
|
69
|
|
|
// $function->addParameter($param); |
70
|
|
|
// } |
71
|
|
|
|
72
|
|
|
// return $function; |
|
|
|
|
73
|
|
|
// } |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a new PHP function |
77
|
|
|
* |
78
|
|
|
* @param string $name qualified name |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
6 |
|
public static function create($name = null) { |
82
|
6 |
|
return new static($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a new PHP function |
87
|
|
|
* |
88
|
|
|
* @param string $name qualified name |
89
|
|
|
*/ |
90
|
9 |
|
public function __construct($name = null) { |
91
|
9 |
|
$this->setQualifiedName($name); |
92
|
9 |
|
$this->docblock = new Docblock(); |
93
|
9 |
|
$this->initParameters(); |
94
|
9 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
3 |
|
public function generateDocblock() { |
100
|
3 |
|
$docblock = $this->getDocblock(); |
101
|
3 |
|
$docblock->setShortDescription($this->getDescription()); |
102
|
3 |
|
$docblock->setLongDescription($this->getLongDescription()); |
103
|
|
|
|
104
|
|
|
// return tag |
105
|
3 |
|
$this->generateTypeTag(new ReturnTag()); |
106
|
|
|
|
107
|
|
|
// param tags |
108
|
3 |
|
$this->generateParamDocblock(); |
109
|
3 |
|
} |
110
|
|
|
} |
111
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.