Completed
Push — master ( a1c8e7...7d2da4 )
by Thomas
04:30
created

PhpParameter::fromReflection()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.3697

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 33
ccs 16
cts 24
cp 0.6667
rs 5.3846
cc 8
eloc 19
nc 20
nop 1
crap 10.3697
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
	 *
41
	 * @param string|null $name        	
42
	 */
43 15
	public static function create($name = null) {
44 15
		return new static($name);
45
	}
46
47 4
	public static function fromReflection(\ReflectionParameter $ref) {
48 4
		$parameter = new static();
49 4
		$parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference());
50
		
51 4
		if ($ref->isDefaultValueAvailable()) {
52 4
			$parameter->setDefaultValue($ref->getDefaultValue());
53 4
		}
54
		
55
		// find type and description in docblock
56 4
		$docblock = new Docblock($ref->getDeclaringFunction());
57
		
58 4
		$params = $docblock->getTags('param');
59 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...
60 4
			return $t->getVariable() == '$' . $name;
61 4
		});
62
		
63 4
		if ($tag !== null) {
64 4
			$parameter->setType($tag->getType(), $tag->getDescription());
65 4
		}
66
		
67
		// set type if not found in comment
68 4
		if ($parameter->getType() === null) {
69
			if ($ref->isArray()) {
70
				$parameter->setType('array');
71
			} elseif ($class = $ref->getClass()) {
72
				$parameter->setType($class->getName());
73
			} elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) {
74
				$parameter->setType('callable');
75
			}
76
		}
77
		
78 4
		return $parameter;
79
	}
80
81 25
	public function __construct($name = null) {
82 25
		$this->setName($name);
83 25
	}
84
85
	/**
86
	 *
87
	 * @param boolean $bool        	
88
	 */
89 5
	public function setPassedByReference($bool) {
90 5
		$this->passedByReference = (boolean) $bool;
91
		
92 5
		return $this;
93
	}
94
95 11
	public function isPassedByReference() {
96 11
		return $this->passedByReference;
97
	}
98
99
	/**
100
	 *
101
	 * @return ParamTag
102
	 */
103 7
	public function getDocblockTag() {
104 7
		return ParamTag::create()->setType($this->getType() ?  : 'mixed')->setVariable($this->getName())->setDescription($this->getTypeDescription());
105
	}
106
107
	/**
108
	 *
109
	 * @see #setDescription
110
	 * @param string $description        	
111
	 */
112 7
	public function setTypeDescription($description) {
113 7
		$this->setDescription($description);
114 7
	}
115
116
	/**
117
	 * Alias for getDescription()
118
	 *
119
	 * @return string
120
	 */
121 8
	public function getTypeDescription() {
122 8
		return $this->getDescription();
123
	}
124
}
125