PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/dcycleproject.org/modules/contrib/coder/coder_sniffer/Test/bad.php

https://gitlab.com/leevh/dcyclesite
PHP | 798 lines | 389 code | 135 blank | 274 comment | 43 complexity | b3a992fe7b3e2f64864291a150016b84 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?
  2. #Discouraged comment style
  3. //Inline comments need to be prefixed by a space after the //.
  4. // But there should be only one space.
  5. // Not more.
  6. // Now follows a list:
  7. // - item 1
  8. // wrong indentation level here.
  9. // - now follows a sub-list:
  10. // - should be only 2 additional spaces.
  11. // - this one is correct.
  12. // Blank comments are not allowed outside of a comment block.
  13. //
  14. // PHP Constants should be written in CAPITAL letters
  15. true; // Comments should be on a separate line.
  16. false;
  17. // Comment indented too far.
  18. null;
  19. // Missing spaces
  20. if(TRUE){
  21. TRUE;
  22. }
  23. if (TRUE){
  24. TRUE;
  25. }
  26. if(TRUE) {
  27. TRUE;
  28. }
  29. define('C_TEST',1);
  30. // The global keyword must be all lower case.
  31. // Custom global variables must be prefixed with an underscore.
  32. GLOBAL $myvar, $user;
  33. // Has whitespace at the end of the line.
  34. $whitespaces = 'Yes, Please';
  35. // Operators - must have a space before and after
  36. $i= 0;
  37. $i+= 0;
  38. $i-= 0;
  39. $i== 0;
  40. $i=== 0;
  41. $i!== 0;
  42. $i!= 0;
  43. $i> 0;
  44. $i< 0;
  45. $i>= 0;
  46. $i<= 0;
  47. $i=0;
  48. $i+=0;
  49. $i-=0;
  50. $i==0;
  51. $i===0;
  52. $i!==0;
  53. $i!=0;
  54. $i>0;
  55. $i<0;
  56. $i>=0;
  57. $i<=0;
  58. $i =0;
  59. $i +=0;
  60. $i -=0;
  61. $i ==0;
  62. $i ==0;
  63. $i !=0;
  64. $i >0;
  65. $i <0;
  66. $i >=0;
  67. $i <=0;
  68. $i += 0;
  69. $i -= 0;
  70. $i == 0;
  71. $i === 0;
  72. $i !== 0;
  73. $i != 0;
  74. $i > 0;
  75. $i < 0;
  76. $i >= 0;
  77. $i <= 0;
  78. $i = 0;
  79. $i += 0;
  80. $i -= 0;
  81. $i == 0;
  82. $i === 0;
  83. $i !== 0;
  84. $i != 0;
  85. $i > 0;
  86. $i < 0;
  87. $i >= 0;
  88. $i <= 0;
  89. // Arihtmetic operators, too.
  90. $i = 1+1;
  91. $i = 1- 1;
  92. $i = 1 /2;
  93. $i = 1 * 1;
  94. // Unary operators must not have a space
  95. $i --;
  96. -- $i;
  97. $i ++;
  98. ++ $i;
  99. $i = - 1;
  100. $i = + 1;
  101. $i = -1;
  102. $i = +1;
  103. array('i' => - 1);
  104. array('i' => + 1);
  105. $i = (1 == - 1);
  106. $i = (1 === - 1);
  107. $i = (1 == + 1);
  108. $i = (1 === + 1);
  109. range(-50, - 45);
  110. range(- 50, -45);
  111. $i[0]+ 1;
  112. $x->{$i}+ 1;
  113. ! $x;
  114. ! ($x + $y);
  115. // Casting must have one space
  116. (int)$i;
  117. (int) $i;
  118. // Last item of a multiline array has to be followed by a
  119. // comma. On inline arrays not!
  120. $a = array('1', '2', '3',);
  121. $a = array(
  122. '1',
  123. '2',
  124. '3'
  125. );
  126. $a = array('1', '2', array('3', ));
  127. $a = array('1', '2', array('3',), );
  128. $a = array('1', '2', array('3'), );
  129. $a = array('1', '2', array('3',), );
  130. $a = array('1', '2', array(
  131. '3'),
  132. );
  133. $a = array(
  134. '1',
  135. '2',
  136. array('3',)
  137. );
  138. // Missing comma on the last item.
  139. $field = array(
  140. 'field_name' => 'test_text',
  141. 'type' => 'text'
  142. );
  143. // Array white space style.
  144. $a = array ('1');
  145. $a = array( '1');
  146. $a = array('1' );
  147. $a = array( '1','2' );
  148. // Wrong usage of array keyword.
  149. $a = Array('1');
  150. $a = ARRAY('x');
  151. // Array indentation error.
  152. $a = array(
  153. 'x' => 'y',
  154. );
  155. // Single line array declaration is too long.
  156. $page_options = array('home' => t('Front Page'), 'all' => t('All Pages'), 'list' => t('List Pages'));
  157. // Item assignment operators must be prefixed and followed by a space
  158. $a = array('one'=>'1');
  159. $a = array('one'=> '1');
  160. $a = array('one' =>'1');
  161. foreach ( $a as $key=>$value) {
  162. }
  163. foreach ( $a as $key =>$value) {
  164. }
  165. foreach ( $a as $key=> $value) {
  166. }
  167. // "as" keyword must be lower case.
  168. foreach ($a AS $key => $value) {
  169. }
  170. // elseif and else must have a newline after the curly braces of the former condition
  171. if(TRUE || TRUE){
  172. $i;
  173. }elseif(TRUE && TRUE){
  174. $i;
  175. }else{
  176. $i;
  177. }
  178. // elseif and else must have a newline after the curly braces of the former condition
  179. if (TRUE || TRUE) {
  180. $i;
  181. }
  182. elseif(TRUE && TRUE){
  183. $i;
  184. }
  185. else{
  186. $i;
  187. }
  188. // If conditions have a space before and after the condition parenthesis
  189. if(TRUE || TRUE) {
  190. $i;
  191. }elseif(TRUE && TRUE) {
  192. $i;
  193. }else {
  194. $i;
  195. }
  196. // else if is not allowed
  197. if (TRUE) {
  198. $i;
  199. }else if (TRUE) {
  200. $i;
  201. }
  202. // White spaces before and after the condition are not allowed.
  203. if ( TRUE) {
  204. $i;
  205. }
  206. elseif (TRUE ) {
  207. $i;
  208. }
  209. // Break has to be intended 2 spaces
  210. switch ($condition) {
  211. case 1:
  212. $i;
  213. break;
  214. case 2:
  215. $i;
  216. break;
  217. }
  218. // Missing Spaces
  219. switch($condition) {
  220. default:
  221. $i;
  222. }
  223. switch($condition){
  224. default:
  225. $i;
  226. }
  227. switch ($condition){
  228. default:
  229. $i;
  230. }
  231. // Missing space after "do"
  232. do{
  233. $i;
  234. } while ($condition);
  235. // Missing space before "while"
  236. do {
  237. $i;
  238. }while ($condition);
  239. // Missing space after "while"
  240. do {
  241. $i;
  242. } while($condition);
  243. // For loop formatting.
  244. // For loop formatting.
  245. for ( $i = 0; $i < 5; $i++) {
  246. }
  247. for ($i = 0 ; $i < 5; $i++) {
  248. }
  249. for ($i = 0;$i < 5; $i++) {
  250. }
  251. for ($i = 0; $i < 5 ; $i++) {
  252. }
  253. for ($i = 0; $i < 5;$i++) {
  254. }
  255. // Control structure keywords must be lower case.
  256. IF (TRUE) {
  257. FoReACH ($a as $key => $value) {
  258. tRY {
  259. $value++;
  260. }
  261. CATCH (Exception $e) {
  262. }
  263. }
  264. }
  265. /**
  266. * Short description
  267. *
  268. * We use doxygen style comments.
  269. * What's sad because eclipse PDT and
  270. * PEAR CodeSniffer base on phpDoc comment style.
  271. * Makes working with drupal not easier :|
  272. *
  273. * @param $field1
  274. * Doxygen style comments
  275. * @param $field2
  276. * Doxygen style comments
  277. * @param $field3
  278. * Doxygen style comments
  279. * @return
  280. * Doxygen style comments
  281. */
  282. function foo($field1, $field2, $field3 = NULL) {
  283. $system["description"] = t("This module inserts funny text into posts randomly.");
  284. /**
  285. * Inline doc blocks are not allowed.
  286. */
  287. return $system[$field];
  288. }
  289. /**
  290. * use a single space between the closing parenthesis and the open bracket
  291. */
  292. function foo() {
  293. }
  294. /**
  295. * use a single space between the closing parenthesis and the open bracket
  296. */
  297. function foo(){
  298. }
  299. // There has to be a space betrween the comma and the next argument
  300. $var = foo($i,$i);
  301. // Multiline function call - all the parameters have to be on the next line
  302. // I don't thing that this rule is strictly followed...
  303. $var = foo($i,
  304. $i,
  305. $i
  306. );
  307. // Multiline function call - closing parenthesis has to be on a own line
  308. $var = foo(
  309. $i,
  310. $i,
  311. $i);
  312. // Only multiline comments with /** should be used to comment classes
  313. // Curly brace has to be on the same line
  314. class Bar
  315. {
  316. // Private properties must not have a prefix
  317. private $_secret = 1;
  318. // Public properties must not have a prefix
  319. protected $_foo = 1;
  320. // Public properties must not have a prefix
  321. public $_bar = 1;
  322. // Public static variables use camelCase.
  323. public static $base_path = NULL;
  324. // "var" keyword must no be used.
  325. var $x = 5;
  326. }
  327. // Comments must have content ;) and there must be a space after the class name
  328. /**
  329. *
  330. */
  331. class FooBar{
  332. }
  333. // When calling class constructors with no arguments, always include parentheses:
  334. $bar = new Bar;
  335. // Check for spaces between classname and opening parenthesis
  336. $bar = new Bar ();
  337. // Check argument formating
  338. $bar = new Bar($i,$i);
  339. // Concatenation - there has to be a space
  340. $i ."test";
  341. $i .'test';
  342. $i .$i;
  343. $i .C_TEST;
  344. $i."test";
  345. $i.'test';
  346. $i.$i;
  347. $i.C_TEST;
  348. $i. "test";
  349. $i. 'test';
  350. $i. $i;
  351. $i. C_TEST;
  352. /**
  353. *
  354. */
  355. Class FunctionTest {
  356. public function _foo() {
  357. }
  358. protected function _bar() {
  359. }
  360. /**
  361. * Asterisks of this comment are wrong. And "private" should no be used.
  362. *
  363. */
  364. private function foobar() {
  365. }
  366. }
  367. interface startsLowerInterface {}
  368. class Uses_UnderScores {}
  369. function _refix() {
  370. }
  371. // Usage of t() - there should be no escaping.
  372. t('She\'s a good person.');
  373. t("This is a \"fancy\" string.");
  374. // Concatenation of strings is bad in t().
  375. t('Your user name: ' . $user_name);
  376. // Empty t() calls are not allowed.
  377. t();
  378. // require_once should be a statement.
  379. require_once('somefile.php');
  380. /*
  381. * Wrong comment style
  382. */
  383. function test1() {
  384. }
  385. /**
  386. *
  387. */
  388. function test2() {
  389. }
  390. /**
  391. * Implementation of hook_menu().
  392. */
  393. function mymodule_menu() {
  394. return array();
  395. }
  396. /**
  397. * Implementation hook_foo_BAR_ID_bar() for some_type_bar().
  398. *
  399. * Extended "Implements" syntax for hooks.
  400. */
  401. function mymodule_foo_some_type_bar() {
  402. }
  403. /**
  404. * Implements hook_test_info().
  405. *
  406. * @param int $param
  407. * Duplicated documentation which should be on the hook definition only.
  408. */
  409. function mymodule_test_info($param) {
  410. }
  411. /**
  412. * Implements hook_test2_info().
  413. *
  414. * @return int
  415. * Duplicated documentation which should be on the hook definition only.
  416. */
  417. function mymodule_test2_info($param) {
  418. }
  419. /**
  420. *
  421. * Extra newline above is not allowed.
  422. */
  423. function test3() {
  424. }
  425. /**
  426. * Extra newlines between function descriptions are not allowed.
  427. *
  428. *
  429. * More description here ...
  430. */
  431. function test4() {
  432. }
  433. /**
  434. * There must be a new line before tags.
  435. * @param int $x
  436. * A number.
  437. */
  438. function test5($x) {
  439. }
  440. /**
  441. * Throws tag must have an Exception name.
  442. *
  443. * @throws
  444. */
  445. function test6() {
  446. }
  447. /**
  448. * Return tag must not be empty.
  449. *
  450. * @return
  451. */
  452. function test7() {
  453. }
  454. /**
  455. * Param tags must come first.
  456. *
  457. * @return array()
  458. *
  459. * @param int $x
  460. * A number.
  461. */
  462. function test8($x) {
  463. }
  464. /**
  465. * Last Param tag must be followed by a new line.
  466. *
  467. * @param int $x
  468. * A number.
  469. * @param int $y
  470. * Another number.
  471. * @return bool
  472. */
  473. function test9($x, $y) {
  474. }
  475. /**
  476. * Only one space before param type and return type.
  477. *
  478. * @param array $x
  479. * Shiny array.
  480. *
  481. * @return int
  482. * Some number.
  483. */
  484. function test10($x) {
  485. }
  486. /**
  487. * Documented params do not match.
  488. *
  489. * @param bool $foobar
  490. * Wrong name.
  491. * @param bool $Y
  492. * Wrong case.
  493. * @param $z
  494. * Missing type.
  495. * @param int
  496. * @param string $a Comment should be on a new line.
  497. * @param string $non_existent
  498. * Parameter does not exist.
  499. *
  500. * @return bool This description should be on the next line.
  501. *
  502. * @see Too much space here
  503. * @see my_function().
  504. * @see foo_bar() and here some not allowed text.
  505. */
  506. function test11($x, $y, $z, $a, $b) {
  507. }
  508. /**
  509. * Referenced variables should not contain the & in the docblock.
  510. *
  511. * @param int &$x
  512. * Refrenced variable.
  513. */
  514. function test12(&$x, &$y) {
  515. }
  516. /**
  517. * Malformed doxygen asterisks.
  518. *@return array
  519. */
  520. function test13() {
  521. }
  522. /**
  523. * There should be no empty line between doc block and function.
  524. */
  525. function test14() {
  526. }
  527. /**
  528. * Return data type documentation must not be the variable name.
  529. *
  530. * @return $foo
  531. * Description bla.
  532. */
  533. function test15() {
  534. $foo = TRUE;
  535. return $foo;
  536. }
  537. /** This line should not be used.
  538. *
  539. */
  540. function test16() {
  541. }
  542. /**
  543. * Invalid data types.
  544. *
  545. * @param type $x
  546. * Description here.
  547. * @param boolean $y
  548. * Description here.
  549. *
  550. * @return unknown_type
  551. * Description here.
  552. */
  553. function test17($x, $y) {
  554. }
  555. /**
  556. * Too much space after the function keyword.
  557. */
  558. function test18() {
  559. }
  560. /**
  561. * Space before opening parenthesis is not allowed.
  562. */
  563. function test19 () {
  564. }
  565. /**
  566. * There should be a space after the comma.
  567. */
  568. function test20($a,$b) {
  569. }
  570. /**
  571. * Some indentation errors.
  572. */
  573. function test21() {
  574. foo();
  575. if (TRUE) {
  576. bar();
  577. }
  578. // Too far left.
  579. $x = 1 + 1;
  580. }
  581. /**
  582. * Function parameters must be all lower case.
  583. */
  584. function test22function($Fullname) {
  585. }
  586. // Test string concatenation.
  587. $x = 'This string is to short to be' . 'concatenated';
  588. // Object operator spacing is not allowed.
  589. $z = $foo-> x;
  590. $z = $foo -> x;
  591. $z = $foo ->x;
  592. // strings in l() should be translated.
  593. $x = l('Link text', '<front>');
  594. $x = 'Some markup text<br>';
  595. $ip = $_SERVER['REMOTE_ADDR'];
  596. // Inline if statements with ? and wrong spacing.
  597. $x = $y == $z? 23 : 42;
  598. $x = $y == $z ?23 : 42;
  599. $x = $y == $z ? 23 : 42;
  600. $x = $y == $z ? 23 : 42;
  601. // Inline if statements with : and wrong spacing.
  602. $x = $y == $z ? 23: 42;
  603. $x = $y == $z ? 23 :42;
  604. $x = $y == $z ? 23 : 42;
  605. $x = $y == $z ? 23 : 42;
  606. $x = $y == $z ? 23 :'foo';
  607. $x = $y == $z ? : 42;
  608. // Watchdog messages should not use t().
  609. watchdog('mymodule', t('Log message here.'));
  610. // Inline control structures are bad.
  611. if ($x == $y) $x = 42;
  612. // isset() must be all lower case.
  613. if (isSet($x)) {
  614. }
  615. /**
  616. * Comment should end with '*', not '**' before the slash.
  617. **/
  618. function test23() {
  619. }
  620. /**
  621. * Comment has 2 spaces at the beginning.
  622. */
  623. function test24() {
  624. }
  625. /**
  626. * Wrong tag indentation.
  627. *
  628. * @return bool
  629. * Description.
  630. */
  631. function test25() {
  632. return TRUE;
  633. }
  634. class FooBar2 {
  635. /**
  636. * Function body should be on a line by itself, same for the closing brace.
  637. */
  638. public function test26() {print 'x';}
  639. }
  640. // Security issue: http://drupal.org/node/750148
  641. preg_match('/.+/e', 'subject');
  642. preg_match('/.+/iemesuexADSUeXJ', 'subject');
  643. preg_filter('/.+/imsuexADSUXJ', 'replacement', 'subject');
  644. preg_replace('/.+/imsuxADSUeXJ', 'replacement', 'subject');
  645. // Use a not so common delimiter.
  646. preg_match('@.+@e', 'subject');
  647. preg_match('@.+@iemesuexADSUeXJ', 'subject');
  648. preg_filter('@.+@imsuexADSUXJ', 'replacement', 'subject');
  649. preg_replace('@.+@imsuxADSUeXJ', 'replacement', 'subject');
  650. interface notA_GoodInterfaceName {
  651. }
  652. // The catch statement must be on a new line.
  653. try {
  654. do_something();
  655. } catch (Exception $e) {
  656. scream();
  657. }
  658. // Function name aliases should not be used.
  659. $x = join($glue, $pieces);
  660. // Empty strings passed to t() are wrong.
  661. t('');
  662. t("");
  663. ?>