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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.4/CodeSniffer/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.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
 
 * Verifies that properties are declared correctly.
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
 
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
16
 
    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
17
 
}
18
 
 
19
 
/**
20
 
 * Verifies that properties are declared correctly.
21
 
 *
22
 
 * @category  PHP
23
 
 * @package   PHP_CodeSniffer
24
 
 * @author    Greg Sherwood <gsherwood@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.4
28
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
29
 
 */
30
 
class PSR2_Sniffs_Classes_PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
31
 
{
32
 
 
33
 
 
34
 
    /**
35
 
     * Processes the function tokens within the class.
36
 
     *
37
 
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
38
 
     * @param int                  $stackPtr  The position where the token was found.
39
 
     *
40
 
     * @return void
41
 
     */
42
 
    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
43
 
    {
44
 
        $tokens = $phpcsFile->getTokens();
45
 
 
46
 
        if ($tokens[$stackPtr]['content'][1] === '_') {
47
 
            $error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
48
 
            $data  = array($tokens[$stackPtr]['content']);
49
 
            $phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
50
 
        }
51
 
 
52
 
        // Detect multiple properties defined at the same time. Throw an error
53
 
        // for this, but also only process the first property in the list so we don't
54
 
        // repeat errors.
55
 
        $find = PHP_CodeSniffer_Tokens::$scopeModifiers;
56
 
        $find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
57
 
        $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
58
 
        if ($tokens[$prev]['code'] === T_VARIABLE) {
59
 
            return;
60
 
        }
61
 
 
62
 
        if ($tokens[$prev]['code'] === T_VAR) {
63
 
            $error = 'The var keyword must not be used to declare a property';
64
 
            $phpcsFile->addError($error, $stackPtr, 'VarUsed');
65
 
        }
66
 
 
67
 
        $next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), ($stackPtr + 1));
68
 
        if ($tokens[$next]['code'] === T_VARIABLE) {
69
 
            $error = 'There must not be more than one property declared per statement';
70
 
            $phpcsFile->addError($error, $stackPtr, 'Multiple');
71
 
        }
72
 
 
73
 
        $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
74
 
        if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
75
 
            $error = 'Visibility must be declared on property "%s"';
76
 
            $data  = array($tokens[$stackPtr]['content']);
77
 
            $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
78
 
        }
79
 
 
80
 
    }//end processMemberVar()
81
 
 
82
 
 
83
 
    /**
84
 
     * Processes normal variables.
85
 
     *
86
 
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
87
 
     * @param int                  $stackPtr  The position where the token was found.
88
 
     *
89
 
     * @return void
90
 
     */
91
 
    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
92
 
    {
93
 
        // We don't care about normal variables.
94
 
 
95
 
    }//end processVariable()
96
 
 
97
 
 
98
 
    /**
99
 
     * Processes variables in double quoted strings.
100
 
     *
101
 
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
102
 
     * @param int                  $stackPtr  The position where the token was found.
103
 
     *
104
 
     * @return void
105
 
     */
106
 
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
107
 
    {
108
 
        // We don't care about normal variables.
109
 
 
110
 
    }//end processVariableInString()
111
 
 
112
 
 
113
 
}//end class
114
 
 
115
 
?>