Completed
Pull Request — master (#33)
by
unknown
07:50
created

PhpProperty::fromReflection()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.027

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 28
ccs 20
cts 22
cp 0.9091
rs 8.439
cc 6
eloc 20
nc 6
nop 1
crap 6.027
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\TypeDocblockGeneratorPart;
20
use gossi\codegen\model\parts\ValuePart;
21
use gossi\docblock\Docblock;
22
use gossi\docblock\tags\VarTag;
23
24
/**
25
 * Represents a PHP property.
26
 *
27
 * @author Johannes M. Schmitt <[email protected]>
28
 * @author Thomas Gossmann
29
 */
30
class PhpProperty extends AbstractPhpMember {
31
32
	use TypeDocblockGeneratorPart;
33
	use ValuePart;
34
35
	/**
36
	 * Creates a new PHP property
37
	 *
38
	 * @param string $name the properties name
39
	 * @return static
40
	 */
41 11
	public static function create($name) {
42 11
		return new static($name);
43
	}
44
45
	/**
46
	 * Creates a new PHP property from reflection
47
	 *
48
	 * @param \ReflectionProperty $ref
49
	 * @return static
50
	 */
51 4
	public static function fromReflection(\ReflectionProperty $ref) {
52 4
		$property = new static($ref->name);
53 4
		$property->setStatic($ref->isStatic())
54 4
			->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE));
55
56 4
		$docblock = new Docblock($ref);
57 4
		$property->setDocblock($docblock);
58 4
		$property->setDescription($docblock->getShortDescription());
59 4
		$property->setLongDescription($docblock->getLongDescription());
60
61 4
		$vars = $docblock->getTags('var');
62 4
		if ($vars->size() > 0) {
63 2
			$var = $vars->get(0);
64 2
			$property->setType($var->getType(), $var->getDescription());
65 2
		}
66
67 4
		$defaultProperties = $ref->getDeclaringClass()->getDefaultProperties();
68 4
		if (isset($defaultProperties[$ref->name])) {
69 2
			$default = $defaultProperties[$ref->name];
70 2
			if (is_string($default)) {
71
				$property->setValue($default);
72
			} else {
73 2
				$property->setExpression($default);
74
			}
75 2
		}
76
77 4
		return $property;
78
	}
79
80
	/**
81
	 * Generates docblock based on provided information
82
	 */
83 8
	public function generateDocblock() {
84 8
		$docblock = $this->getDocblock();
85 8
		$docblock->setShortDescription($this->getDescription());
86 8
		$docblock->setLongDescription($this->getLongDescription());
87
88
		// var tag
89 8
		$this->generateTypeTag(new VarTag());
90 8
	}
91
}
92