4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Doubler\Generator\Node;
14 use Prophecy\Doubler\Generator\TypeHintReference;
15 use Prophecy\Exception\InvalidArgumentException;
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 private $visibility = 'public';
27 private $static = false;
28 private $returnsReference = false;
30 private $nullableReturnType = false;
35 private $arguments = array();
38 * @var TypeHintReference
40 private $typeHintReference;
46 public function __construct($name, $code = null, TypeHintReference $typeHintReference = null)
50 $this->typeHintReference = $typeHintReference ?: new TypeHintReference();
53 public function getVisibility()
55 return $this->visibility;
59 * @param string $visibility
61 public function setVisibility($visibility)
63 $visibility = strtolower($visibility);
65 if (!in_array($visibility, array('public', 'private', 'protected'))) {
66 throw new InvalidArgumentException(sprintf(
67 '`%s` method visibility is not supported.', $visibility
71 $this->visibility = $visibility;
74 public function isStatic()
79 public function setStatic($static = true)
81 $this->static = (bool) $static;
84 public function returnsReference()
86 return $this->returnsReference;
89 public function setReturnsReference()
91 $this->returnsReference = true;
94 public function getName()
99 public function addArgument(ArgumentNode $argument)
101 $this->arguments[] = $argument;
105 * @return ArgumentNode[]
107 public function getArguments()
109 return $this->arguments;
112 public function hasReturnType()
114 return null !== $this->returnType;
118 * @param string $type
120 public function setReturnType($type = null)
122 if ($type === '' || $type === null) {
123 $this->returnType = null;
132 if (isset($typeMap[$type])) {
133 $type = $typeMap[$type];
135 $this->returnType = $this->typeHintReference->isBuiltInReturnTypeHint($type) ?
137 '\\' . ltrim($type, '\\');
140 public function getReturnType()
142 return $this->returnType;
148 public function setNullableReturnType($bool = true)
150 $this->nullableReturnType = (bool) $bool;
156 public function hasNullableReturnType()
158 return $this->nullableReturnType;
162 * @param string $code
164 public function setCode($code)
169 public function getCode()
171 if ($this->returnsReference)
173 return "throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');";
176 return (string) $this->code;
179 public function useParentCode()
181 $this->code = sprintf(
182 'return parent::%s(%s);', $this->getName(), implode(', ',
183 array_map(array($this, 'generateArgument'), $this->arguments)
188 private function generateArgument(ArgumentNode $arg)
190 $argument = '$'.$arg->getName();
192 if ($arg->isVariadic()) {
193 $argument = '...'.$argument;