Skip to content

Commit

Permalink
Added ClassConstantVisibilitySniff
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz authored and kukulich committed Feb 9, 2017
1 parent ace0a6e commit 1d6a047
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
@@ -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
);
}
}

}
4 changes: 3 additions & 1 deletion ruleset.xml
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<ruleset name="Slevomat Coding Standard">
<rule ref="SlevomatCodingStandard/ruleset.xml"/>
<rule ref="SlevomatCodingStandard/ruleset.xml">
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility"/><!-- PHP >= 7.1 only -->
</rule>
<rule ref="vendor/consistence/coding-standard/Consistence/ruleset.xml">
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedIF"/><!-- Allow empty if statements - usually with a comment -->
</rule>
Expand Down
38 changes: 38 additions & 0 deletions tests/Sniffs/Classes/ClassConstantVisibilitySniffTest.php
@@ -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);
}

}
12 changes: 12 additions & 0 deletions tests/Sniffs/Classes/data/classWithConstants.php
@@ -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;

}
5 changes: 5 additions & 0 deletions tests/Sniffs/Classes/data/noClassConstants.php
@@ -0,0 +1,5 @@
<?php

use const Boo\BOO;

const FOO = 'foo';
9 changes: 9 additions & 0 deletions tests/Sniffs/Classes/data/noClassConstantsWithNamespace.php
@@ -0,0 +1,9 @@
<?php

namespace Foo {

use const Boo\BOO;

const FOO = 'foo';

}

0 comments on commit 1d6a047

Please sign in to comment.