PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/php/PHP_CodeSniffer/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

http://github.com/jonswar/perl-code-tidyall
PHP | 479 lines | 353 code | 63 blank | 63 comment | 90 complexity | a8d719aeb94c4ad9976588e694fb355a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, 0BSD, MIT
  1. <?php
  2. /**
  3. * Checks the declaration of the class and its inheritance is correct.
  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\PSR2\Sniffs\Classes;
  10. use PHP_CodeSniffer\Standards\PEAR\Sniffs\Classes\ClassDeclarationSniff as PEARClassDeclarationSniff;
  11. use PHP_CodeSniffer\Util\Tokens;
  12. use PHP_CodeSniffer\Files\File;
  13. class ClassDeclarationSniff extends PEARClassDeclarationSniff
  14. {
  15. /**
  16. * The number of spaces code should be indented.
  17. *
  18. * @var integer
  19. */
  20. public $indent = 4;
  21. /**
  22. * Processes this test, when one of its tokens is encountered.
  23. *
  24. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  25. * @param int $stackPtr The position of the current token
  26. * in the stack passed in $tokens.
  27. *
  28. * @return void
  29. */
  30. public function process(File $phpcsFile, $stackPtr)
  31. {
  32. // We want all the errors from the PEAR standard, plus some of our own.
  33. parent::process($phpcsFile, $stackPtr);
  34. // Just in case.
  35. $tokens = $phpcsFile->getTokens();
  36. if (isset($tokens[$stackPtr]['scope_opener']) === false) {
  37. return;
  38. }
  39. $this->processOpen($phpcsFile, $stackPtr);
  40. $this->processClose($phpcsFile, $stackPtr);
  41. }//end process()
  42. /**
  43. * Processes the opening section of a class declaration.
  44. *
  45. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  46. * @param int $stackPtr The position of the current token
  47. * in the stack passed in $tokens.
  48. *
  49. * @return void
  50. */
  51. public function processOpen(File $phpcsFile, $stackPtr)
  52. {
  53. $tokens = $phpcsFile->getTokens();
  54. $stackPtrType = strtolower($tokens[$stackPtr]['content']);
  55. // Check alignment of the keyword and braces.
  56. if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
  57. $prevContent = $tokens[($stackPtr - 1)]['content'];
  58. if ($prevContent !== $phpcsFile->eolChar) {
  59. $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
  60. $spaces = strlen($blankSpace);
  61. if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL], true) === true
  62. && $spaces !== 1
  63. ) {
  64. $prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
  65. $error = 'Expected 1 space between %s and %s keywords; %s found';
  66. $data = [
  67. $prevContent,
  68. $stackPtrType,
  69. $spaces,
  70. ];
  71. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeKeyword', $data);
  72. if ($fix === true) {
  73. $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
  74. }
  75. }
  76. } else if ($tokens[($stackPtr - 2)]['code'] === T_ABSTRACT
  77. || $tokens[($stackPtr - 2)]['code'] === T_FINAL
  78. ) {
  79. $prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
  80. $error = 'Expected 1 space between %s and %s keywords; newline found';
  81. $data = [
  82. $prevContent,
  83. $stackPtrType,
  84. ];
  85. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NewlineBeforeKeyword', $data);
  86. if ($fix === true) {
  87. $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
  88. }
  89. }//end if
  90. }//end if
  91. // We'll need the indent of the class/interface declaration for later.
  92. $classIndent = 0;
  93. for ($i = ($stackPtr - 1); $i > 0; $i--) {
  94. if ($tokens[$i]['line'] === $tokens[$stackPtr]['line']) {
  95. continue;
  96. }
  97. // We changed lines.
  98. if ($tokens[($i + 1)]['code'] === T_WHITESPACE) {
  99. $classIndent = $tokens[($i + 1)]['length'];
  100. }
  101. break;
  102. }
  103. $className = $phpcsFile->findNext(T_STRING, $stackPtr);
  104. // Spacing of the keyword.
  105. $gap = $tokens[($stackPtr + 1)]['content'];
  106. if (strlen($gap) !== 1) {
  107. $found = strlen($gap);
  108. $error = 'Expected 1 space between %s keyword and %s name; %s found';
  109. $data = [
  110. $stackPtrType,
  111. $stackPtrType,
  112. $found,
  113. ];
  114. $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword', $data);
  115. if ($fix === true) {
  116. $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
  117. }
  118. }
  119. // Check after the class/interface name.
  120. if ($tokens[($className + 2)]['line'] === $tokens[$className]['line']) {
  121. $gap = $tokens[($className + 1)]['content'];
  122. if (strlen($gap) !== 1) {
  123. $found = strlen($gap);
  124. $error = 'Expected 1 space after %s name; %s found';
  125. $data = [
  126. $stackPtrType,
  127. $found,
  128. ];
  129. $fix = $phpcsFile->addFixableError($error, $className, 'SpaceAfterName', $data);
  130. if ($fix === true) {
  131. $phpcsFile->fixer->replaceToken(($className + 1), ' ');
  132. }
  133. }
  134. }
  135. $openingBrace = $tokens[$stackPtr]['scope_opener'];
  136. // Check positions of the extends and implements keywords.
  137. foreach (['extends', 'implements'] as $keywordType) {
  138. $keyword = $phpcsFile->findNext(constant('T_'.strtoupper($keywordType)), ($stackPtr + 1), $openingBrace);
  139. if ($keyword !== false) {
  140. if ($tokens[$keyword]['line'] !== $tokens[$stackPtr]['line']) {
  141. $error = 'The '.$keywordType.' keyword must be on the same line as the %s name';
  142. $data = [$stackPtrType];
  143. $fix = $phpcsFile->addFixableError($error, $keyword, ucfirst($keywordType).'Line', $data);
  144. if ($fix === true) {
  145. $phpcsFile->fixer->beginChangeset();
  146. $comments = [];
  147. for ($i = ($stackPtr + 1); $i < $keyword; ++$i) {
  148. if ($tokens[$i]['code'] === T_COMMENT) {
  149. $comments[] = trim($tokens[$i]['content']);
  150. }
  151. if ($tokens[$i]['code'] === T_WHITESPACE
  152. || $tokens[$i]['code'] === T_COMMENT
  153. ) {
  154. $phpcsFile->fixer->replaceToken($i, ' ');
  155. }
  156. }
  157. $phpcsFile->fixer->addContent($stackPtr, ' ');
  158. if (empty($comments) === false) {
  159. $i = $keyword;
  160. while ($tokens[($i + 1)]['line'] === $tokens[$keyword]['line']) {
  161. ++$i;
  162. }
  163. $phpcsFile->fixer->addContentBefore($i, ' '.implode(' ', $comments));
  164. }
  165. $phpcsFile->fixer->endChangeset();
  166. }//end if
  167. } else {
  168. // Check the whitespace before. Whitespace after is checked
  169. // later by looking at the whitespace before the first class name
  170. // in the list.
  171. $gap = $tokens[($keyword - 1)]['length'];
  172. if ($gap !== 1) {
  173. $error = 'Expected 1 space before '.$keywordType.' keyword; %s found';
  174. $data = [$gap];
  175. $fix = $phpcsFile->addFixableError($error, $keyword, 'SpaceBefore'.ucfirst($keywordType), $data);
  176. if ($fix === true) {
  177. $phpcsFile->fixer->replaceToken(($keyword - 1), ' ');
  178. }
  179. }
  180. }//end if
  181. }//end if
  182. }//end foreach
  183. // Check each of the extends/implements class names. If the extends/implements
  184. // keyword is the last content on the line, it means we need to check for
  185. // the multi-line format, so we do not include the class names
  186. // from the extends/implements list in the following check.
  187. // Note that classes can only extend one other class, so they can't use a
  188. // multi-line extends format, whereas an interface can extend multiple
  189. // other interfaces, and so uses a multi-line extends format.
  190. if ($tokens[$stackPtr]['code'] === T_INTERFACE) {
  191. $keywordTokenType = T_EXTENDS;
  192. } else {
  193. $keywordTokenType = T_IMPLEMENTS;
  194. }
  195. $implements = $phpcsFile->findNext($keywordTokenType, ($stackPtr + 1), $openingBrace);
  196. $multiLineImplements = false;
  197. if ($implements !== false) {
  198. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $implements, true);
  199. if ($tokens[$prev]['line'] !== $tokens[$implements]['line']) {
  200. $multiLineImplements = true;
  201. }
  202. }
  203. $find = [
  204. T_STRING,
  205. $keywordTokenType,
  206. ];
  207. $classNames = [];
  208. $nextClass = $phpcsFile->findNext($find, ($className + 2), ($openingBrace - 1));
  209. while ($nextClass !== false) {
  210. $classNames[] = $nextClass;
  211. $nextClass = $phpcsFile->findNext($find, ($nextClass + 1), ($openingBrace - 1));
  212. }
  213. $classCount = count($classNames);
  214. $checkingImplements = false;
  215. $implementsToken = null;
  216. foreach ($classNames as $n => $className) {
  217. if ($tokens[$className]['code'] === $keywordTokenType) {
  218. $checkingImplements = true;
  219. $implementsToken = $className;
  220. continue;
  221. }
  222. if ($checkingImplements === true
  223. && $multiLineImplements === true
  224. && ($tokens[($className - 1)]['code'] !== T_NS_SEPARATOR
  225. || $tokens[($className - 2)]['code'] !== T_STRING)
  226. ) {
  227. $prev = $phpcsFile->findPrevious(
  228. [
  229. T_NS_SEPARATOR,
  230. T_WHITESPACE,
  231. ],
  232. ($className - 1),
  233. $implements,
  234. true
  235. );
  236. if ($prev === $implementsToken && $tokens[$className]['line'] !== ($tokens[$prev]['line'] + 1)) {
  237. if ($keywordTokenType === T_EXTENDS) {
  238. $error = 'The first item in a multi-line extends list must be on the line following the extends keyword';
  239. $fix = $phpcsFile->addFixableError($error, $className, 'FirstExtendsInterfaceSameLine');
  240. } else {
  241. $error = 'The first item in a multi-line implements list must be on the line following the implements keyword';
  242. $fix = $phpcsFile->addFixableError($error, $className, 'FirstInterfaceSameLine');
  243. }
  244. if ($fix === true) {
  245. $phpcsFile->fixer->beginChangeset();
  246. for ($i = ($prev + 1); $i < $className; $i++) {
  247. if ($tokens[$i]['code'] !== T_WHITESPACE) {
  248. break;
  249. }
  250. $phpcsFile->fixer->replaceToken($i, '');
  251. }
  252. $phpcsFile->fixer->addNewline($prev);
  253. $phpcsFile->fixer->endChangeset();
  254. }
  255. } else if ($tokens[$prev]['line'] !== ($tokens[$className]['line'] - 1)) {
  256. if ($keywordTokenType === T_EXTENDS) {
  257. $error = 'Only one interface may be specified per line in a multi-line extends declaration';
  258. $fix = $phpcsFile->addFixableError($error, $className, 'ExtendsInterfaceSameLine');
  259. } else {
  260. $error = 'Only one interface may be specified per line in a multi-line implements declaration';
  261. $fix = $phpcsFile->addFixableError($error, $className, 'InterfaceSameLine');
  262. }
  263. if ($fix === true) {
  264. $phpcsFile->fixer->beginChangeset();
  265. for ($i = ($prev + 1); $i < $className; $i++) {
  266. if ($tokens[$i]['code'] !== T_WHITESPACE) {
  267. break;
  268. }
  269. $phpcsFile->fixer->replaceToken($i, '');
  270. }
  271. $phpcsFile->fixer->addNewline($prev);
  272. $phpcsFile->fixer->endChangeset();
  273. }
  274. } else {
  275. $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($className - 1), $implements);
  276. if ($tokens[$prev]['line'] !== $tokens[$className]['line']) {
  277. $found = 0;
  278. } else {
  279. $found = $tokens[$prev]['length'];
  280. }
  281. $expected = ($classIndent + $this->indent);
  282. if ($found !== $expected) {
  283. $error = 'Expected %s spaces before interface name; %s found';
  284. $data = [
  285. $expected,
  286. $found,
  287. ];
  288. $fix = $phpcsFile->addFixableError($error, $className, 'InterfaceWrongIndent', $data);
  289. if ($fix === true) {
  290. $padding = str_repeat(' ', $expected);
  291. if ($found === 0) {
  292. $phpcsFile->fixer->addContent($prev, $padding);
  293. } else {
  294. $phpcsFile->fixer->replaceToken($prev, $padding);
  295. }
  296. }
  297. }
  298. }//end if
  299. } else if ($tokens[($className - 1)]['code'] !== T_NS_SEPARATOR
  300. || $tokens[($className - 2)]['code'] !== T_STRING
  301. ) {
  302. // Not part of a longer fully qualified class name.
  303. if ($tokens[($className - 1)]['code'] === T_COMMA
  304. || ($tokens[($className - 1)]['code'] === T_NS_SEPARATOR
  305. && $tokens[($className - 2)]['code'] === T_COMMA)
  306. ) {
  307. $error = 'Expected 1 space before "%s"; 0 found';
  308. $data = [$tokens[$className]['content']];
  309. $fix = $phpcsFile->addFixableError($error, ($nextComma + 1), 'NoSpaceBeforeName', $data);
  310. if ($fix === true) {
  311. $phpcsFile->fixer->addContentBefore(($nextComma + 1), ' ');
  312. }
  313. } else {
  314. if ($tokens[($className - 1)]['code'] === T_NS_SEPARATOR) {
  315. $prev = ($className - 2);
  316. } else {
  317. $prev = ($className - 1);
  318. }
  319. $last = $phpcsFile->findPrevious(T_WHITESPACE, $prev, null, true);
  320. $content = $phpcsFile->getTokensAsString(($last + 1), ($prev - $last));
  321. if ($content !== ' ') {
  322. $found = strlen($content);
  323. $error = 'Expected 1 space before "%s"; %s found';
  324. $data = [
  325. $tokens[$className]['content'],
  326. $found,
  327. ];
  328. $fix = $phpcsFile->addFixableError($error, $className, 'SpaceBeforeName', $data);
  329. if ($fix === true) {
  330. if ($tokens[$prev]['code'] === T_WHITESPACE) {
  331. $phpcsFile->fixer->beginChangeset();
  332. $phpcsFile->fixer->replaceToken($prev, ' ');
  333. while ($tokens[--$prev]['code'] === T_WHITESPACE) {
  334. $phpcsFile->fixer->replaceToken($prev, ' ');
  335. }
  336. $phpcsFile->fixer->endChangeset();
  337. } else {
  338. $phpcsFile->fixer->addContent($prev, ' ');
  339. }
  340. }
  341. }//end if
  342. }//end if
  343. }//end if
  344. if ($checkingImplements === true
  345. && $tokens[($className + 1)]['code'] !== T_NS_SEPARATOR
  346. && $tokens[($className + 1)]['code'] !== T_COMMA
  347. ) {
  348. if ($n !== ($classCount - 1)) {
  349. // This is not the last class name, and the comma
  350. // is not where we expect it to be.
  351. if ($tokens[($className + 2)]['code'] !== $keywordTokenType) {
  352. $error = 'Expected 0 spaces between "%s" and comma; %s found';
  353. $data = [
  354. $tokens[$className]['content'],
  355. $tokens[($className + 1)]['length'],
  356. ];
  357. $fix = $phpcsFile->addFixableError($error, $className, 'SpaceBeforeComma', $data);
  358. if ($fix === true) {
  359. $phpcsFile->fixer->replaceToken(($className + 1), '');
  360. }
  361. }
  362. }
  363. $nextComma = $phpcsFile->findNext(T_COMMA, $className);
  364. } else {
  365. $nextComma = ($className + 1);
  366. }//end if
  367. }//end foreach
  368. }//end processOpen()
  369. /**
  370. * Processes the closing section of a class declaration.
  371. *
  372. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  373. * @param int $stackPtr The position of the current token
  374. * in the stack passed in $tokens.
  375. *
  376. * @return void
  377. */
  378. public function processClose(File $phpcsFile, $stackPtr)
  379. {
  380. $tokens = $phpcsFile->getTokens();
  381. // Check that the closing brace comes right after the code body.
  382. $closeBrace = $tokens[$stackPtr]['scope_closer'];
  383. $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
  384. if ($prevContent !== $tokens[$stackPtr]['scope_opener']
  385. && $tokens[$prevContent]['line'] !== ($tokens[$closeBrace]['line'] - 1)
  386. ) {
  387. $error = 'The closing brace for the %s must go on the next line after the body';
  388. $data = [$tokens[$stackPtr]['content']];
  389. $fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data);
  390. if ($fix === true) {
  391. $phpcsFile->fixer->beginChangeset();
  392. for ($i = ($prevContent + 1); $i < $closeBrace; $i++) {
  393. $phpcsFile->fixer->replaceToken($i, '');
  394. }
  395. if (strpos($tokens[$prevContent]['content'], $phpcsFile->eolChar) === false) {
  396. $phpcsFile->fixer->replaceToken($closeBrace, $phpcsFile->eolChar.$tokens[$closeBrace]['content']);
  397. }
  398. $phpcsFile->fixer->endChangeset();
  399. }
  400. }//end if
  401. // Check the closing brace is on it's own line, but allow
  402. // for comments like "//end class".
  403. $ignoreTokens = Tokens::$phpcsCommentTokens;
  404. $ignoreTokens[] = T_WHITESPACE;
  405. $ignoreTokens[] = T_COMMENT;
  406. $nextContent = $phpcsFile->findNext($ignoreTokens, ($closeBrace + 1), null, true);
  407. if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar
  408. && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
  409. ) {
  410. $type = strtolower($tokens[$stackPtr]['content']);
  411. $error = 'Closing %s brace must be on a line by itself';
  412. $data = [$type];
  413. $phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data);
  414. }
  415. }//end processClose()
  416. }//end class