/library/phpdocx/lib/php_codesniffer/CodeSniffer/CommentParser/ParameterElement.php

https://github.com/r1zib/salesforce · PHP · 335 lines · 114 code · 56 blank · 165 comment · 11 complexity · b886e1c16bfa9192105cae16ef989b71 MD5 · raw file

  1. <?php
  2. /**
  3. * A class to represent param tags within a function comment.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @author Marc McIntyre <mmcintyre@squiz.net>
  11. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @version CVS: $Id: ParameterElement.php 270281 2008-12-02 02:38:34Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_CommentParser_AbstractDocElement', true) === false) {
  17. $error = 'Class PHP_CodeSniffer_CommentParser_AbstractDocElement not found';
  18. throw new PHP_CodeSniffer_Exception($error);
  19. }
  20. /**
  21. * A class to represent param tags within a function comment.
  22. *
  23. * @category PHP
  24. * @package PHP_CodeSniffer
  25. * @author Greg Sherwood <gsherwood@squiz.net>
  26. * @author Marc McIntyre <mmcintyre@squiz.net>
  27. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  28. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  29. * @version Release: 1.3.0RC1
  30. * @link http://pear.php.net/package/PHP_CodeSniffer
  31. */
  32. class PHP_CodeSniffer_CommentParser_ParameterElement extends PHP_CodeSniffer_CommentParser_AbstractDocElement
  33. {
  34. /**
  35. * The variable name of this parameter name, including the $ sign.
  36. *
  37. * @var string
  38. */
  39. private $_varName = '';
  40. /**
  41. * The comment of this parameter tag.
  42. *
  43. * @var string
  44. */
  45. private $_comment = '';
  46. /**
  47. * The variable type of this parameter tag.
  48. *
  49. * @var string
  50. */
  51. private $_type = '';
  52. /**
  53. * The whitespace that exists before the variable name.
  54. *
  55. * @var string
  56. */
  57. private $_varNameWhitespace = '';
  58. /**
  59. * The whitespace that exists before the comment.
  60. *
  61. * @var string
  62. */
  63. private $_commentWhitespace = null;
  64. /**
  65. * The whitespace that exists before the variable type.
  66. *
  67. * @var string
  68. */
  69. private $_typeWhitespace = '';
  70. /**
  71. * Constructs a PHP_CodeSniffer_CommentParser_ParameterElement.
  72. *
  73. * @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element
  74. * previous to
  75. * this one.
  76. * @param array $tokens The tokens
  77. * that make up
  78. * this element.
  79. * @param PHP_CodeSniffer_File $phpcsFile The file that
  80. * this element
  81. * is in.
  82. */
  83. public function __construct(
  84. $previousElement,
  85. $tokens,
  86. PHP_CodeSniffer_File $phpcsFile
  87. ) {
  88. parent::__construct($previousElement, $tokens, 'param', $phpcsFile);
  89. // Handle special variable type: array(x => y).
  90. $type = strtolower($this->_type);
  91. if ($this->_varName === '=>' && strpos($type, 'array(') !== false) {
  92. $rawContent = $this->getRawContent();
  93. $matches = array();
  94. $pattern = '/^(\s+)(array\(.*\))(\s+)(\$\S*)(\s+)(.*)/i';
  95. if (preg_match($pattern, $rawContent, $matches) !== 0) {
  96. // Process the sub elements correctly for this special case.
  97. if (count($matches) === 7) {
  98. $this->processSubElement('type', $matches[2], $matches[1]);
  99. $this->processSubElement('varName', $matches[4], $matches[3]);
  100. $this->processSubElement('comment', $matches[6], $matches[5]);
  101. }
  102. }
  103. }
  104. }//end __construct()
  105. /**
  106. * Returns the element names that this tag is comprised of, in the order
  107. * that they appear in the tag.
  108. *
  109. * @return array(string)
  110. * @see processSubElement()
  111. */
  112. protected function getSubElements()
  113. {
  114. return array(
  115. 'type',
  116. 'varName',
  117. 'comment',
  118. );
  119. }//end getSubElements()
  120. /**
  121. * Processes the sub element with the specified name.
  122. *
  123. * @param string $name The name of the sub element to process.
  124. * @param string $content The content of this sub element.
  125. * @param string $beforeWhitespace The whitespace that exists before the
  126. * sub element.
  127. *
  128. * @return void
  129. * @see getSubElements()
  130. */
  131. protected function processSubElement($name, $content, $beforeWhitespace)
  132. {
  133. $element = '_'.$name;
  134. $whitespace = $element.'Whitespace';
  135. $this->$element = $content;
  136. $this->$whitespace = $beforeWhitespace;
  137. }//end processSubElement()
  138. /**
  139. * Returns the variable name that this parameter tag represents.
  140. *
  141. * @return string
  142. */
  143. public function getVarName()
  144. {
  145. return $this->_varName;
  146. }//end getVarName()
  147. /**
  148. * Returns the variable type that this string represents.
  149. *
  150. * @return string
  151. */
  152. public function getType()
  153. {
  154. return $this->_type;
  155. }//end getType()
  156. /**
  157. * Returns the comment of this comment for this parameter.
  158. *
  159. * @return string
  160. */
  161. public function getComment()
  162. {
  163. return $this->_comment;
  164. }//end getComment()
  165. /**
  166. * Returns the whitespace before the variable type.
  167. *
  168. * @return stirng
  169. * @see getWhiteSpaceBeforeVarName()
  170. * @see getWhiteSpaceBeforeComment()
  171. */
  172. public function getWhiteSpaceBeforeType()
  173. {
  174. return $this->_typeWhitespace;
  175. }//end getWhiteSpaceBeforeType()
  176. /**
  177. * Returns the whitespace before the variable name.
  178. *
  179. * @return string
  180. * @see getWhiteSpaceBeforeComment()
  181. * @see getWhiteSpaceBeforeType()
  182. */
  183. public function getWhiteSpaceBeforeVarName()
  184. {
  185. return $this->_varNameWhitespace;
  186. }//end getWhiteSpaceBeforeVarName()
  187. /**
  188. * Returns the whitespace before the comment.
  189. *
  190. * @return string
  191. * @see getWhiteSpaceBeforeVarName()
  192. * @see getWhiteSpaceBeforeType()
  193. */
  194. public function getWhiteSpaceBeforeComment()
  195. {
  196. return $this->_commentWhitespace;
  197. }//end getWhiteSpaceBeforeComment()
  198. /**
  199. * Returns the postition of this parameter are it appears in the comment.
  200. *
  201. * This method differs from getOrder as it is only relative to method
  202. * parameters.
  203. *
  204. * @return int
  205. */
  206. public function getPosition()
  207. {
  208. if (($this->getPreviousElement() instanceof PHP_CodeSniffer_CommentParser_ParameterElement) === false) {
  209. return 1;
  210. } else {
  211. return ($this->getPreviousElement()->getPosition() + 1);
  212. }
  213. }//end getPosition()
  214. /**
  215. * Returns true if this parameter's variable aligns with the other's.
  216. *
  217. * @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
  218. * to check
  219. * alignment with.
  220. *
  221. * @return boolean
  222. */
  223. public function alignsVariableWith(
  224. PHP_CodeSniffer_CommentParser_ParameterElement $other
  225. ) {
  226. // Format is:
  227. // @param type $variable Comment.
  228. // @param <-a-><---b---->
  229. // Compares the index before param variable.
  230. $otherVar = (strlen($other->_type) + strlen($other->_varNameWhitespace));
  231. $thisVar = (strlen($this->_type) + strlen($this->_varNameWhitespace));
  232. if ($otherVar !== $thisVar) {
  233. return false;
  234. }
  235. return true;
  236. }//end alignsVariableWith()
  237. /**
  238. * Returns true if this parameter's comment aligns with the other's.
  239. *
  240. * @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
  241. * to check
  242. * alignment with.
  243. *
  244. * @return boolean
  245. */
  246. public function alignsCommentWith(
  247. PHP_CodeSniffer_CommentParser_ParameterElement $other
  248. ) {
  249. // Compares the index before param comment.
  250. $otherComment
  251. = (strlen($other->_varName) + strlen($other->_commentWhitespace));
  252. $thisComment
  253. = (strlen($this->_varName) + strlen($this->_commentWhitespace));
  254. if ($otherComment !== $thisComment) {
  255. return false;
  256. }
  257. return true;
  258. }//end alignsCommentWith()
  259. /**
  260. * Returns true if this parameter aligns with the other paramter.
  261. *
  262. * @param PHP_CodeSniffer_CommentParser_ParameterElement $other The other param
  263. * to check
  264. * alignment with.
  265. *
  266. * @return boolean
  267. */
  268. public function alignsWith(PHP_CodeSniffer_CommentParser_ParameterElement $other)
  269. {
  270. if ($this->alignsVariableWith($other) === false) {
  271. return false;
  272. }
  273. if ($this->alignsCommentWith($other) === false) {
  274. return false;
  275. }
  276. return true;
  277. }//end alignsWith()
  278. }//end class
  279. ?>