Completed
Push — master ( 13eb4d...6f4a41 )
by Thomas
02:46
created

PhpParameter::isPassedByReference()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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\docblock\tags\ParamTag;
21
use gossi\codegen\model\parts\NameTrait;
22
use gossi\codegen\model\parts\DefaultValueTrait;
23
use gossi\codegen\model\parts\TypeTrait;
24
use gossi\docblock\Docblock;
25
26
/**
27
 * Represents a PHP parameter.
28
 *
29
 * @author Johannes M. Schmitt <[email protected]>
30
 */
31
class PhpParameter extends AbstractModel {
32
	
33
	use NameTrait;
34
	use DefaultValueTrait;
35
	use TypeTrait;
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 15
	public static function create($name = null) {
46 15
		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
			$parameter->setDefaultValue($ref->getDefaultValue());
61 4
		}
62
		
63
		// find type and description in docblock
64 4
		$docblock = new Docblock($ref->getDeclaringFunction());
65
		
66 4
		$params = $docblock->getTags('param');
67 4
		$tag = $params->find($ref->name, function (ParamTag $t, $name) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68 4
			return $t->getVariable() == '$' . $name;
69 4
		});
70
		
71 4
		if ($tag !== null) {
72 4
			$parameter->setType($tag->getType(), $tag->getDescription());
73 4
		}
74
		
75
		// set type if not found in comment
76 4
		if ($parameter->getType() === null) {
77
			if ($ref->isArray()) {
78
				$parameter->setType('array');
79
			} elseif ($class = $ref->getClass()) {
80
				$parameter->setType($class->getName());
81
			} elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
82
				$parameter->setType('callable');
83
			}
84
		}
85
		
86 4
		return $parameter;
87
	}
88
89
	/**
90
	 * Creates a new PHP parameter
91
	 * 
92
	 * @param string $name the parameter name
93
	 */
94 25
	public function __construct($name = null) {
95 25
		$this->setName($name);
96 25
	}
97
98
	/**
99
	 * Sets whether this parameter is passed by reference
100
	 * 
101
	 * @param boolean $bool `true` if passed by reference and `false` if not
102
	 * @return $this        	
103
	 */
104 5
	public function setPassedByReference($bool) {
105 5
		$this->passedByReference = (boolean) $bool;
106
		
107 5
		return $this;
108
	}
109
110
	/**
111
	 * Returns whether this parameter is passed by reference
112
	 * 
113
	 * @return boolean `true` if passed by reference and `false` if not
114
	 */
115 11
	public function isPassedByReference() {
116 11
		return $this->passedByReference;
117
	}
118
119
	/**
120
	 * Returns a docblock tag for this parameter
121
	 * 
122
	 * @return ParamTag
123
	 */
124 7
	public function getDocblockTag() {
125 7
		return ParamTag::create()
126 7
			->setType($this->getType() ?  : 'mixed')
127 7
			->setVariable($this->getName())
128 7
			->setDescription($this->getTypeDescription());
129
	}
130
131
	/**
132
	 * Alias for setDescription()
133
	 *
134
	 * @see #setDescription
135
	 * @param string $description
136
	 * @return $this        	
137
	 */
138 7
	public function setTypeDescription($description) {
139 7
		return $this->setDescription($description);
140
	}
141
142
	/**
143
	 * Alias for getDescription()
144
	 *
145
	 * @see #getDescription
146
	 * @return string
147
	 */
148 8
	public function getTypeDescription() {
149 8
		return $this->getDescription();
150
	}
151
}
152