1<?php
2/**
3 * Ensures that a system does not include itself.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer_MySource
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Ensures that a system does not include itself.
17 *
18 * @category  PHP
19 * @package   PHP_CodeSniffer_MySource
20 * @author    Greg Sherwood <gsherwood@squiz.net>
21 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
22 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23 * @version   Release: @package_version@
24 * @link      http://pear.php.net/package/PHP_CodeSniffer
25 */
26class MySource_Sniffs_Channels_IncludeOwnSystemSniff implements PHP_CodeSniffer_Sniff
27{
28
29
30    /**
31     * Returns an array of tokens this test wants to listen for.
32     *
33     * @return array
34     */
35    public function register()
36    {
37        return array(T_DOUBLE_COLON);
38
39    }//end register()
40
41
42    /**
43     * Processes this sniff, when one of its tokens is encountered.
44     *
45     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46     * @param int                  $stackPtr  The position of the current token in
47     *                                        the stack passed in $tokens.
48     *
49     * @return void
50     */
51    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52    {
53        $fileName = $phpcsFile->getFilename();
54        $matches  = array();
55        if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|i', $fileName, $matches) === 0) {
56            // Not an actions file.
57            return;
58        }
59
60        $ownClass = $matches[2];
61        $tokens   = $phpcsFile->getTokens();
62
63        $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 2), null, false, true);
64        $typeName = trim($tokens[$typeName]['content'], " '");
65        switch (strtolower($tokens[($stackPtr + 1)]['content'])) {
66        case 'includesystem' :
67            $included = strtolower($typeName);
68            break;
69        case 'includeasset' :
70            $included = strtolower($typeName).'assettype';
71            break;
72        case 'includewidget' :
73            $included = strtolower($typeName).'widgettype';
74            break;
75        default:
76            return;
77        }
78
79        if ($included === strtolower($ownClass)) {
80            $error = "You do not need to include \"%s\" from within the system's own actions file";
81            $data  = array($ownClass);
82            $phpcsFile->addError($error, $stackPtr, 'NotRequired', $data);
83        }
84
85    }//end process()
86
87
88    /**
89     * Determines the included class name from given token.
90     *
91     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
92     * @param array                $tokens    The array of file tokens.
93     * @param int                  $stackPtr  The position in the tokens array of the
94     *                                        potentially included class.
95     *
96     * @return string
97     */
98    protected function getIncludedClassFromToken(
99        PHP_CodeSniffer_File $phpcsFile,
100        array $tokens,
101        $stackPtr
102    ) {
103
104        return false;
105
106    }//end getIncludedClassFromToken()
107
108
109}//end class
110