/tests/lib/PHP/CodeSniffer/CommentParser/ParameterElement.php

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