ClosingFileCommentSniff   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 34.78 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 24
loc 69
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
D process() 24 42 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * CodeIgniter_Sniffs_Files_ClosingFileCommentSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Thomas Ernest <[email protected]>
10
 * @copyright 2006 Thomas Ernest
11
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 */
14
15
/**
16
 * CodeIgniter_Sniffs_Files_ClosingFileCommentSniff.
17
 *
18
 * Ensures that a comment containing the file name is available at the end of file.
19
 * Only other comments and whitespaces are allowed to follow this specific comment.
20
 *
21
 * It may be all kind of comment like multi-line and inline C-style comments as
22
 * well as PERL-style comments. Any number of white may separate comment delimiters
23
 * from comment content. However, content has to be equal to template
24
 * "End of file <file_name>". Comparison between content and template is
25
 * case-sensitive.
26
 *
27
 * @category  PHP
28
 * @package   PHP_CodeSniffer
29
 * @author    Thomas Ernest <[email protected]>
30
 * @copyright 2006 Thomas Ernest
31
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
32
 * @link      http://pear.php.net/package/PHP_CodeSniffer
33
 */
34
35
namespace CodeIgniter\Sniffs\Files;
36
37
use PHP_CodeSniffer\Files\File;
38
39
class ClosingFileCommentSniff extends AbstractClosingCommentSniff
40
{
41
42
    /**
43
     * Returns an array of tokens this test wants to listen for.
44
     *
45
     * @return array
46
     */
47
    public function register()
48
    {
49
        return array(
50
            T_OPEN_TAG,
51
        );
52
53
    }//end register()
54
55
56
    /**
57
     * Processes this test, when one of its tokens is encountered.
58
     *
59
     * @param File $phpcsFile The current file being scanned.
60
     * @param int                  $stackPtr  The position of the current token
61
     *                                        in the stack passed in $tokens.
62
     *
63
     * @return void
64
     */
65
    public function process(File $phpcsFile, $stackPtr)
66
    {
67
        // We are only interested if this is the first open tag.
68 View Code Duplication
        if ($stackPtr !== 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
            if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
70
                return;
71
            }
72
        }
73
74
        $fullFilename = $phpcsFile->getFilename();
75
        $filename = basename($fullFilename);
76
        $commentTemplate = "End of file $filename";
77
78
        $tokens = $phpcsFile->getTokens();
79
        $currentToken = count($tokens) - 1;
80
        $hasClosingFileComment = false;
81
        $isNotAWhitespaceOrAComment = false;
82 View Code Duplication
        while ($currentToken >= 0
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            && ! $isNotAWhitespaceOrAComment
84
            && ! $hasClosingFileComment
85
        ) {
86
            $token = $tokens[$currentToken];
87
            $tokenCode = $token['code'];
88
            if (T_COMMENT === $tokenCode) {
89
                $commentString = self::_getCommentContent($token['content']);
90
                if (0 === strcmp($commentString, $commentTemplate)) {
91
                    $hasClosingFileComment = true;
92
                }
93
            } else if (T_WHITESPACE === $tokenCode) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
94
                // Whitespaces are allowed between the closing file comment,
95
                // other comments and end of file
96
            } else {
97
                $isNotAWhitespaceOrAComment = true;
98
            }
99
            $currentToken--;
100
        }
101
102
        if ( ! $hasClosingFileComment) {
103
            $error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 165 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
104
            $phpcsFile->addError($error, $currentToken);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
105
        }
106
    }//end process()
107
}//end class
108
109
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
110