Skip to content

Commit a81e341

Browse files
committed
Fixed bug #2615 : Constant visibility false positive on non-class constants
1 parent 0afebf1 commit a81e341

File tree

5 files changed

+14
-171
lines changed

5 files changed

+14
-171
lines changed

package.xml

Lines changed: 3 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -17,181 +17,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
1717
<date>2019-09-27</date>
1818
<time>08:24:00</time>
1919
<version>
20-
<release>3.5.0</release>
21-
<api>3.5.0</api>
20+
<release>3.5.1</release>
21+
<api>3.5.1</api>
2222
</version>
2323
<stability>
2424
<release>stable</release>
2525
<api>stable</api>
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29-
- The included PSR12 standard is now complete and ready to use
30-
-- Check your code using PSR-12 by running PHPCS with --standard=PSR12
31-
- Added support for PHP 7.4 typed properties
32-
-- The nullable operator is now tokenized as T_NULLABLE inside property types, as it is elsewhere
33-
-- To get the type of a member var, use the File::getMemberProperties() method, which now contains a "type" array index
34-
--- This contains the type of the member var, or a blank string if not specified
35-
--- If the type is nullable, the return type will contain the leading ?
36-
--- If a type is specified, the position of the first token in the type will be set in a "type_token" array index
37-
--- If a type is specified, the position of the last token in the type will be set in a "type_end_token" array index
38-
--- If the type is nullable, a "nullable_type" array index will also be set to TRUE
39-
--- If the type contains namespace information, it will be cleaned of whitespace and comments in the return value
40-
- The PSR1 standard now correctly bans alternate PHP tags
41-
-- Previously, it only banned short open tags and not the pre-7.0 alternate tags
42-
- Added support for only checking files that have been locally staged in a git repo
43-
-- Use --filter=gitstaged to check these files
44-
-- You still need to give PHPCS a list of files or directories in which to apply the filter
45-
-- Thanks to Juliette Reinders Folmer for the contribution
46-
- JSON reports now end with a newline character
47-
- The phpcs.xsd schema now validates phpcs-only and phpcbf-only attributes correctly
48-
-- Thanks to Juliette Reinders Folmer for the patch
49-
- The tokenizer now correctly identifies inline control structures in more cases
50-
- All helper methods inside the File class now throw RuntimeException instead of TokenizerException
51-
-- Some tokenizer methods were also throwing RuntimeExpection but now correctly throw TokenizerException
52-
-- Thanks to Juliette Reinders Folmer for the patch
53-
- The File::getMethodParameters() method now returns more information, and supports closure USE groups
54-
-- If a type hint is specified, the position of the last token in the hint will be set in a "type_hint_end_token" array index
55-
-- If a default is specified, the position of the first token in the default value will be set in a "default_token" array index
56-
-- If a default is specified, the position of the equals sign will be set in a "default_equal_token" array index
57-
-- If the param is not the last, the position of the comma will be set in a "comma_token" array index
58-
-- If the param is passed by reference, the position of the reference operator will be set in a "reference_token" array index
59-
-- If the param is variable length, the position of the variadic operator will be set in a "variadic_token" array index
60-
- The T_LIST token and it's opening and closing parentheses now contain references to each other in the tokens array
61-
-- Uses the same parenthesis_opener/closer/owner indexes as other tokens
62-
-- Thanks to Juliette Reinders Folmer for the patch
63-
- The T_ANON_CLASS token and it's opening and closing parentheses now contain references to each other in the tokens array
64-
-- Uses the same parenthesis_opener/closer/owner indexes as other tokens
65-
-- Only applicable if the anon class is passing arguments to the constructor
66-
-- Thanks to Juliette Reinders Folmer for the patch
67-
- The PHP 7.4 T_BAD_CHARACTER token has been made available for older versions
68-
-- Allows you to safely look for this token, but it will not appear unless checking with PHP 7.4+
69-
- Metrics are now available for Squiz.WhiteSpace.FunctionSpacing
70-
-- Use the "info" report to see blank lines before/after functions
71-
-- Thanks to Juliette Reinders Folmer for the patch
72-
- Metrics are now available for Squiz.WhiteSpace.MemberVarSpacing
73-
-- Use the "info" report to see blank lines before member vars
74-
-- Thanks to Juliette Reinders Folmer for the patch
75-
- Added Generic.ControlStructures.DisallowYodaConditions sniff
76-
-- Ban the use of Yoda conditions
77-
-- Thanks to Mponos George for the contribution
78-
- Added Generic.PHP.RequireStrictTypes sniff
79-
-- Enforce the use of a strict types declaration in PHP files
80-
- Added Generic.WhiteSpace.SpreadOperatorSpacingAfter sniff
81-
-- Checks whitespace between the spread operator and the variable/function call it applies to
82-
-- Thanks to Juliette Reinders Folmer for the contribution
83-
- Added PSR12.Classes.AnonClassDeclaration sniff
84-
-- Enforces the formatting of anonymous classes
85-
- Added PSR12.Classes.ClosingBrace sniff
86-
-- Enforces that closing braces of classes/interfaces/traits/functions are not followed by a comment or statement
87-
- Added PSR12.ControlStructures.BooleanOperatorPlacement sniff
88-
-- Enforces that boolean operators between conditions are consistently at the start or end of the line
89-
- Added PSR12.ControlStructures.ControlStructureSpacing sniff
90-
-- Enforces that spacing and indents are correct inside control structure parenthesis
91-
- Added PSR12.Files.DeclareStatement sniff
92-
-- Enforces the formatting of declare statements within a file
93-
- Added PSR12.Files.FileHeader sniff
94-
-- Enforces the order and formatting of file header blocks
95-
- Added PSR12.Files.ImportStatement sniff
96-
-- Enforces the formatting of import statements within a file
97-
- Added PSR12.Files.OpenTag sniff
98-
-- Enforces that the open tag is on a line by itself when used at the start of a php-only file
99-
- Added PSR12.Functions.ReturnTypeDeclaration sniff
100-
-- Enforces the formatting of return type declarations in functions and closures
101-
- Added PSR12.Properties.ConstantVisibility sniff
102-
-- Enforces that constants must have their visibility defined
103-
-- Uses a warning instead of an error due to this conditionally requiring the project to support PHP 7.1+
104-
- Added PSR12.Traits.UseDeclaration sniff
105-
-- Enforces the formatting of trait import statements within a class
106-
- Generic.Files.LineLength ignoreComments property now ignores comments at the end of a line
107-
-- Previously, this property was incorrectly causing the sniff to ignore any line that ended with a comment
108-
-- Now, the trailing comment is not included in the line length, but the rest of the line is still checked
109-
- Generic.Files.LineLength now only ignores unwrappable comments when the comment is on a line by itself
110-
-- Previously, a short unwrappable comment at the end of the line would have the sniff ignore the entire line
111-
- Generic.Functions.FunctionCallArgumentSpacing no longer checks spacing around assignment operators inside function calls
112-
-- Use the Squiz.WhiteSpace.OperatorSpacing sniff to enforce spacing around assignment operators
113-
--- Note that this sniff checks spacing around all assignment operators, not just inside function calls
114-
-- The Generic.Functions.FunctionCallArgumentSpacing.NoSpaceBeforeEquals error has been removed
115-
--- use Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore instead
116-
-- The Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterEquals error has been removed
117-
--- use Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter instead
118-
-- This also changes the PEAR/PSR2/PSR12 standards so they no longer check assignment operators inside function calls
119-
--- They were previously checking these operators when they should not have
120-
-- Thanks to Juliette Reinders Folmer for the patch
121-
- Generic.WhiteSpace.ScopeIndent no longer performs exact indents checking for chained method calls
122-
-- Other sniffs can be used to enforce chained method call indent rules
123-
-- Thanks to Pieter Frenssen for the patch
124-
- PEAR.WhiteSpace.ObjectOperatorIndent now supports multi-level chained statements
125-
-- When enabled, chained calls must be indented 1 level more or less than the previous line
126-
-- Set the new "multilevel" setting to TRUE in a ruleset.xml file to enable this behaviour
127-
-- Thanks to Marcos Passos for the patch
128-
- PSR2.ControlStructures.ControlStructureSpacing now allows whitespace after the opening parenthesis if followed by a comment
129-
-- Thanks to Michał Bundyra for the patch
130-
- PSR2.Classes.PropertyDeclaration now enforces a single space after a property type keyword
131-
-- The PSR2 standard itself excludes this new check as it is not defined in the written standard
132-
-- Using the PSR12 standard will enforce this check
133-
- Squiz.Commenting.BlockComment no longer requires blank line before comment if it's the first content after the PHP open tag
134-
-- Thanks to Juliette Reinders Folmer for the patch
135-
- Squiz.Functions.FunctionDeclarationArgumentSpacing now has more accurate error messages
136-
-- This includes renaming the SpaceAfterDefault error code to SpaceAfterEquals, which reflects the real error
137-
- Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a reference operator
138-
-- If you don't want this new behaviour, exclude the SpacingAfterReference error message in a ruleset.xml file
139-
- Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a variadic operator
140-
-- If you don't want this new behaviour, exclude the SpacingAfterVariadic error message in a ruleset.xml file
141-
- Squiz.Functions.MultiLineFunctionDeclaration now has improved fixing for the FirstParamSpacing and UseFirstParamSpacing errors
142-
- Squiz.Operators.IncrementDecrementUsage now suggests pre-increment of variables instead of post-increment
143-
-- This change does not enforce pre-increment over post-increment; only the suggestion has changed
144-
-- Thanks to Juliette Reinders Folmer for the patch
145-
- Squiz.PHP.DisallowMultipleAssignments now has a second error code for when assignments are found inside control structure conditions
146-
-- The new error code is Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
147-
-- All other multiple assignment cases use the existing error code Squiz.PHP.DisallowMultipleAssignments.Found
148-
-- Thanks to Juliette Reinders Folmer for the patch
149-
- Squiz.WhiteSpace.FunctionSpacing now applies beforeFirst and afterLast spacing rules to nested functions
150-
-- Previously, these rules only applied to the first and last function in a class, interface, or trait
151-
-- These rules now apply to functions nested in any statement block, including other functions and conditions
152-
- Squiz.WhiteSpace.OperatorSpacing now has improved handling of parse errors
153-
-- Thanks to Juliette Reinders Folmer for the patch
154-
- Squiz.WhiteSpace.OperatorSpacing now checks spacing around the instanceof operator
155-
-- Thanks to Jakub Chábek for the patch
156-
- Squiz.WhiteSpace.OperatorSpacing can now enforce a single space before assignment operators
157-
-- Previously, the sniff this spacing as multiple assignment operators are sometimes aligned
158-
-- Now, you can set the ignoreSpacingBeforeAssignments sniff property to FALSE to enable checking
159-
-- Default remains TRUE, so spacing before assignments is not checked by default
160-
-- Thanks to Jakub Chábek for the patch
161-
- Fixed bug #2391 : Sniff-specific ignore rules inside rulesets are filtering out too many files
162-
-- Thanks to Juliette Reinders Folmer and Willington Vega for the patch
163-
- Fixed bug #2478 : FunctionCommentThrowTag.WrongNumber when exception is thrown once but built conditionally
164-
- Fixed bug #2479 : Generic.WhiteSpace.ScopeIndent error when using array destructing with exact indent checking
165-
- Fixed bug #2498 : Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed autofix breaks heredoc
166-
- Fixed bug #2502 : Generic.WhiteSpace.ScopeIndent false positives with nested switch indentation and case fall-through
167-
- Fixed bug #2504 : Generic.WhiteSpace.ScopeIndent false positives with nested arrays and nowdoc string
168-
- Fixed bug #2511 : PSR2 standard not checking if closing paren of single-line function declaration is on new line
169-
- Fixed bug #2512 : Squiz.PHP.NonExecutableCode does not support alternate SWITCH control structure
170-
-- Thanks to Juliette Reinders Folmer for the patch
171-
- Fixed bug #2522 : Text generator throws error when code sample line is too long
172-
-- Thanks to Juliette Reinders Folmer for the patch
173-
- Fixed bug #2526 : XML report format has bad syntax on Windows
174-
-- Thanks to Juliette Reinders Folmer for the patch
175-
- Fixed bug #2529 : Generic.Formatting.MultipleStatementAlignment wrong error for assign in string concat
176-
- Fixed bug #2534 : Unresolvable installed_paths can lead to open_basedir errors
177-
-- Thanks to Oliver Nowak for the patch
178-
- Fixed bug #2541 : Text doc generator does not allow for multi-line rule explanations
179-
-- Thanks to Juliette Reinders Folmer for the patch
180-
- Fixed bug #2549 : Searching for a phpcs.xml file can throw warnings due to open_basedir restrictions
181-
-- Thanks to Matthew Peveler for the patch
182-
- Fixed bug #2558 : PHP 7.4 throwing offset syntax with curly braces is deprecated message
183-
-- Thanks to Matthew Peveler for the patch
184-
- Fixed bug #2561 : PHP 7.4 compatibility fix / implode argument order
185-
-- Thanks to Juliette Reinders Folmer for the patch
186-
- Fixed bug #2562 : Inline WHILE triggers SpaceBeforeSemicolon incorrectly
187-
-- Thanks to Juliette Reinders Folmer for the patch
188-
- Fixed bug #2565 : Generic.ControlStructures.InlineControlStructure confused by mixed short/long tags
189-
-- Thanks to Juliette Reinders Folmer for the patch
190-
- Fixed bug #2566 : Author tag email validation doesn't support all TLDs
191-
-- Thanks to Juliette Reinders Folmer for the patch
192-
- Fixed bug #2575 : Custom error messages don't have data replaced when cache is enabled
193-
- Fixed bug #2601 : Squiz.WhiteSpace.FunctionSpacing incorrect fix when spacing is 0
194-
- Fixed bug #2608 : PSR2 throws errors for use statements when multiple namespaces are defined in a file
29+
- Fixed bug #2615 : Constant visibility false positive on non-class constants
19530
</notes>
19631
<contents>
19732
<dir name="/">

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Config
2323
*
2424
* @var string
2525
*/
26-
const VERSION = '3.5.0';
26+
const VERSION = '3.5.1';
2727

2828
/**
2929
* Package stability; either stable, beta or alpha.

src/Standards/PSR12/Sniffs/Properties/ConstantVisibilitySniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public function register()
4141
public function process(File $phpcsFile, $stackPtr)
4242
{
4343
$tokens = $phpcsFile->getTokens();
44-
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
44+
45+
// Make sure this is a class constant.
46+
if ($phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens) === false) {
47+
return;
48+
}
49+
50+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
4551
if (isset(Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) {
4652
return;
4753
}

src/Standards/PSR12/Tests/Properties/ConstantVisibilityUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ class Foo {
33
public const BAR = 'bar';
44
const BAZ = 'baz';
55
}
6+
7+
const APPLICATION_ENV = 'development';

src/Standards/PSR12/Tests/Properties/ConstantVisibilityUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14-
class UseDeclarationUnitTest extends AbstractSniffUnitTest
14+
class ConstantVisibilityUnitTest extends AbstractSniffUnitTest
1515
{
1616

1717

0 commit comments

Comments
 (0)