Fork of git://github.com/easybib/coding-standard.git

revision a91e0ac83a4317e291f414ebcfbb4e5a9d303f5c

raw

.gitignore

No idea how to display this file
raw

EasyBib/Sniffs/Commenting/ReturnStatementsSniff.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
 
class EasyBib_Sniffs_Commenting_ReturnStatementsSniff implements \PHP_CodeSniffer_Sniff
{
    public function register()
    {
        return array(T_FUNCTION);
    }
 
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
        if (null === $methodName) {
            return;
        }
 
        $tokens = $phpcsFile->getTokens();
 
        if ('__construct' === $methodName) {
            $toFind     = array(T_DOC_COMMENT,);
            $commentEnd = $phpcsFile->findPrevious(T_DOC_COMMENT, ($stackPtr-1));
            if (false === $commentEnd) {
                return;
            }
            $look = $commentEnd-1;
            while (true && ($look > 0)) {
                $docComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($look));
                if (false === $docComment) {
                    return;
                }
                $docRow  = $tokens[$docComment];
                $content = trim($docRow['content']);
                if (strstr($content, '@return void')) {
                    $phpcsFile->addError('`@return void` for __construct is invalid.', $docComment, 'ConstructNoReturnVoid');
                    return;
                }
                if ('/**' == $content) {
                    return;
                }
            }
        }
    }
}
 
raw

EasyBib/Sniffs/Methods/ConstructShouldNotReturnSniff.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
 * EasyBib\Sniffs\Methods\ConstructShouldNotReturnSniff
 *
 * PHP version 5.3
 *
 * @category QA
 * @package  EasyBib\Sniffs
 * @author   Till Klampaeckel <till@php.net>
 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
 * @version  GIT: <git_id>
 * @link     http://
 */
 
namespace EasyBib\Sniffs\Methods;
 
/**
 * EasyBib\Sniffs\Methods\ConstructShouldNotReturnSniff
 *
 * Ensures that there are no `return` statements in a `__construct` and that the docblock
 * says `return $this` - if anything.
 *
 * @category  QA
 * @package   EasyBib\Sniffs
 * @author    Till Klampaeckel <till@php.net>
 * @license   http://opensource.org/licenses/bsd-license.php New BSD License
 * @version   Release: @package_version@
 * @link      http://
 */
class ConstructShouldNotReturnSniff extends \PHP_CodeSniffer_Standards_AbstractScopeSniff
{
    public function __construct()
    {
        parent::__construct(array(T_FUNCTION), array(T_RETURN));
    }
 
    /**
     * Processes the tokens that this sniff is interested in.
     *
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
     * @param int                  $stackPtr  The position in the stack where
     *                                        the token was found.
     * @param int                  $currScope The current scope opener token.
     *
     * @return void
     */
    protected function processTokenWithinScope(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
    {
        $methodName = $phpcsFile->getDeclarationName($currScope);
        if (null === $methodName) {
            return;
        }
        if ('__construct' !== $methodName) {
            return;
        }
 
        $tokens = $phpcsFile->getTokens();
        if (!isset($tokens[$stackPtr])) {
            return;
        }
 
        if ('T_RETURN' === $tokens[$stackPtr]['type'] && 'T_SEMICOLON' !== $tokens[$stackPtr+1]['type']) {
            $phpcsFile->addWarning('__construct cannot return anything', $stackPtr, 'NoReturnInConstruct');
            return;
        }
    }
}
 
raw

EasyBib/ruleset.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?xml version="1.0"?>
<ruleset name="EasyBib">
 
 <description>EasyBib Coding Standard.</description>
 
 <exclude-pattern>*tests/*</exclude-pattern>
 <exclude-pattern>*data/*</exclude-pattern>
 <exclude-pattern>*var/*</exclude-pattern>
 <exclude-pattern>*bin/*</exclude-pattern>
 <exclude-pattern>*vendor/*</exclude-pattern>
 <exclude-pattern>*www/*</exclude-pattern>
 <exclude-pattern>*views/scripts/*</exclude-pattern>
 <exclude-pattern>*etc/*</exclude-pattern>
 <exclude-pattern>*examples/*</exclude-pattern>
 <exclude-pattern>*docs/*</exclude-pattern>
 
 <rule ref="PEAR"/>
 <rule ref="PSR1"/>
 <rule ref="PSR2"/>
 
 <rule ref="Generic.Commenting.Todo.CommentFound">
  <message>Please review this TODO comment: %s</message>
  <severity>3</severity>
 </rule>
 
 <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
 <rule ref="Generic.NamingConventions.CamelCapsFunctionName">
  <properties>
   <property name="strict" value="false"/>
  </properties>
 </rule>
 
 <rule ref="PEAR.NamingConventions.ValidFunctionName">
  <exclude name="PEAR.NamingConventions.ValidFunctionName.PrivateNoUnderscore"/>
  <exclude name="PEAR.NamingConventions.ValidFunctionName.FunctionUnderscore"/>
 </rule>
 
 <rule ref="PEAR.NamingConventions.ValidVariableName">
  <exclude name="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
 </rule>
 
 <rule ref="PSR2.Files.EndFileNewline">
  <exclude name="PSR2.Files.EndFileNewline.NotFound"/>
 </rule>
 
 <rule ref="EasyBib.Methods.ConstructShouldNotReturn"/>
 <rule ref="EasyBib.Commenting.ReturnStatements"/>
</ruleset>
 
raw

README.md

EasyBib's Coding Standard

Usage (WIP)

  1. Install: pear install PHP_CodeSniffer
  2. clone this repo
  3. check CS

This is how:

 phpcs \
 --standard=/path/to/repo/EasyBib/ruleset.xml \
 target-dir

Todo

  • create a pear package
  • figure out how to register the style
  • add this to .travis.yml on various projects
raw

composer.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    "type": "library",
    "license": "BSD",
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.php.net"
        }
    ],
    "autoload": {
        "psr-0": {
            "EasyBib\\PHP\\CodeSniffer": "library",
            "PHP_CodeSniffer": "vendor/pear-pear.php.net/PHP_CodeSniffer"
        }
    },
    "require-dev": {
        "pear-pear/php_codesniffer": "*"
    }
}
 
raw

test.php

1
2
3
4
5
6
<?php
require './vendor/autoload.php';
 
$cs = new \EasyBib\PHP\CodeSniffer\CodingStandard;
$cs->getIncludedSniffs();
 

History