PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/hamcrest-php/generator/FactoryMethod.php

http://github.com/hafriedlander/phockito
PHP | 206 lines | 150 code | 30 blank | 26 comment | 21 complexity | ed80fe331446568719c8063b1a281081 MD5 | raw file
  1. <?php
  2. /*
  3. Copyright (c) 2009 hamcrest.org
  4. */
  5. /**
  6. * Represents a single static factory method from a {@link Matcher} class.
  7. *
  8. * @todo Search method in file contents for func_get_args() to replace factoryVarArgs.
  9. */
  10. class FactoryMethod
  11. {
  12. /**
  13. * @var FactoryClass
  14. */
  15. private $class;
  16. /**
  17. * @var ReflectionMethod
  18. */
  19. private $reflector;
  20. /**
  21. * @var array of string
  22. */
  23. private $comment;
  24. /**
  25. * @var bool
  26. */
  27. private $isVarArgs;
  28. /**
  29. * @var array of FactoryCall
  30. */
  31. private $calls;
  32. /**
  33. * @var array FactoryParameter
  34. */
  35. private $parameters;
  36. public function __construct(FactoryClass $class, ReflectionMethod $reflector) {
  37. $this->class = $class;
  38. $this->reflector = $reflector;
  39. $this->extractCommentWithoutLeadingShashesAndStars();
  40. $this->extractFactoryNamesFromComment();
  41. $this->extractParameters();
  42. }
  43. public function extractCommentWithoutLeadingShashesAndStars() {
  44. $this->comment = explode("\n", $this->reflector->getDocComment());
  45. foreach ($this->comment as &$line) {
  46. $line = preg_replace('#^\s*(/\\*+|\\*+/|\\*)\s?#', '', $line);
  47. }
  48. $this->trimLeadingBlankLinesFromComment();
  49. $this->trimTrailingBlankLinesFromComment();
  50. }
  51. public function trimLeadingBlankLinesFromComment() {
  52. while (count($this->comment) > 0) {
  53. $line = array_shift($this->comment);
  54. if (trim($line) != '') {
  55. array_unshift($this->comment, $line);
  56. break;
  57. }
  58. }
  59. }
  60. public function trimTrailingBlankLinesFromComment() {
  61. while (count($this->comment) > 0) {
  62. $line = array_pop($this->comment);
  63. if (trim($line) != '') {
  64. array_push($this->comment, $line);
  65. break;
  66. }
  67. }
  68. }
  69. public function extractFactoryNamesFromComment() {
  70. $this->calls = array();
  71. for ($i = 0; $i < count($this->comment); $i++) {
  72. if ($this->extractFactoryNamesFromLine($this->comment[$i])) {
  73. unset($this->comment[$i]);
  74. }
  75. }
  76. $this->trimTrailingBlankLinesFromComment();
  77. }
  78. public function extractFactoryNamesFromLine($line) {
  79. if (preg_match('/^\s*@factory(\s+(.+))?$/', $line, $match)) {
  80. $this->createCalls($this->extraceFactoryNamesFromAnnotation(
  81. isset($match[2]) ? trim($match[2]) : null
  82. ));
  83. return true;
  84. }
  85. return false;
  86. }
  87. public function extraceFactoryNamesFromAnnotation($value) {
  88. $primaryName = $this->reflector->getName();
  89. if (empty($value)) {
  90. return array($primaryName);
  91. }
  92. preg_match_all('/(\.{3}|-|[a-zA-Z_][a-zA-Z_0-9]*)/', $value, $match);
  93. $names = $match[0];
  94. if (in_array('...', $names)) {
  95. $this->isVarArgs = true;
  96. }
  97. if (!in_array('-', $names) && !in_array($primaryName, $names)) {
  98. array_unshift($names, $primaryName);
  99. }
  100. return $names;
  101. }
  102. public function createCalls(array $names) {
  103. $names = array_unique($names);
  104. foreach ($names as $name) {
  105. if ($name != '-' && $name != '...') {
  106. $this->calls[] = new FactoryCall($this, $name);
  107. }
  108. }
  109. }
  110. public function extractParameters() {
  111. $this->parameters = array();
  112. if (!$this->isVarArgs) {
  113. foreach ($this->reflector->getParameters() as $parameter) {
  114. $this->parameters[] = new FactoryParameter($this, $parameter);
  115. }
  116. }
  117. }
  118. public function getParameterDeclarations() {
  119. if ($this->isVarArgs || !$this->hasParameters()) {
  120. return '';
  121. }
  122. $params = array();
  123. foreach ($this->parameters as $parameter) {
  124. $params[] = $parameter->getDeclaration();
  125. }
  126. return implode(', ', $params);
  127. }
  128. public function getParameterInvocations() {
  129. if ($this->isVarArgs) {
  130. return '';
  131. }
  132. $params = array();
  133. foreach ($this->parameters as $parameter) {
  134. $params[] = $parameter->getInvocation();
  135. }
  136. return implode(', ', $params);
  137. }
  138. public function getClass() {
  139. return $this->class;
  140. }
  141. public function getClassName() {
  142. return $this->class->getName();
  143. }
  144. public function getName() {
  145. return $this->reflector->name;
  146. }
  147. public function isFactory() {
  148. return count($this->calls) > 0;
  149. }
  150. public function getCalls() {
  151. return $this->calls;
  152. }
  153. public function acceptsVariableArguments() {
  154. return $this->isVarArgs;
  155. }
  156. public function hasParameters() {
  157. return !empty($this->parameters);
  158. }
  159. public function getParameters() {
  160. return $this->parameters;
  161. }
  162. public function getFullName() {
  163. return $this->getClassName() . '::' . $this->getName();
  164. }
  165. public function getCommentText() {
  166. return implode(PHP_EOL, $this->comment);
  167. }
  168. public function getComment($indent = '') {
  169. $comment = $indent . '/**';
  170. foreach ($this->comment as $line) {
  171. $comment .= PHP_EOL . $indent . ' * ' . $line;
  172. }
  173. $comment .= PHP_EOL . $indent . ' */';
  174. return $comment;
  175. }
  176. }