~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.5/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Squiz_Sniffs_ControlStructures_InlineControlStructureSniff.
 
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-2014 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
/**
 
17
 * Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff.
 
18
 *
 
19
 * Tests the spacing of shorthand IF statements.
 
20
 *
 
21
 * @category  PHP
 
22
 * @package   PHP_CodeSniffer
 
23
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
24
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
25
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
26
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
27
 * @version   Release: 1.5.5
 
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
29
 */
 
30
class Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff implements PHP_CodeSniffer_Sniff
 
31
{
 
32
 
 
33
 
 
34
    /**
 
35
     * Returns an array of tokens this test wants to listen for.
 
36
     *
 
37
     * @return array
 
38
     */
 
39
    public function register()
 
40
    {
 
41
        return array(T_INLINE_THEN);
 
42
 
 
43
    }//end register()
 
44
 
 
45
 
 
46
    /**
 
47
     * Processes this sniff, when one of its tokens is encountered.
 
48
     *
 
49
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
50
     * @param int                  $stackPtr  The position of the current token in the
 
51
     *                                        stack passed in $tokens.
 
52
     *
 
53
     * @return void
 
54
     */
 
55
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
56
    {
 
57
        $tokens = $phpcsFile->getTokens();
 
58
 
 
59
        // Find the opening bracket of the inline IF.
 
60
        $i = 0;
 
61
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
 
62
            $parens = $tokens[$stackPtr]['nested_parenthesis'];
 
63
            $i      = array_pop($parens);
 
64
            if (isset($tokens[$i]['parenthesis_owner']) === true) {
 
65
                // The parenthesis are owned by a token like an array or
 
66
                // function, so are not just used for grouping.
 
67
                $i = 0;
 
68
            }
 
69
        }
 
70
 
 
71
        if ($i <= 0) {
 
72
            // Could not find the beginning of the statement. Probably not
 
73
            // wrapped with brackets, so assume it ends with a
 
74
            // semicolon (end of statement) or comma (end of array value).
 
75
            $else         = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1));
 
76
            $statementEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), ($else + 1));
 
77
        } else {
 
78
            $statementEnd = $tokens[$i]['parenthesis_closer'];
 
79
        }
 
80
 
 
81
        // Make sure it's all on the same line.
 
82
        if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
 
83
            $error = 'Inline shorthand IF statement must be declared on a single line';
 
84
            $phpcsFile->addError($error, $stackPtr, 'NotSingleLine');
 
85
            return;
 
86
        }
 
87
 
 
88
        // Make sure there are spaces around the question mark.
 
89
        $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
 
90
        $contentAfter  = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
 
91
        if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
 
92
            $error = 'Inline shorthand IF statement requires brackets around comparison';
 
93
            $phpcsFile->addError($error, $stackPtr, 'NoBrackets');
 
94
            return;
 
95
        }
 
96
 
 
97
        $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
 
98
        if ($spaceBefore !== 1) {
 
99
            $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found';
 
100
            $data  = array($spaceBefore);
 
101
            $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data);
 
102
        }
 
103
 
 
104
        $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
 
105
        if ($spaceAfter !== 1) {
 
106
            $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
 
107
            $data  = array($spaceAfter);
 
108
            $phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
 
109
        }
 
110
 
 
111
        // Make sure the ELSE has the correct spacing.
 
112
        $inlineElse    = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
 
113
        $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
 
114
        $contentAfter  = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
 
115
 
 
116
        $spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
 
117
        if ($spaceBefore !== 1) {
 
118
            $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
 
119
            $data  = array($spaceBefore);
 
120
            $phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data);
 
121
        }
 
122
 
 
123
        $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
 
124
        if ($spaceAfter !== 1) {
 
125
            $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
 
126
            $data  = array($spaceAfter);
 
127
            $phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data);
 
128
        }
 
129
 
 
130
    }//end process()
 
131
 
 
132
 
 
133
}//end class
 
134
 
 
135
 
 
136
?>