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\Exception\Doubler\MethodNotExtendableException;
15 use Prophecy\Exception\InvalidArgumentException;
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 private $parentClass = 'stdClass';
25 private $interfaces = array();
26 private $properties = array();
27 private $unextendableMethods = array();
32 private $methods = array();
34 public function getParentClass()
36 return $this->parentClass;
40 * @param string $class
42 public function setParentClass($class)
44 $this->parentClass = $class ?: 'stdClass';
50 public function getInterfaces()
52 return $this->interfaces;
56 * @param string $interface
58 public function addInterface($interface)
60 if ($this->hasInterface($interface)) {
64 array_unshift($this->interfaces, $interface);
68 * @param string $interface
72 public function hasInterface($interface)
74 return in_array($interface, $this->interfaces);
77 public function getProperties()
79 return $this->properties;
82 public function addProperty($name, $visibility = 'public')
84 $visibility = strtolower($visibility);
86 if (!in_array($visibility, array('public', 'private', 'protected'))) {
87 throw new InvalidArgumentException(sprintf(
88 '`%s` property visibility is not supported.', $visibility
92 $this->properties[$name] = $visibility;
96 * @return MethodNode[]
98 public function getMethods()
100 return $this->methods;
103 public function addMethod(MethodNode $method, $force = false)
105 if (!$this->isExtendable($method->getName())){
107 'Method `%s` is not extendable, so can not be added.', $method->getName()
109 throw new MethodNotExtendableException($message, $this->getParentClass(), $method->getName());
112 if ($force || !isset($this->methods[$method->getName()])) {
113 $this->methods[$method->getName()] = $method;
117 public function removeMethod($name)
119 unset($this->methods[$name]);
123 * @param string $name
125 * @return MethodNode|null
127 public function getMethod($name)
129 return $this->hasMethod($name) ? $this->methods[$name] : null;
133 * @param string $name
137 public function hasMethod($name)
139 return isset($this->methods[$name]);
145 public function getUnextendableMethods()
147 return $this->unextendableMethods;
151 * @param string $unextendableMethod
153 public function addUnextendableMethod($unextendableMethod)
155 if (!$this->isExtendable($unextendableMethod)){
158 $this->unextendableMethods[] = $unextendableMethod;
162 * @param string $method
165 public function isExtendable($method)
167 return !in_array($method, $this->unextendableMethods);