Completed
Push — master ( b79486...df4d3f )
by Alexander
02:06
created

PropertyDeclarationSniff::processVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * CodingStandard_Sniffs_Classes_PropertyDeclarationSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Greg Sherwood <[email protected]>
10
 * @author   Alexander Obuhovich <[email protected]>
11
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
12
 * @link     https://github.com/aik099/CodingStandard
13
 */
14
15
// @codeCoverageIgnoreStart
16
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
17
    $error = 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found';
18
    throw new PHP_CodeSniffer_Exception($error);
19
}
20
// @codeCoverageIgnoreEnd
21
22
/**
23
 * Verifies that properties are declared correctly.
24
 *
25
 * @category PHP
26
 * @package  PHP_CodeSniffer
27
 * @author   Greg Sherwood <[email protected]>
28
 * @author   Alexander Obuhovich <[email protected]>
29
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
30
 * @link     https://github.com/aik099/CodingStandard
31
 */
32
class CodingStandard_Sniffs_Classes_PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
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 1
    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
45
    {
46 1
        $tokens = $phpcsFile->getTokens();
47
48
        // Detect multiple properties defined at the same time. Throw an error
49
        // for this, but also only process the first property in the list so we don't
50
        // repeat errors.
51 1
        $find = PHP_CodeSniffer_Tokens::$scopeModifiers;
52 1
        $find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
53 1
        $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
54 1
        if ($tokens[$prev]['code'] === T_VARIABLE) {
55 1
            return;
56
        }
57
58 1
        if ($tokens[$prev]['code'] === T_VAR) {
59 1
            $error = 'The var keyword must not be used to declare a property';
60 1
            $phpcsFile->addError($error, $stackPtr, 'VarUsed');
61 1
        }
62
63 1
        $next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), ($stackPtr + 1));
64 1
        if ($tokens[$next]['code'] === T_VARIABLE) {
65 1
            $error = 'There must not be more than one property declared per statement';
66 1
            $phpcsFile->addError($error, $stackPtr, 'Multiple');
67 1
        }
68
69 1
        $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
70 1
        if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
71 1
            $error = 'Visibility must be declared on property "%s"';
72 1
            $data  = array($tokens[$stackPtr]['content']);
73 1
            $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
74 1
        }
75
76 1
    }//end processMemberVar()
77
78
79
    /**
80
     * Processes normal variables.
81
     *
82
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
83
     * @param int                  $stackPtr  The position where the token was found.
84
     *
85
     * @return void
86
     * @codeCoverageIgnore
87
     */
88
    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
89
    {
90
        // We don't care about normal variables.
91
92
    }//end processVariable()
93
94
95
    /**
96
     * Processes variables in double quoted strings.
97
     *
98
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
99
     * @param int                  $stackPtr  The position where the token was found.
100
     *
101
     * @return void
102
     * @codeCoverageIgnore
103
     */
104
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
105
    {
106
        // We don't care about normal variables.
107
108
    }//end processVariableInString()
109
110
111
}//end class
112