~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/Functions/MultiLineFunctionDeclarationSniff.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
 * Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff.
 
4
 *
 
5
 * PHP version 5
 
6
 *
 
7
 * @category  PHP
 
8
 * @package   PHP_CodeSniffer
 
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
10
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
 
11
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
13
 */
 
14
 
 
15
if (class_exists('PEAR_Sniffs_Functions_FunctionDeclarationSniff', true) === false) {
 
16
    $error = 'Class PEAR_Sniffs_Functions_FunctionDeclarationSniff not found';
 
17
    throw new PHP_CodeSniffer_Exception($error);
 
18
}
 
19
 
 
20
/**
 
21
 * Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff.
 
22
 *
 
23
 * Ensure single and multi-line function declarations are defined correctly.
 
24
 *
 
25
 * @category  PHP
 
26
 * @package   PHP_CodeSniffer
 
27
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
28
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
 
29
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
30
 * @version   Release: 1.5.0RC2
 
31
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
32
 */
 
33
class Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff extends PEAR_Sniffs_Functions_FunctionDeclarationSniff
 
34
{
 
35
 
 
36
 
 
37
    /**
 
38
     * Processes multi-line declarations.
 
39
     *
 
40
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
41
     * @param int                  $stackPtr  The position of the current token
 
42
     *                                        in the stack passed in $tokens.
 
43
     * @param array                $tokens    The stack of tokens that make up
 
44
     *                                        the file.
 
45
     *
 
46
     * @return void
 
47
     */
 
48
    public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
 
49
    {
 
50
        // We do everything the parent sniff does, and a bit more.
 
51
        parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
 
52
 
 
53
        $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
 
54
        $this->processBracket($phpcsFile, $openBracket, $tokens, 'function');
 
55
 
 
56
        if ($tokens[$stackPtr]['code'] !== T_CLOSURE) {
 
57
            return;
 
58
        }
 
59
 
 
60
        $use = $phpcsFile->findNext(T_USE, ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
 
61
        if ($use === false) {
 
62
            return;
 
63
        }
 
64
 
 
65
        $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
 
66
        $this->processBracket($phpcsFile, $openBracket, $tokens, 'use');
 
67
 
 
68
        // Also check spacing.
 
69
        if ($tokens[($use - 1)]['code'] === T_WHITESPACE) {
 
70
            $gap = strlen($tokens[($use - 1)]['content']);
 
71
        } else {
 
72
            $gap = 0;
 
73
        }
 
74
 
 
75
    }//end processMultiLineDeclaration()
 
76
 
 
77
 
 
78
    /**
 
79
     * Processes the contents of a single set of brackets.
 
80
     *
 
81
     * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
 
82
     * @param int                  $openBracket The position of the open bracket
 
83
     *                                          in the stack passed in $tokens.
 
84
     * @param array                $tokens      The stack of tokens that make up
 
85
     *                                          the file.
 
86
     * @param string               $type        The type of the token the brackets
 
87
     *                                          belong to (function or use).
 
88
     *
 
89
     * @return void
 
90
     */
 
91
    public function processBracket(PHP_CodeSniffer_File $phpcsFile, $openBracket, $tokens, $type='function')
 
92
    {
 
93
        $errorPrefix = '';
 
94
        if ($type === 'use') {
 
95
            $errorPrefix = 'Use';
 
96
        }
 
97
 
 
98
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
 
99
 
 
100
        // The open bracket should be the last thing on the line.
 
101
        if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
 
102
            $next = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
 
103
            if ($tokens[$next]['line'] !== ($tokens[$openBracket]['line'] + 1)) {
 
104
                $error = 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
 
105
                $phpcsFile->addError($error, $next, $errorPrefix.'FirstParamSpacing');
 
106
            }
 
107
        }
 
108
 
 
109
        // Each line between the brackets should contain a single parameter.
 
110
        $lastCommaLine = null;
 
111
        for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
 
112
            // Skip brackets, like arrays, as they can contain commas.
 
113
            if (isset($tokens[$i]['parenthesis_opener']) === true) {
 
114
                $i = $tokens[$i]['parenthesis_closer'];
 
115
                continue;
 
116
            }
 
117
 
 
118
            if ($tokens[$i]['code'] === T_COMMA) {
 
119
                if ($lastCommaLine !== null && $lastCommaLine === $tokens[$i]['line']) {
 
120
                    $error = 'Multi-line '.$type.' declarations must define one parameter per line';
 
121
                    $phpcsFile->addError($error, $i, $errorPrefix.'OneParamPerLine');
 
122
                } else {
 
123
                    // Comma must be the last thing on the line.
 
124
                    $next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
 
125
                    if ($tokens[$next]['line'] !== ($tokens[$i]['line'] + 1)) {
 
126
                        $error = 'Commas in multi-line '.$type.' declarations must be the last content on a line';
 
127
                        $phpcsFile->addError($error, $next, $errorPrefix.'ContentAfterComma');
 
128
                    }
 
129
                }
 
130
 
 
131
                $lastCommaLine = $tokens[$i]['line'];
 
132
            }
 
133
        }
 
134
 
 
135
    }//end processBracket()
 
136
 
 
137
 
 
138
}//end class
 
139
 
 
140
?>