Method.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Class method documentation generator.
  4. *
  5. * @package Kohana/Userguide
  6. * @category Base
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. class Kohana_Kodoc_Method extends Kodoc {
  12. /**
  13. * @var ReflectionMethod The ReflectionMethod for this class
  14. */
  15. public $method;
  16. /**
  17. * @var array Array of Kodoc_Method_Param
  18. */
  19. public $params;
  20. /**
  21. * @var array The things this function can return
  22. */
  23. public $return = [];
  24. /**
  25. * @var string The source code for this function
  26. */
  27. public $source;
  28. public function __construct($class, $method)
  29. {
  30. $this->method = new ReflectionMethod($class, $method);
  31. $this->class = $parent = $this->method->getDeclaringClass();
  32. if ($modifiers = $this->method->getModifiers())
  33. {
  34. $this->modifiers = '<small>'.implode(' ', Reflection::getModifierNames($modifiers)).'</small> ';
  35. }
  36. do
  37. {
  38. if ($parent->hasMethod($method) AND $comment = $parent->getMethod($method)->getDocComment())
  39. {
  40. // Found a description for this method
  41. break;
  42. }
  43. }
  44. while ($parent = $parent->getParentClass());
  45. list($this->description, $tags) = Kodoc::parse($comment);
  46. if (($file = $this->class->getFileName()) AND empty($this->class->getTraitNames()))
  47. {
  48. $this->source = Kodoc::source($file, $this->method->getStartLine(), $this->method->getEndLine());
  49. }
  50. if (isset($tags['param']))
  51. {
  52. $params = [];
  53. foreach ($this->method->getParameters() as $i => $param)
  54. {
  55. $param = new Kodoc_Method_Param([$this->method->class, $this->method->name],$i);
  56. if (isset($tags['param'][$i]))
  57. {
  58. preg_match('/^(\S+)(?:\s*(?:\$'.$param->name.'\s*)?(.+))?$/s', $tags['param'][$i], $matches);
  59. $param->type = $matches[1];
  60. if (isset($matches[2]))
  61. {
  62. $param->description = ucfirst($matches[2]);
  63. }
  64. }
  65. $params[] = $param;
  66. }
  67. $this->params = $params;
  68. unset($tags['param']);
  69. }
  70. if (isset($tags['return']))
  71. {
  72. foreach ($tags['return'] as $return)
  73. {
  74. if (preg_match('/^(\S*)(?:\s*(.+?))?$/', $return, $matches))
  75. {
  76. $this->return[] = [$matches[1], isset($matches[2]) ? $matches[2] : ''];
  77. }
  78. }
  79. unset($tags['return']);
  80. }
  81. $this->tags = $tags;
  82. }
  83. public function params_short()
  84. {
  85. $out = '';
  86. $required = TRUE;
  87. $first = TRUE;
  88. foreach ($this->params as $param)
  89. {
  90. if ($required AND $param->default AND $first)
  91. {
  92. $out .= '[ '.$param;
  93. $required = FALSE;
  94. $first = FALSE;
  95. }
  96. elseif ($required AND $param->default)
  97. {
  98. $out .= '[, '.$param;
  99. $required = FALSE;
  100. }
  101. elseif ($first)
  102. {
  103. $out .= $param;
  104. $first = FALSE;
  105. }
  106. else
  107. {
  108. $out .= ', '.$param;
  109. }
  110. }
  111. if ( ! $required)
  112. {
  113. $out .= '] ';
  114. }
  115. return $out;
  116. }
  117. } // End Kodoc_Method