Skip to content

Commit

Permalink
Added PSR12.Properties.ConstantVisibility sniff (ref squizlabs#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Aug 23, 2019
1 parent b39b765 commit 3285109
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Enforce the use of a strict types declaration in PHP files
- Added PSR12.Files.DeclareStatement sniff
-- Enforces the formatting of declare statements within a file
- Added PSR12.Properties.ConstantVisibility sniff
-- Enforces that constants must have their visibility defined
-- Uses a warning instead of an error due to this conditionally requiring the project to support PHP 7.1+
- Added PSR12.Traits.UseDeclaration sniff
-- Enforces the formatting of trait import statements within a class
- Generic.Files.LineLength ignoreComments property now ignores comments at the end of a line
Expand Down
55 changes: 55 additions & 0 deletions src/Standards/PSR12/Sniffs/Properties/ConstantVisibilitySniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Verifies that all class constants have their visibility set.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Properties;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class ConstantVisibilitySniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [T_CONST];

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if (isset(Tokens::$scopeModifiers[$tokens[$prev]['code']]) === true) {
return;
}

$error = 'Visibility must be declared on all constants if your project supports PHP 7.1 or later';
$phpcsFile->addWarning($error, $stackPtr, 'NotFound');

}//end process()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
class Foo {
public const BAR = 'bar';
const BAZ = 'baz';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Unit test class for the ConstantVisibility sniff.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\PSR12\Tests\Properties;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class UseDeclarationUnitTest extends AbstractSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array<int, int>
*/
public function getErrorList()
{
return [];

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array<int, int>
*/
public function getWarningList()
{
return [4 => 1];

}//end getWarningList()


}//end class
5 changes: 2 additions & 3 deletions src/Standards/PSR12/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,10 @@
<!-- 4.2 Using traits -->

<!-- The use keyword used inside the classes to implement traits MUST be declared on the next line after the opening brace. -->

<!-- Each individual trait that is imported into a class MUST be included one-per-line and each inclusion MUST have its own use import statement. -->

<!-- When the class has nothing after the use import statement, the class closing brace MUST be on the next line after the use import statement. Otherwise, it MUST have a blank line after the use import statement. -->

<!-- When using the insteadof and as operators they must be used as follows taking note of indentation, spacing, and new lines. -->
<!-- checked by PSR12.Traits.UseDeclaration -->

<!-- 4.3 Properties and Constants -->

Expand All @@ -147,6 +145,7 @@
<rule ref="PSR2.Classes.PropertyDeclaration"/>

<!-- Visibility MUST be declared on all constants if your project PHP minimum version supports constant visibilities (PHP 7.1 or later). -->
<!-- checked by PSR12.Properties.ConstantVisibility -->

<!-- There MUST be a space between type declaration and property name. -->

Expand Down

0 comments on commit 3285109

Please sign in to comment.