Completed
Push — master ( 6f10f1...5be53c )
by Thomas
05:40
created

PhpFunction::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\codegen\utils\ReflectionUtils;
29
use gossi\docblock\Docblock;
30
use gossi\docblock\tags\ReturnTag;
31
32
/**
33
 * Represents a PHP function.
34
 *
35
 * @author Johannes M. Schmitt <[email protected]>
36
 * @author Thomas Gossmann
37
 */
38
class PhpFunction extends AbstractModel implements GenerateableInterface, NamespaceInterface, DocblockInterface, RoutineInterface {
39
40
	use BodyPart;
41
	use DocblockPart;
42
	use LongDescriptionPart;
43
	use ParametersPart;
44
	use QualifiedNamePart;
45
	use ReferenceReturnPart;
46
	use TypeDocblockGeneratorPart;
47
	use TypePart;
48
49
	/**
50
	 * Creates a PHP function from reflection
51
	 *
52
	 * @deprecated will be removed in version 0.5
53
	 * @param \ReflectionFunction $ref
54
	 * @return PhpFunction
55
	 */
56 1
	public static function fromReflection(\ReflectionFunction $ref) {
57 1
		$function = self::create($ref->name)
58 1
			->setReferenceReturned($ref->returnsReference())
59 1
			->setBody(ReflectionUtils::getFunctionBody($ref));
60
61 1
		$docblock = new Docblock($ref);
62 1
		$function->setDocblock($docblock);
63 1
		$function->setDescription($docblock->getShortDescription());
64 1
		$function->setLongDescription($docblock->getLongDescription());
65
66 1
		foreach ($ref->getParameters() as $refParam) {
67 1
			assert($refParam instanceof \ReflectionParameter); // hmm - assert here?
68
69 1
			$param = PhpParameter::fromReflection($refParam);
70 1
			$function->addParameter($param);
71
		}
72
73 1
		return $function;
74
	}
75
76
	/**
77
	 * Creates a new PHP function
78
	 *
79
	 * @param string $name qualified name
80
	 * @return static
81
	 */
82 7
	public static function create($name = null) {
83 7
		return new static($name);
84
	}
85
86
	/**
87
	 * Creates a new PHP function
88
	 *
89
	 * @param string $name qualified name
90
	 */
91 10
	public function __construct($name = null) {
92 10
		$this->setQualifiedName($name);
93 10
		$this->docblock = new Docblock();
94 10
		$this->initParameters();
95 10
	}
96
97
	/**
98
	 * @inheritDoc
99
	 */
100 3
	public function generateDocblock() {
101 3
		$docblock = $this->getDocblock();
102 3
		$docblock->setShortDescription($this->getDescription());
103 3
		$docblock->setLongDescription($this->getLongDescription());
104
105
		// return tag
106 3
		$this->generateTypeTag(new ReturnTag());
107
108
		// param tags
109 3
		$this->generateParamDocblock();
110 3
	}
111
}
112