~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20140721144241-t0v9knmgtzftbsw6
Tags: upstream-1.5.3
Import upstream version 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Generic_Sniffs_NamingConventions_ConstructorNameSniff.
4
 
 *
5
 
 * PHP version 5
6
 
 *
7
 
 * @category  PHP
8
 
 * @package   PHP_CodeSniffer
9
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 
 * @author    Leif Wickland <lwickland@rightnow.com>
11
 
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
12
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 
 */
15
 
 
16
 
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
17
 
    $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
18
 
    throw new PHP_CodeSniffer_Exception($error);
19
 
}
20
 
 
21
 
/**
22
 
 * Generic_Sniffs_NamingConventions_ConstructorNameSniff.
23
 
 *
24
 
 * Favor PHP 5 constructor syntax, which uses "function __construct()".
25
 
 * Avoid PHP 4 constructor syntax, which uses "function ClassName()".
26
 
 *
27
 
 * @category PHP
28
 
 * @package  PHP_CodeSniffer
29
 
 * @author   Leif Wickland <lwickland@rightnow.com>
30
 
 * @license  https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31
 
 * @version  Release: 1.5.0RC2
32
 
 * @link     http://pear.php.net/package/PHP_CodeSniffer
33
 
 */
34
 
class Generic_Sniffs_NamingConventions_ConstructorNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
35
 
{
36
 
 
37
 
 
38
 
    /**
39
 
     * Constructs the test with the tokens it wishes to listen for.
40
 
     *
41
 
     * @return void
42
 
     */
43
 
    public function __construct()
44
 
    {
45
 
        parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
46
 
 
47
 
    }//end __construct()
48
 
 
49
 
 
50
 
    /**
51
 
     * Processes this test when one of its tokens is encountered.
52
 
     *
53
 
     * @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
54
 
     * @param int                  $stackPtr  The position of the current token
55
 
     *                                        in the stack passed in $tokens.
56
 
     * @param int                  $currScope A pointer to the start of the scope.
57
 
     *
58
 
     * @return void
59
 
     */
60
 
    protected function processTokenWithinScope(
61
 
        PHP_CodeSniffer_File $phpcsFile,
62
 
        $stackPtr,
63
 
        $currScope
64
 
    ) {
65
 
        $className  = $phpcsFile->getDeclarationName($currScope);
66
 
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
67
 
 
68
 
        if (strcasecmp($methodName, $className) === 0) {
69
 
            $error = 'PHP4 style constructors are not allowed; use "__construct()" instead';
70
 
            $phpcsFile->addError($error, $stackPtr, 'OldStyle');
71
 
        } else if (strcasecmp($methodName, '__construct') !== 0) {
72
 
            // Not a constructor.
73
 
            return;
74
 
        }
75
 
 
76
 
        $tokens = $phpcsFile->getTokens();
77
 
 
78
 
        $parentClassName = $phpcsFile->findExtendedClassName($currScope);
79
 
        if ($parentClassName === false) {
80
 
            return;
81
 
        }
82
 
 
83
 
        $endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
84
 
        $startIndex       = $stackPtr;
85
 
        while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) {
86
 
            if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
87
 
                && $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName
88
 
            ) {
89
 
                $error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead';
90
 
                $phpcsFile->addError($error, ($doubleColonIndex + 1), 'OldStyleCall');
91
 
            }
92
 
 
93
 
            $startIndex = ($doubleColonIndex + 1);
94
 
        }
95
 
 
96
 
    }//end processTokenWithinScope()
97
 
 
98
 
 
99
 
}//end class
100
 
 
101
 
?>