1 | <?php |
||
32 | class CodingStandard_Sniffs_Classes_PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff |
||
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 | protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
45 | { |
||
46 | $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 | $find = PHP_CodeSniffer_Tokens::$scopeModifiers; |
||
52 | $find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON)); |
||
53 | $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1)); |
||
54 | if ($tokens[$prev]['code'] === T_VARIABLE) { |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | if ($tokens[$prev]['code'] === T_VAR) { |
||
59 | $error = 'The var keyword must not be used to declare a property'; |
||
60 | $phpcsFile->addError($error, $stackPtr, 'VarUsed'); |
||
61 | } |
||
62 | |||
63 | $next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), ($stackPtr + 1)); |
||
64 | if ($tokens[$next]['code'] === T_VARIABLE) { |
||
65 | $error = 'There must not be more than one property declared per statement'; |
||
66 | $phpcsFile->addError($error, $stackPtr, 'Multiple'); |
||
67 | } |
||
68 | |||
69 | $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr); |
||
70 | if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) { |
||
71 | $error = 'Visibility must be declared on property "%s"'; |
||
72 | $data = array($tokens[$stackPtr]['content']); |
||
73 | $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data); |
||
74 | } |
||
75 | |||
76 | }//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) |
||
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) |
||
109 | |||
110 | |||
111 | }//end class |
||
112 |
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.