Completed
Pull Request — master (#291)
by Juliette
02:26
created

getErrorMsgTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * PHPCompatibility_AbstractNewFeatureSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PPHPCompatibility_AbstractNewFeatureSniff.
12
 *
13
 * @category PHP
14
 * @package  PHPCompatibility
15
 * @author   Juliette Reinders Folmer <[email protected]>
16
 */
17
abstract class PHPCompatibility_AbstractNewFeatureSniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
    extends PHPCompatibility_AbstractComplexVersionSniff
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
19
{
20
21
22
    /**
23
     * Determine whether an error/warning should be thrown for an item based on collected information.
24
     *
25
     * @param array $errorInfo Detail information about an item.
26
     *
27
     * @return bool
28
     */
29
    protected function shouldThrowError(array $errorInfo)
30
    {
31
        return ($errorInfo['not_in_version'] !== '');
32
    }
33
34
35
    /**
36
     * Retrieve the relevant detail (version) information for use in an error message.
37
     *
38
     * @param array $itemArray Version and other information about the item.
39
     * @param array $itemInfo  Base information about the item.
40
     *
41
     * @return array
42
     */
43
    public function getErrorInfo(array $itemArray, array $itemInfo)
44
    {
45
        $errorInfo = array(
46
            'not_in_version' => '',
47
            'error'          => true,
48
        );
49
50
        $versionArray = $this->getVersionArray($itemArray);
51
52
        if (empty($versionArray) === false) {
53 View Code Duplication
            foreach ($versionArray as $version => $present) {
1 ignored issue
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...
54
                if ($errorInfo['not_in_version'] === '' && $present === false
55
                    && $this->supportsBelow($version) === true
56
                ) {
57
                    $errorInfo['not_in_version'] = $version;
58
                }
59
            }
60
        }
61
62
        return $errorInfo;
63
    }
64
65
66
    /**
67
     * Get the error message template for this sniff.
68
     *
69
     * @return string
70
     */
71
    protected function getErrorMsgTemplate()
72
    {
73
        return '%s is not present in PHP version %s or earlier';
74
    }
75
76
77
    /**
78
     * Generates the error or warning for this item.
79
     *
80
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
81
     * @param int                  $stackPtr  The position of the relevant token in
82
     *                                        the stack.
83
     * @param array                $itemInfo  Base information about the item.
84
     * @param array                $errorInfo Array with detail (version) information
85
     *                                        relevant to the item.
86
     *
87
     * @return void
88
     */
89
    public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
90
    {
91
        $itemName = $this->getItemName($itemInfo, $errorInfo);
92
        $error    = $this->getErrorMsgTemplate();
93
94
        $errorCode = $this->stringToErrorCode($itemName).'Found';
95
        $data      = array(
96
            $itemName,
97
            $errorInfo['not_in_version'],
98
        );
99
100
        $error = $this->filterErrorMsg($error, $itemInfo, $errorInfo);
101
        $data  = $this->filterErrorData($data, $itemInfo, $errorInfo);
102
103
        $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
104
    }
105
106
107
}//end class
108