~ubuntu-branches/ubuntu/trusty/php-codesniffer/trusty

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-07-12 15:16:25 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130712151625-4autdc0twzbueha9
Tags: 1.5.0~rc2-1
* New upstream release.
* Refreshed patch.
* Standards-Version is now 3.9.4.
* Canonical VCS URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Verifies that class members are spaced correctly.
 
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
 
 
16
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
 
17
    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
 
18
}
 
19
 
 
20
/**
 
21
 * Verifies that class members are spaced correctly.
 
22
 *
 
23
 * @category  PHP
 
24
 * @package   PHP_CodeSniffer
 
25
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
26
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
27
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
 
28
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
29
 * @version   Release: 1.5.0RC2
 
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
31
 */
 
32
class Squiz_Sniffs_WhiteSpace_MemberVarSpacingSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
 
33
{
 
34
 
 
35
 
 
36
    /**
 
37
     * Processes the function tokens within the class.
 
38
     *
 
39
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
 
40
     * @param int                  $stackPtr  The position where the token was found.
 
41
     *
 
42
     * @return void
 
43
     */
 
44
    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
45
    {
 
46
        $tokens = $phpcsFile->getTokens();
 
47
 
 
48
        // There needs to be 1 blank line before the var, not counting comments.
 
49
        $prevLineToken = null;
 
50
        for ($i = ($stackPtr - 1); $i > 0; $i--) {
 
51
            if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) {
 
52
                // Skip comments.
 
53
                continue;
 
54
            } else if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
 
55
                // Not the end of the line.
 
56
                continue;
 
57
            } else {
 
58
                // If this is a WHITESPACE token, and the token right before
 
59
                // it is a DOC_COMMENT, then it is just the newline after the
 
60
                // member var's comment, and can be skipped.
 
61
                if ($tokens[$i]['code'] === T_WHITESPACE && in_array($tokens[($i - 1)]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) {
 
62
                    continue;
 
63
                }
 
64
 
 
65
                $prevLineToken = $i;
 
66
                break;
 
67
            }
 
68
        }
 
69
 
 
70
        if (is_null($prevLineToken) === true) {
 
71
            // Never found the previous line, which means
 
72
            // there are 0 blank lines before the member var.
 
73
            $foundLines = 0;
 
74
        } else {
 
75
            $prevContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_DOC_COMMENT), $prevLineToken, null, true);
 
76
            $foundLines  = ($tokens[$prevLineToken]['line'] - $tokens[$prevContent]['line']);
 
77
        }//end if
 
78
 
 
79
        if ($foundLines !== 1) {
 
80
            $error = 'Expected 1 blank line before member var; %s found';
 
81
            $data  = array($foundLines);
 
82
            $phpcsFile->addError($error, $stackPtr, 'After', $data);
 
83
        }
 
84
 
 
85
    }//end processMemberVar()
 
86
 
 
87
 
 
88
    /**
 
89
     * Processes normal variables.
 
90
     *
 
91
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
 
92
     * @param int                  $stackPtr  The position where the token was found.
 
93
     *
 
94
     * @return void
 
95
     */
 
96
    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
97
    {
 
98
        // We don't care about normal variables.
 
99
 
 
100
    }//end processVariable()
 
101
 
 
102
 
 
103
    /**
 
104
     * Processes variables in double quoted strings.
 
105
     *
 
106
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
 
107
     * @param int                  $stackPtr  The position where the token was found.
 
108
     *
 
109
     * @return void
 
110
     */
 
111
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
112
    {
 
113
        // We don't care about normal variables.
 
114
 
 
115
    }//end processVariableInString()
 
116
 
 
117
 
 
118
}//end class
 
119
 
 
120
?>