Completed
Push — master ( c0dc80...73a4ac )
by Thomas
03:03
created

PhpFunction::generateDocblock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
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\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);
1 ignored issue
show
Deprecated Code introduced by
The method gossi\codegen\model\PhpParameter::fromReflection() has been deprecated with message: will be removed in version 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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();
1 ignored issue
show
Unused Code introduced by
The call to the method gossi\codegen\model\PhpFunction::initParameters() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
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