~ubuntu-branches/ubuntu/wily/php-codesniffer/wily

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.3/CodeSniffer/Standards/Squiz/Sniffs/Files/FileExtensionSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Dawid Nowak, Weston Ruter, David Prévot
  • Date: 2014-08-09 12:28:47 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140809122847-giuxi3ezmeajasvy
Tags: 1.5.4-1
* Team upload

[ Greg Sherwood ]
* Removed use of sys_get_temp_dir() as this is not supported by the min PHP
  version
* Fixed bug #20268 : Incorrect documentation titles in PEAR documentation
* Generic ScopeIndentSniff now accounts for different open tag indents
* Forgot that short array token are detected by the tokenizer even on
  unsupported PHP versions
* Fixed bug #20296 : new array notion in function comma check fails
* Fixed bug #20310 : PSR2 does not check for space after function name
* Fixed bug #20309 : Use "member variable" term in sniff "processMemberVar"
  method
* Fixed bug #20307 : PHP_CodeSniffer_Standards_AbstractVariableSniff analyze
  traits
* Fixed bug #20308 : Squiz.ValidVariableNameSniff - wrong variable usage
* Squiz InlineCommentSniff no longer requires a blank line after
  post-statement comments (request #20299)
* Fixed bug #20322 : Display rules set to type=error even when suppressing
  warnings
* Invalid sniff codes passed to --sniffs now show a friendly error message
  (request #20313)
* Generic LineLengthSniff now shows a warning if the iconv module is disabled
  (request #20314)
* Fixed bug #20323 : PHPCS tries to load sniffs from hidden directories
* Squiz SelfMemberReferenceSniff now works correctly with namespaces
* Prepare for 1.5.4 release

[ Dawid Nowak ]
* Correct multiline call detection: PSR2_FunctionCallSignatureSniff (string
  with multiple lines doesn't  necessarily mean multiple lines call)

[ Weston Ruter ]
* Allow installed_paths to be relative to the phpcs root directory

[ David Prévot ]
* Update phpcs.1
* Add XS-Testsuite still needed for ci.d.n

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Squiz_Sniffs_Files_FileExtensionSniff.
4
 
 *
5
 
 * PHP version 5
6
 
 *
7
 
 * @category  PHP
8
 
 * @package   PHP_CodeSniffer
9
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
11
 
 * @copyright 2006-2014 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
 
/**
17
 
 * Squiz_Sniffs_Files_FileExtensionSniff.
18
 
 *
19
 
 * Tests that classes and interfaces are not declared in .php files
20
 
 *
21
 
 * @category  PHP
22
 
 * @package   PHP_CodeSniffer
23
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
24
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
25
 
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27
 
 * @version   Release: 1.5.3
28
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
29
 
 */
30
 
class Squiz_Sniffs_Files_FileExtensionSniff implements PHP_CodeSniffer_Sniff
31
 
{
32
 
 
33
 
 
34
 
    /**
35
 
     * Returns an array of tokens this test wants to listen for.
36
 
     *
37
 
     * @return array
38
 
     */
39
 
    public function register()
40
 
    {
41
 
        return array(T_OPEN_TAG);
42
 
 
43
 
    }//end register()
44
 
 
45
 
 
46
 
    /**
47
 
     * Processes this test, when one of its tokens is encountered.
48
 
     *
49
 
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
 
     * @param int                  $stackPtr  The position of the current token in the
51
 
     *                                        stack passed in $tokens.
52
 
     *
53
 
     * @return void
54
 
     */
55
 
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
56
 
    {
57
 
        $tokens = $phpcsFile->getTokens();
58
 
 
59
 
        // Make sure this is the first PHP open tag so we don't process
60
 
        // the same file twice.
61
 
        $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
62
 
        if ($prevOpenTag !== false) {
63
 
            return;
64
 
        }
65
 
 
66
 
        $fileName  = $phpcsFile->getFileName();
67
 
        $extension = substr($fileName, strrpos($fileName, '.'));
68
 
        $nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_TRAIT), $stackPtr);
69
 
 
70
 
        if ($extension === '.php') {
71
 
            if ($nextClass !== false) {
72
 
                $error = '%s found in ".php" file; use ".inc" extension instead';
73
 
                $data  = array(ucfirst($tokens[$nextClass]['content']));
74
 
                $phpcsFile->addError($error, $stackPtr, 'ClassFound', $data);
75
 
            }
76
 
        } else if ($extension === '.inc') {
77
 
            if ($nextClass === false) {
78
 
                $error = 'No interface or class found in ".inc" file; use ".php" extension instead';
79
 
                $phpcsFile->addError($error, $stackPtr, 'NoClass');
80
 
            }
81
 
        }
82
 
 
83
 
    }//end process()
84
 
 
85
 
 
86
 
}//end class
87
 
 
88
 
 
89
 
?>