1<?php
2/**
3 * PEAR_Sniffs_Functions_FunctionDeclarationSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2014 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/**
16 * PEAR_Sniffs_Formatting_MultiLineAssignmentSniff.
17 *
18 * If an assignment goes over two lines, ensure the equal sign is indented.
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version   Release: @package_version@
26 * @link      http://pear.php.net/package/PHP_CodeSniffer
27 */
28class PEAR_Sniffs_Formatting_MultiLineAssignmentSniff implements PHP_CodeSniffer_Sniff
29{
30
31    /**
32     * The number of spaces code should be indented.
33     *
34     * @var int
35     */
36    public $indent = 4;
37
38
39    /**
40     * Returns an array of tokens this test wants to listen for.
41     *
42     * @return array
43     */
44    public function register()
45    {
46        return array(T_EQUAL);
47
48    }//end register()
49
50
51    /**
52     * Processes this test, when one of its tokens is encountered.
53     *
54     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
55     * @param int                  $stackPtr  The position of the current token
56     *                                        in the stack passed in $tokens.
57     *
58     * @return void
59     */
60    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61    {
62        $tokens = $phpcsFile->getTokens();
63
64        // Equal sign can't be the last thing on the line.
65        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
66        if ($next === false) {
67            // Bad assignment.
68            return;
69        }
70
71        if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
72            $error = 'Multi-line assignments must have the equal sign on the second line';
73            $phpcsFile->addError($error, $stackPtr, 'EqualSignLine');
74            return;
75        }
76
77        // Make sure it is the first thing on the line, otherwise we ignore it.
78        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), false, true);
79        if ($prev === false) {
80            // Bad assignment.
81            return;
82        }
83
84        if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
85            return;
86        }
87
88        // Find the required indent based on the ident of the previous line.
89        $assignmentIndent = 0;
90        $prevLine         = $tokens[$prev]['line'];
91        for ($i = ($prev - 1); $i >= 0; $i--) {
92            if ($tokens[$i]['line'] !== $prevLine) {
93                $i++;
94                break;
95            }
96        }
97
98        if ($tokens[$i]['code'] === T_WHITESPACE) {
99            $assignmentIndent = strlen($tokens[$i]['content']);
100        }
101
102        // Find the actual indent.
103        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1));
104
105        $expectedIndent = ($assignmentIndent + $this->indent);
106        $foundIndent    = strlen($tokens[$prev]['content']);
107        if ($foundIndent !== $expectedIndent) {
108            $error = 'Multi-line assignment not indented correctly; expected %s spaces but found %s';
109            $data  = array(
110                      $expectedIndent,
111                      $foundIndent,
112                     );
113            $phpcsFile->addError($error, $stackPtr, 'Indent', $data);
114        }
115
116    }//end process()
117
118
119}//end class
120