Skip to content
This repository
Browse code

Add a "White space before a control structure" sniff

  • Loading branch information...
commit b679a14a2da176849491d5f11507878c6a025d78 1 parent da480c6
Nikolai Plath authored October 23, 2012
103  Sniffs/ControlStructures/WhiteSpaceBeforeSniff.php
... ...
@@ -0,0 +1,103 @@
  1
+<?php
  2
+/**
  3
+ * Joomla_Sniffs_ControlStructures_WhiteSpaceBeforeSniff.
  4
+ *
  5
+ * PHP version 5
  6
+ *
  7
+ * @package     PHP_CodeSniffer
  8
+ * @subpackage  PHP
  9
+ * @author      Nikolai Plath <der.el.kuku@gmail.com>
  10
+ * @copyright   2012 OSM
  11
+ * @license     http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  12
+ * @link        http://pear.php.net/package/PHP_CodeSniffer
  13
+ */
  14
+
  15
+/**
  16
+ * White space before a control structure.
  17
+ *
  18
+ * Checks that there is an empty line before a control structure to improve readability.
  19
+ * This only applies if the line before the structure contains code.
  20
+ * Comments or curly braces are considered valid.
  21
+ *
  22
+ * <b class="bad">Bad:</b>
  23
+ * $foo = $bar;
  24
+ * if(condition)
  25
+ * {
  26
+ *     // blah
  27
+ * }
  28
+ *
  29
+ * <b class="good">Good:</b>
  30
+ * $foo = $bar;
  31
+ *
  32
+ * if(condition)
  33
+ * {
  34
+ *     // blah
  35
+ * }
  36
+ *
  37
+ * This rule applies for the structures:
  38
+ * <b>if, for, foreach, while, switch, try and return</b>
  39
+ *
  40
+ * @version    Release: 1.3.0RC1
  41
+ * @category   PHP
  42
+ * @package    PHP_CodeSniffer
  43
+ * @author     Greg Sherwood <gsherwood@squiz.net>
  44
+ * @author     Marc McIntyre <mmcintyre@squiz.net>
  45
+ * @copyright  2006 Squiz Pty Ltd (ABN 77 084 670 600)
  46
+ * @license    http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  47
+ * @link       http://pear.php.net/package/PHP_CodeSniffer
  48
+ *
  49
+ * @since      1.0
  50
+ */
  51
+class Joomla_Sniffs_ControlStructures_WhiteSpaceBeforeSniff implements PHP_CodeSniffer_Sniff
  52
+{
  53
+	/**
  54
+	 * Registers the tokens that this sniff wants to listen for.
  55
+	 *
  56
+	 * @return array
  57
+	 */
  58
+	public function register()
  59
+	{
  60
+		return array(
  61
+			T_IF
  62
+		, T_FOR
  63
+		, T_FOREACH
  64
+		, T_SWITCH
  65
+		, T_TRY
  66
+		, T_WHILE
  67
+		, T_DO
  68
+		, T_RETURN
  69
+		);
  70
+	}
  71
+
  72
+	/**
  73
+	 * Processes this test, when one of its tokens is encountered.
  74
+	 *
  75
+	 * @param   PHP_CodeSniffer_File  $phpcsFile  The file being scanned.
  76
+	 * @param   integer               $stackPtr   The position of the current token in the stack passed in $tokens.
  77
+	 *
  78
+	 * @return void
  79
+	 */
  80
+	public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  81
+	{
  82
+		$tokens = $phpcsFile->getTokens();
  83
+
  84
+		if (isset($tokens[$stackPtr]['scope_opener']) === false
  85
+			&& $tokens[$stackPtr]['code'] != T_RETURN)
  86
+		{
  87
+			return;
  88
+		}
  89
+
  90
+		$previousSemicolon = $phpcsFile->findPrevious(array(T_SEMICOLON), ($stackPtr - 1), null, false);
  91
+
  92
+		if ($tokens[$stackPtr]['line'] - 1 == $tokens[$previousSemicolon]['line'])
  93
+		{
  94
+			$error = sprintf('Please consider an empty line before the %s statement;',
  95
+				$tokens[$stackPtr]['content']
  96
+			);
  97
+
  98
+			$phpcsFile->addError($error, $stackPtr, 'SpaceBefore');
  99
+
  100
+			return;
  101
+		}
  102
+	}
  103
+}

0 notes on commit b679a14

Please sign in to comment.
Something went wrong with that request. Please try again.