Skip to content

Commit

Permalink
VarPropertyCommentSniff added [closes #22]
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 21, 2015
1 parent ad843f6 commit 26b6acb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* This file is part of Zenify
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
*/

namespace ZenifyCodingStandard\Sniffs\Commenting;

use PHP_CodeSniffer_File;
use PHP_CodeSniffer_Standards_AbstractVariableSniff;


/**
* Rules:
* - Property should have docblock comment.
*/
class VarPropertyCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
{

/**
* {@inheritdoc}
*/
protected function processMemberVar(PHP_CodeSniffer_File $file, $position)
{
$commentToken = [T_COMMENT, T_DOC_COMMENT_CLOSE_TAG];
$commentEnd = $file->findPrevious($commentToken, $position);
if ($commentEnd === FALSE) {
$file->addError('Property should have docblock comment', $position);
return;
}
}


/**
* {@inheritdoc}
*/
protected function processVariable(PHP_CodeSniffer_File $file, $position)
{
}


/**
* {@inheritdoc}
*/
protected function processVariableInString(PHP_CodeSniffer_File $file, $position)
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Zenify\CodingStandard\Tests\Sniffs\Commenting\VarPropertyComment;

use PHPUnit_Framework_TestCase;
use Zenify\CodingStandard\Tests\CodeSnifferRunner;


class VarPropertyCommentSniffTest extends PHPUnit_Framework_TestCase
{

/**
* @var CodeSnifferRunner
*/
private $codeSnifferRunner;


protected function setUp()
{
$this->codeSnifferRunner = new CodeSnifferRunner('ZenifyCodingStandard.Commenting.VarPropertyComment');
}


public function testDetection()
{
$this->assertSame(1, $this->codeSnifferRunner->getErrorCountInFile(__DIR__ . '/wrong.php'));
$this->assertSame(0, $this->codeSnifferRunner->getErrorCountInFile(__DIR__ . '/correct.php'));
}

}
14 changes: 14 additions & 0 deletions tests/Sniffs/Commenting/VarPropertyComment/correct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SomeNamespace;


class SomeClass
{

/**
* @var int
*/
public $count;

}
11 changes: 11 additions & 0 deletions tests/Sniffs/Commenting/VarPropertyComment/wrong.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SomeOtherNamespace;


class SomeClass
{

public $count;

}

0 comments on commit 26b6acb

Please sign in to comment.