ByteOrderMarkSniff   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 7.58 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 5
loc 66
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A getBomDefinitions() 0 10 1
B process() 5 21 5

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_ByteOrderMarkSniff.
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_ByteOrderMarkSniff.
17
 *
18
 * Ensures that no BOM appears at the beginning of file.
19
 *
20
 * @category  PHP
21
 * @package   PHP_CodeSniffer
22
 * @author    Thomas Ernest <[email protected]>
23
 * @copyright 2006 Thomas Ernest
24
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
25
 * @link      http://pear.php.net/package/PHP_CodeSniffer
26
 */
27
28
namespace CodeIgniter\Sniffs\Files;
29
30
use PHP_CodeSniffer\Sniffs\Sniff;
31
use PHP_CodeSniffer\Files\File;
32
33
class ByteOrderMarkSniff implements Sniff
34
{
35
    /**
36
     * Returns an array of tokens this test wants to listen for.
37
     *
38
     * @return array
39
     */
40
    public function register()
41
    {
42
        return array( T_OPEN_TAG );
43
    }//end register()
44
45
    /**
46
     * List of supported BOM definitions.
47
     *
48
     * Use encoding names as keys and hex BOM representations as values.
49
     *
50
     * @return array
51
     */
52
    protected function getBomDefinitions()
53
    {
54
        return array(
55
            'UTF-8'       => 'efbbbf',
56
            'UTF-16 (BE)' => 'feff',
57
            'UTF-16 (LE)' => 'fffe',
58
            'UTF-32 (BE)' => '0000feff',
59
            'UTF-32 (LE)' => 'fffe0000'
60
        );
61
    }//end getBomDefinitions()
62
63
    /**
64
     * Process tokens.
65
     *
66
     * Actually, only proceed when we're at index 0, this should be the only case
67
     * that will contain BOM. Then check if BOM definition matches what
68
     * we've found as file's inline HTML. Inline HTML could be longer than just BOM
69
     * so make sure you test as much as needed.
70
     *
71
     * @param File $phpcsFile The current file being scanned.
72
     * @param int                  $stackPtr  The position of the current token
73
     *                                        in the stack passed in $tokens.
74
     *
75
     * @return void
76
     */
77
    public function process(File $phpcsFile, $stackPtr )
78
    {
79
        // We are only interested if this is the first open tag.
80 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...
81
            if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
82
                return;
83
            }
84
        }
85
86
        $tokens = $phpcsFile->getTokens();
87
        $fileStartString = $tokens[0]['content'];
88
        foreach ($this->getBomDefinitions() as $bomName => $expectedBomHex) {
89
            $bomByteLength = strlen($expectedBomHex) / 2;
90
            $fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength));
91
            if ($fileStartHex === $expectedBomHex) {
92
                $error = "File contains a $bomName byte order mark (BOM).";
93
                $phpcsFile->addError($error, $stackPtr);
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...
94
                break;
95
            }
96
        }
97
    }//end process()
98
}
0 ignored issues
show
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...
99