PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/php/PHP_CodeSniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

http://github.com/jonswar/perl-code-tidyall
PHP | 481 lines | 322 code | 60 blank | 99 comment | 102 complexity | e0255dcdfb0d0c8f88822fba5fcb16e3 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, 0BSD, MIT
  1. <?php
  2. /**
  3. * Ensure single and multi-line function declarations are defined correctly.
  4. *
  5. * @author Greg Sherwood <gsherwood@squiz.net>
  6. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8. */
  9. namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions;
  10. use PHP_CodeSniffer\Sniffs\Sniff;
  11. use PHP_CodeSniffer\Files\File;
  12. use PHP_CodeSniffer\Util\Tokens;
  13. use PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\OpeningFunctionBraceKernighanRitchieSniff;
  14. use PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\OpeningFunctionBraceBsdAllmanSniff;
  15. class FunctionDeclarationSniff implements Sniff
  16. {
  17. /**
  18. * A list of tokenizers this sniff supports.
  19. *
  20. * @var array
  21. */
  22. public $supportedTokenizers = [
  23. 'PHP',
  24. 'JS',
  25. ];
  26. /**
  27. * The number of spaces code should be indented.
  28. *
  29. * @var integer
  30. */
  31. public $indent = 4;
  32. /**
  33. * Returns an array of tokens this test wants to listen for.
  34. *
  35. * @return array
  36. */
  37. public function register()
  38. {
  39. return [
  40. T_FUNCTION,
  41. T_CLOSURE,
  42. ];
  43. }//end register()
  44. /**
  45. * Processes this test, when one of its tokens is encountered.
  46. *
  47. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  48. * @param int $stackPtr The position of the current token
  49. * in the stack passed in $tokens.
  50. *
  51. * @return void
  52. */
  53. public function process(File $phpcsFile, $stackPtr)
  54. {
  55. $tokens = $phpcsFile->getTokens();
  56. if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
  57. || isset($tokens[$stackPtr]['parenthesis_closer']) === false
  58. || $tokens[$stackPtr]['parenthesis_opener'] === null
  59. || $tokens[$stackPtr]['parenthesis_closer'] === null
  60. ) {
  61. return;
  62. }
  63. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  64. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  65. if (strtolower($tokens[$stackPtr]['content']) === 'function') {
  66. // Must be one space after the FUNCTION keyword.
  67. if ($tokens[($stackPtr + 1)]['content'] === $phpcsFile->eolChar) {
  68. $spaces = 'newline';
  69. } else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
  70. $spaces = $tokens[($stackPtr + 1)]['length'];
  71. } else {
  72. $spaces = 0;
  73. }
  74. if ($spaces !== 1) {
  75. $error = 'Expected 1 space after FUNCTION keyword; %s found';
  76. $data = [$spaces];
  77. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterFunction', $data);
  78. if ($fix === true) {
  79. if ($spaces === 0) {
  80. $phpcsFile->fixer->addContent($stackPtr, ' ');
  81. } else {
  82. $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
  83. }
  84. }
  85. }
  86. }//end if
  87. // Must be no space before the opening parenthesis. For closures, this is
  88. // enforced by the previous check because there is no content between the keywords
  89. // and the opening parenthesis.
  90. // Unfinished closures are tokenized as T_FUNCTION however, and can be excluded
  91. // by checking for the scope_opener.
  92. if ($tokens[$stackPtr]['code'] === T_FUNCTION
  93. && (isset($tokens[$stackPtr]['scope_opener']) === true || $phpcsFile->getMethodProperties($stackPtr)['has_body'] === false)
  94. ) {
  95. if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) {
  96. $spaces = 'newline';
  97. } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) {
  98. $spaces = $tokens[($openBracket - 1)]['length'];
  99. } else {
  100. $spaces = 0;
  101. }
  102. if ($spaces !== 0) {
  103. $error = 'Expected 0 spaces before opening parenthesis; %s found';
  104. $data = [$spaces];
  105. $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data);
  106. if ($fix === true) {
  107. $phpcsFile->fixer->replaceToken(($openBracket - 1), '');
  108. }
  109. }
  110. // Must be no space before semicolon in abstract/interface methods.
  111. if ($phpcsFile->getMethodProperties($stackPtr)['has_body'] === false) {
  112. $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket);
  113. if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
  114. $spaces = 'newline';
  115. } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {
  116. $spaces = $tokens[($end - 1)]['length'];
  117. } else {
  118. $spaces = 0;
  119. }
  120. if ($spaces !== 0) {
  121. $error = 'Expected 0 spaces before semicolon; %s found';
  122. $data = [$spaces];
  123. $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data);
  124. if ($fix === true) {
  125. $phpcsFile->fixer->replaceToken(($end - 1), '');
  126. }
  127. }
  128. }
  129. }//end if
  130. // Must be one space before and after USE keyword for closures.
  131. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  132. $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
  133. if ($use !== false) {
  134. if ($tokens[($use + 1)]['code'] !== T_WHITESPACE) {
  135. $length = 0;
  136. } else if ($tokens[($use + 1)]['content'] === "\t") {
  137. $length = '\t';
  138. } else {
  139. $length = $tokens[($use + 1)]['length'];
  140. }
  141. if ($length !== 1) {
  142. $error = 'Expected 1 space after USE keyword; found %s';
  143. $data = [$length];
  144. $fix = $phpcsFile->addFixableError($error, $use, 'SpaceAfterUse', $data);
  145. if ($fix === true) {
  146. if ($length === 0) {
  147. $phpcsFile->fixer->addContent($use, ' ');
  148. } else {
  149. $phpcsFile->fixer->replaceToken(($use + 1), ' ');
  150. }
  151. }
  152. }
  153. if ($tokens[($use - 1)]['code'] !== T_WHITESPACE) {
  154. $length = 0;
  155. } else if ($tokens[($use - 1)]['content'] === "\t") {
  156. $length = '\t';
  157. } else {
  158. $length = $tokens[($use - 1)]['length'];
  159. }
  160. if ($length !== 1) {
  161. $error = 'Expected 1 space before USE keyword; found %s';
  162. $data = [$length];
  163. $fix = $phpcsFile->addFixableError($error, $use, 'SpaceBeforeUse', $data);
  164. if ($fix === true) {
  165. if ($length === 0) {
  166. $phpcsFile->fixer->addContentBefore($use, ' ');
  167. } else {
  168. $phpcsFile->fixer->replaceToken(($use - 1), ' ');
  169. }
  170. }
  171. }
  172. }//end if
  173. }//end if
  174. if ($this->isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
  175. $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
  176. } else {
  177. $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
  178. }
  179. }//end process()
  180. /**
  181. * Determine if this is a multi-line function declaration.
  182. *
  183. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  184. * @param int $stackPtr The position of the current token
  185. * in the stack passed in $tokens.
  186. * @param int $openBracket The position of the opening bracket
  187. * in the stack passed in $tokens.
  188. * @param array $tokens The stack of tokens that make up
  189. * the file.
  190. *
  191. * @return bool
  192. */
  193. public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens)
  194. {
  195. $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
  196. if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
  197. return true;
  198. }
  199. // Closures may use the USE keyword and so be multi-line in this way.
  200. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  201. $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
  202. if ($use !== false) {
  203. // If the opening and closing parenthesis of the use statement
  204. // are also on the same line, this is a single line declaration.
  205. $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
  206. $close = $tokens[$open]['parenthesis_closer'];
  207. if ($tokens[$open]['line'] !== $tokens[$close]['line']) {
  208. return true;
  209. }
  210. }
  211. }
  212. return false;
  213. }//end isMultiLineDeclaration()
  214. /**
  215. * Processes single-line declarations.
  216. *
  217. * Just uses the Generic BSD-Allman brace sniff.
  218. *
  219. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  220. * @param int $stackPtr The position of the current token
  221. * in the stack passed in $tokens.
  222. * @param array $tokens The stack of tokens that make up
  223. * the file.
  224. *
  225. * @return void
  226. */
  227. public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens)
  228. {
  229. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  230. $sniff = new OpeningFunctionBraceKernighanRitchieSniff();
  231. } else {
  232. $sniff = new OpeningFunctionBraceBsdAllmanSniff();
  233. }
  234. $sniff->checkClosures = true;
  235. $sniff->process($phpcsFile, $stackPtr);
  236. }//end processSingleLineDeclaration()
  237. /**
  238. * Processes multi-line declarations.
  239. *
  240. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  241. * @param int $stackPtr The position of the current token
  242. * in the stack passed in $tokens.
  243. * @param array $tokens The stack of tokens that make up
  244. * the file.
  245. *
  246. * @return void
  247. */
  248. public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
  249. {
  250. // We need to work out how far indented the function
  251. // declaration itself is, so we can work out how far to
  252. // indent parameters.
  253. $functionIndent = 0;
  254. for ($i = ($stackPtr - 1); $i >= 0; $i--) {
  255. if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
  256. break;
  257. }
  258. }
  259. // Move $i back to the line the function is or to 0.
  260. $i++;
  261. if ($tokens[$i]['code'] === T_WHITESPACE) {
  262. $functionIndent = $tokens[$i]['length'];
  263. }
  264. // The closing parenthesis must be on a new line, even
  265. // when checking abstract function definitions.
  266. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  267. $prev = $phpcsFile->findPrevious(
  268. T_WHITESPACE,
  269. ($closeBracket - 1),
  270. null,
  271. true
  272. );
  273. if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) {
  274. if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
  275. $error = 'The closing parenthesis of a multi-line function declaration must be on a new line';
  276. $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine');
  277. if ($fix === true) {
  278. $phpcsFile->fixer->addNewlineBefore($closeBracket);
  279. }
  280. }
  281. }
  282. // If this is a closure and is using a USE statement, the closing
  283. // parenthesis we need to look at from now on is the closing parenthesis
  284. // of the USE statement.
  285. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  286. $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
  287. if ($use !== false) {
  288. $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
  289. $closeBracket = $tokens[$open]['parenthesis_closer'];
  290. $prev = $phpcsFile->findPrevious(
  291. T_WHITESPACE,
  292. ($closeBracket - 1),
  293. null,
  294. true
  295. );
  296. if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) {
  297. if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
  298. $error = 'The closing parenthesis of a multi-line use declaration must be on a new line';
  299. $fix = $phpcsFile->addFixableError($error, $closeBracket, 'UseCloseBracketLine');
  300. if ($fix === true) {
  301. $phpcsFile->fixer->addNewlineBefore($closeBracket);
  302. }
  303. }
  304. }
  305. }//end if
  306. }//end if
  307. // Each line between the parenthesis should be indented 4 spaces.
  308. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  309. $lastLine = $tokens[$openBracket]['line'];
  310. for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
  311. if ($tokens[$i]['line'] !== $lastLine) {
  312. if ($i === $tokens[$stackPtr]['parenthesis_closer']
  313. || ($tokens[$i]['code'] === T_WHITESPACE
  314. && (($i + 1) === $closeBracket
  315. || ($i + 1) === $tokens[$stackPtr]['parenthesis_closer']))
  316. ) {
  317. // Closing braces need to be indented to the same level
  318. // as the function.
  319. $expectedIndent = $functionIndent;
  320. } else {
  321. $expectedIndent = ($functionIndent + $this->indent);
  322. }
  323. // We changed lines, so this should be a whitespace indent token.
  324. if ($tokens[$i]['code'] !== T_WHITESPACE) {
  325. $foundIndent = 0;
  326. } else if ($tokens[$i]['line'] !== $tokens[($i + 1)]['line']) {
  327. // This is an empty line, so don't check the indent.
  328. $foundIndent = $expectedIndent;
  329. $error = 'Blank lines are not allowed in a multi-line function declaration';
  330. $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine');
  331. if ($fix === true) {
  332. $phpcsFile->fixer->replaceToken($i, '');
  333. }
  334. } else {
  335. $foundIndent = $tokens[$i]['length'];
  336. }
  337. if ($expectedIndent !== $foundIndent) {
  338. $error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s';
  339. $data = [
  340. $expectedIndent,
  341. $foundIndent,
  342. ];
  343. $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data);
  344. if ($fix === true) {
  345. $spaces = str_repeat(' ', $expectedIndent);
  346. if ($foundIndent === 0) {
  347. $phpcsFile->fixer->addContentBefore($i, $spaces);
  348. } else {
  349. $phpcsFile->fixer->replaceToken($i, $spaces);
  350. }
  351. }
  352. }
  353. $lastLine = $tokens[$i]['line'];
  354. }//end if
  355. if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
  356. // Skip arrays as they have their own indentation rules.
  357. if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
  358. $i = $tokens[$i]['bracket_closer'];
  359. } else {
  360. $i = $tokens[$i]['parenthesis_closer'];
  361. }
  362. $lastLine = $tokens[$i]['line'];
  363. continue;
  364. }
  365. }//end for
  366. if (isset($tokens[$stackPtr]['scope_opener']) === false) {
  367. return;
  368. }
  369. // The opening brace needs to be one space away from the closing parenthesis.
  370. $opener = $tokens[$stackPtr]['scope_opener'];
  371. if ($tokens[$opener]['line'] !== $tokens[$closeBracket]['line']) {
  372. $error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line';
  373. $fix = $phpcsFile->addFixableError($error, $opener, 'NewlineBeforeOpenBrace');
  374. if ($fix === true) {
  375. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), $closeBracket, true);
  376. $phpcsFile->fixer->beginChangeset();
  377. $phpcsFile->fixer->addContent($prev, ' {');
  378. // If the opener is on a line by itself, removing it will create
  379. // an empty line, so just remove the entire line instead.
  380. $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($opener - 1), $closeBracket, true);
  381. $next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true);
  382. if ($tokens[$prev]['line'] < $tokens[$opener]['line']
  383. && $tokens[$next]['line'] > $tokens[$opener]['line']
  384. ) {
  385. // Clear the whole line.
  386. for ($i = ($prev + 1); $i < $next; $i++) {
  387. if ($tokens[$i]['line'] === $tokens[$opener]['line']) {
  388. $phpcsFile->fixer->replaceToken($i, '');
  389. }
  390. }
  391. } else {
  392. // Just remove the opener.
  393. $phpcsFile->fixer->replaceToken($opener, '');
  394. if ($tokens[$next]['line'] === $tokens[$opener]['line']) {
  395. $phpcsFile->fixer->replaceToken(($opener + 1), '');
  396. }
  397. }
  398. $phpcsFile->fixer->endChangeset();
  399. }//end if
  400. } else {
  401. $prev = $tokens[($opener - 1)];
  402. if ($prev['code'] !== T_WHITESPACE) {
  403. $length = 0;
  404. } else {
  405. $length = strlen($prev['content']);
  406. }
  407. if ($length !== 1) {
  408. $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces';
  409. $fix = $phpcsFile->addFixableError($error, ($opener - 1), 'SpaceBeforeOpenBrace', [$length]);
  410. if ($fix === true) {
  411. if ($length === 0) {
  412. $phpcsFile->fixer->addContentBefore($opener, ' ');
  413. } else {
  414. $phpcsFile->fixer->replaceToken(($opener - 1), ' ');
  415. }
  416. }
  417. return;
  418. }//end if
  419. }//end if
  420. }//end processMultiLineDeclaration()
  421. }//end class