Completed
Pull Request — master (#661)
by Juliette
04:47 queued 02:50
created

SetlocaleStringSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 7.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bowOutEarly() 0 4 1
B processParameters() 5 33 6

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
 * \PHPCompatibility\Sniffs\FunctionParameters\SetlocaleStringSniff.
4
 *
5
 * PHP version 4.2
6
 * PHP version 7.0
7
 *
8
 * @category PHP
9
 * @package  PHPCompatibility
10
 * @author   Juliette Reinders Folmer <[email protected]>
11
 */
12
13
namespace PHPCompatibility\Sniffs\FunctionParameters;
14
15
use PHPCompatibility\AbstractFunctionCallParameterSniff;
16
17
/**
18
 * \PHPCompatibility\Sniffs\FunctionParameters\SetlocaleStringSniff.
19
 *
20
 * Detect: Support for the category parameter passed as a string has been removed.
21
 * Only LC_* constants can be used as of this version [7.0.0].
22
 *
23
 * PHP version 4.2
24
 * PHP version 7.0
25
 *
26
 * @category PHP
27
 * @package  PHPCompatibility
28
 * @author   Juliette Reinders Folmer <[email protected]>
29
 */
30
class SetlocaleStringSniff extends AbstractFunctionCallParameterSniff
31
{
32
33
    /**
34
     * Functions to check for.
35
     *
36
     * @var array
37
     */
38
    protected $targetFunctions = array(
39
        'setlocale' => true,
40
    );
41
42
43
    /**
44
     * Do a version check to determine if this sniff needs to run at all.
45
     *
46
     * @return bool
47
     */
48
    protected function bowOutEarly()
49
    {
50
        return ($this->supportsAbove('4.2') === false);
51
    }
52
53
54
    /**
55
     * Process the parameters of a matched function.
56
     *
57
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
58
     * @param int                   $stackPtr     The position of the current token in the stack.
59
     * @param string                $functionName The token content (function name) which was matched.
60
     * @param array                 $parameters   Array with information about the parameters.
61
     *
62
     * @return int|void Integer stack pointer to skip forward or void to continue
63
     *                  normal file processing.
64
     */
65
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
66
    {
67
        if (isset($parameters[1]) === false) {
68
            return;
69
        }
70
71
        $tokens      = $phpcsFile->getTokens();
72
        $targetParam = $parameters[1];
73
74
        for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
75
            if ($tokens[$i]['code'] !== T_CONSTANT_ENCAPSED_STRING
76
                && $tokens[$i]['code'] !== T_DOUBLE_QUOTED_STRING
77
            ) {
78
                continue;
79
            }
80
81
            $message   = 'Passing the $category as a string to setlocale() has been deprecated since PHP 4.2';
82
            $isError   = false;
83
            $errorCode = 'Deprecated';
84
            $data      = array($targetParam['raw']);
85
86 View Code Duplication
            if ($this->supportsAbove('7.0') === true) {
87
                $message  .= ' and is removed since PHP 7.0';
88
                $isError   = true;
89
                $errorCode = 'Removed';
90
            }
91
92
            $message .= '; Pass one of the LC_* constants instead. Found: %s';
93
94
            $this->addMessage($phpcsFile, $message, $i, $isError, $errorCode, $data);
95
            break;
96
        }
97
    }
98
}//end class
99