Completed
Push — master ( 3afdd0...9086d9 )
by Thomas
03:41
created

PhpParameter::fromReflection()   D

Complexity

Conditions 9
Paths 30

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11.9991

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 38
ccs 16
cts 24
cp 0.6667
rs 4.909
cc 9
eloc 23
nc 30
nop 1
crap 11.9991
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\NamePart;
20
use gossi\codegen\model\parts\TypePart;
21
use gossi\codegen\model\parts\ValuePart;
22
use gossi\docblock\Docblock;
23
use gossi\docblock\tags\ParamTag;
24
25
/**
26
 * Represents a PHP parameter.
27
 *
28
 * @author Johannes M. Schmitt <[email protected]>
29
 * @author Thomas Gossmann
30
 */
31
class PhpParameter extends AbstractModel {
32
33
	use NamePart;
34
	use TypePart;
35
	use ValuePart;
36
37
	private $passedByReference = false;
38
39
	/**
40
	 * Creates a new PHP parameter.
41
	 *
42
	 * @param string $name the parameter name
43
	 * @return static
44
	 */
45 16
	public static function create($name = null) {
46 16
		return new static($name);
47
	}
48
49
	/**
50
	 * Creates a PHP parameter from reflection
51
	 *
52
	 * @param \ReflectionParameter $ref
53
	 * @return PhpParameter
54
	 */
55 4
	public static function fromReflection(\ReflectionParameter $ref) {
56 4
		$parameter = new static();
57 4
		$parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
58
59 4
		if ($ref->isDefaultValueAvailable()) {
60 4
			$default = $ref->getDefaultValue();
61 4
			if (is_string($default)) {
62
				$parameter->setValue($default);
63
			} else {
64 4
				$parameter->setExpression($default);
65
			}
66 4
		}
67 4
68 4
		// find type and description in docblock
69 4
		$docblock = new Docblock($ref->getDeclaringFunction());
70
71 4
		$params = $docblock->getTags('param');
72 4
		$tag = $params->find($ref->name, function (ParamTag $t, $name) {
73 4
			return $t->getVariable() == '$' . $name;
74
		});
75
76 4
		if ($tag !== null) {
77
			$parameter->setType($tag->getType(), $tag->getDescription());
78
		}
79
80
		// set type if not found in comment
81
		if ($parameter->getType() === null) {
82
			if ($ref->isArray()) {
83
				$parameter->setType('array');
84
			} elseif ($class = $ref->getClass()) {
85
				$parameter->setType($class->getName());
86 4
			} elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
87
				$parameter->setType('callable');
88
			}
89
		}
90
91
		return $parameter;
92
	}
93
94 26
	/**
95 26
	 * Creates a new PHP parameter
96 26
	 *
97
	 * @param string $name the parameter name
98
	 */
99
	public function __construct($name = null) {
100
		$this->setName($name);
101
	}
102
103
	/**
104 5
	 * Sets whether this parameter is passed by reference
105 5
	 *
106
	 * @param bool $bool `true` if passed by reference and `false` if not
107 5
	 * @return $this
108
	 */
109
	public function setPassedByReference($bool) {
110
		$this->passedByReference = (boolean) $bool;
111
112
		return $this;
113
	}
114
115 12
	/**
116 12
	 * Returns whether this parameter is passed by reference
117
	 *
118
	 * @return bool `true` if passed by reference and `false` if not
119
	 */
120
	public function isPassedByReference() {
121
		return $this->passedByReference;
122
	}
123
124 7
	/**
125 7
	 * Returns a docblock tag for this parameter
126 7
	 *
127 7
	 * @return ParamTag
128 7
	 */
129
	public function getDocblockTag() {
130
		return ParamTag::create()
131
			->setType($this->getType() ?: 'mixed')
132
			->setVariable($this->getName())
133
			->setDescription($this->getTypeDescription());
134
	}
135
136
	/**
137
	 * Alias for setDescription()
138 7
	 *
139 7
	 * @see #setDescription
140
	 * @param string $description
141
	 * @return $this
142
	 */
143
	public function setTypeDescription($description) {
144
		return $this->setDescription($description);
145
	}
146
147
	/**
148 8
	 * Alias for getDescription()
149 8
	 *
150
	 * @see #getDescription
151
	 * @return string
152
	 */
153
	public function getTypeDescription() {
154
		return $this->getDescription();
155
	}
156
}
157