PageRenderTime 111ms CodeModel.GetById 23ms RepoModel.GetById 3ms app.codeStats 0ms

/vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/Node/ClassNode.php

https://gitlab.com/madwanz64/laravel
PHP | 169 lines | 92 code | 24 blank | 53 comment | 6 complexity | 338a9e2c339bbe7394a4831c15df9bd1 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Prophecy.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. * Marcello Duarte <marcello.duarte@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Prophecy\Doubler\Generator\Node;
  11. use Prophecy\Exception\Doubler\MethodNotExtendableException;
  12. use Prophecy\Exception\InvalidArgumentException;
  13. /**
  14. * Class node.
  15. *
  16. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  17. */
  18. class ClassNode
  19. {
  20. private $parentClass = 'stdClass';
  21. private $interfaces = array();
  22. private $properties = array();
  23. private $unextendableMethods = array();
  24. /**
  25. * @var MethodNode[]
  26. */
  27. private $methods = array();
  28. public function getParentClass()
  29. {
  30. return $this->parentClass;
  31. }
  32. /**
  33. * @param string $class
  34. */
  35. public function setParentClass($class)
  36. {
  37. $this->parentClass = $class ?: 'stdClass';
  38. }
  39. /**
  40. * @return string[]
  41. */
  42. public function getInterfaces()
  43. {
  44. return $this->interfaces;
  45. }
  46. /**
  47. * @param string $interface
  48. */
  49. public function addInterface($interface)
  50. {
  51. if ($this->hasInterface($interface)) {
  52. return;
  53. }
  54. array_unshift($this->interfaces, $interface);
  55. }
  56. /**
  57. * @param string $interface
  58. *
  59. * @return bool
  60. */
  61. public function hasInterface($interface)
  62. {
  63. return in_array($interface, $this->interfaces);
  64. }
  65. public function getProperties()
  66. {
  67. return $this->properties;
  68. }
  69. public function addProperty($name, $visibility = 'public')
  70. {
  71. $visibility = strtolower($visibility);
  72. if (!in_array($visibility, array('public', 'private', 'protected'))) {
  73. throw new InvalidArgumentException(sprintf(
  74. '`%s` property visibility is not supported.', $visibility
  75. ));
  76. }
  77. $this->properties[$name] = $visibility;
  78. }
  79. /**
  80. * @return MethodNode[]
  81. */
  82. public function getMethods()
  83. {
  84. return $this->methods;
  85. }
  86. public function addMethod(MethodNode $method, $force = false)
  87. {
  88. if (!$this->isExtendable($method->getName())){
  89. $message = sprintf(
  90. 'Method `%s` is not extendable, so can not be added.', $method->getName()
  91. );
  92. throw new MethodNotExtendableException($message, $this->getParentClass(), $method->getName());
  93. }
  94. if ($force || !isset($this->methods[$method->getName()])) {
  95. $this->methods[$method->getName()] = $method;
  96. }
  97. }
  98. public function removeMethod($name)
  99. {
  100. unset($this->methods[$name]);
  101. }
  102. /**
  103. * @param string $name
  104. *
  105. * @return MethodNode|null
  106. */
  107. public function getMethod($name)
  108. {
  109. return $this->hasMethod($name) ? $this->methods[$name] : null;
  110. }
  111. /**
  112. * @param string $name
  113. *
  114. * @return bool
  115. */
  116. public function hasMethod($name)
  117. {
  118. return isset($this->methods[$name]);
  119. }
  120. /**
  121. * @return string[]
  122. */
  123. public function getUnextendableMethods()
  124. {
  125. return $this->unextendableMethods;
  126. }
  127. /**
  128. * @param string $unextendableMethod
  129. */
  130. public function addUnextendableMethod($unextendableMethod)
  131. {
  132. if (!$this->isExtendable($unextendableMethod)){
  133. return;
  134. }
  135. $this->unextendableMethods[] = $unextendableMethod;
  136. }
  137. /**
  138. * @param string $method
  139. * @return bool
  140. */
  141. public function isExtendable($method)
  142. {
  143. return !in_array($method, $this->unextendableMethods);
  144. }
  145. }