Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
SlevomatCodingStandard/Sniffs/Classes/ClassConstantVisibilitySniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use SlevomatCodingStandard\Helpers\ClassHelper; | ||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
|
||
class ClassConstantVisibilitySniff implements \PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
const CODE_MISSING_CONSTANT_VISIBILITY = 'MissingConstantVisibility'; | ||
|
||
/** | ||
* @return int[] | ||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
T_CONST, | ||
]; | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
* @param \PHP_CodeSniffer_File $phpcsFile | ||
* @param int $constantPointer | ||
*/ | ||
public function process(\PHP_CodeSniffer_File $phpcsFile, $constantPointer) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if (count($tokens[$constantPointer]['conditions']) === 0) { | ||
return; | ||
} | ||
|
||
$classPointer = array_keys($tokens[$constantPointer]['conditions'])[count($tokens[$constantPointer]['conditions']) - 1]; | ||
if (!in_array($tokens[$classPointer]['code'], [T_CLASS, T_INTERFACE], true)) { | ||
return; | ||
} | ||
|
||
$visibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $constantPointer - 1); | ||
if (!in_array($tokens[$visibilityPointer]['code'], [T_PUBLIC, T_PROTECTED, T_PRIVATE], true)) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
'Constant %s::%s visibility missing.', | ||
ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer), | ||
$tokens[TokenHelper::findNextEffective($phpcsFile, $constantPointer + 1)]['content'] | ||
), | ||
$constantPointer, | ||
self::CODE_MISSING_CONSTANT_VISIBILITY | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
class ClassConstantVisibilitySniffTest extends \SlevomatCodingStandard\Sniffs\TestCase | ||
{ | ||
|
||
public function testErrors() | ||
{ | ||
$report = $this->checkFile(__DIR__ . '/data/classWithConstants.php'); | ||
|
||
$this->assertSame(1, $report->getErrorCount()); | ||
|
||
$this->assertNoSniffError($report, 7); | ||
$this->assertNoSniffError($report, 9); | ||
$this->assertNoSniffError($report, 10); | ||
|
||
$this->assertSniffError( | ||
$report, | ||
6, | ||
ClassConstantVisibilitySniff::CODE_MISSING_CONSTANT_VISIBILITY, | ||
'Constant \ClassWithConstants::PUBLIC_FOO visibility missing.' | ||
); | ||
} | ||
|
||
public function testNoClassConstants() | ||
{ | ||
$report = $this->checkFile(__DIR__ . '/data/noClassConstants.php'); | ||
$this->assertNoSniffErrorInFile($report); | ||
} | ||
|
||
public function testNoClassConstantsWithNamespace() | ||
{ | ||
$report = $this->checkFile(__DIR__ . '/data/noClassConstantsWithNamespace.php'); | ||
$this->assertNoSniffErrorInFile($report); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php // lint >= 7.1 | ||
|
||
class ClassWithConstants | ||
{ | ||
|
||
const PUBLIC_FOO = null; | ||
public const PUBLIC_BAR = null; | ||
|
||
protected const PROTECTED_FOO = null; | ||
private const PRIVATE_FOO = null; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
use const Boo\BOO; | ||
|
||
const FOO = 'foo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Foo { | ||
|
||
use const Boo\BOO; | ||
|
||
const FOO = 'foo'; | ||
|
||
} |