• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 18114150721

30 Sep 2025 12:00AM UTC coverage: 78.601% (-0.6%) from 79.178%
18114150721

push

github

web-flow
Merge pull request #1284 from PHPCSStandards/dependabot/github_actions/3.x/action-runners-f175f89d6c

GH Actions: Bump actions/cache from 4.2.4 to 4.3.0 in the action-runners group

25341 of 32240 relevant lines covered (78.6%)

74.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.23
/src/Standards/MySource/Sniffs/PHP/EvalObjectFactorySniff.php
1
<?php
2
/**
3
 * Ensures that eval() is not used to create objects.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
8
 *
9
 * @deprecated 3.9.0
10
 */
11

12
namespace PHP_CodeSniffer\Standards\MySource\Sniffs\PHP;
13

14
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
15
use PHP_CodeSniffer\Sniffs\Sniff;
16
use PHP_CodeSniffer\Files\File;
17
use PHP_CodeSniffer\Util\Tokens;
18

19
class EvalObjectFactorySniff implements Sniff, DeprecatedSniff
20
{
21

22

23
    /**
24
     * Returns an array of tokens this test wants to listen for.
25
     *
26
     * @return array<int|string>
27
     */
28
    public function register()
3✔
29
    {
30
        return [T_EVAL];
3✔
31

32
    }//end register()
33

34

35
    /**
36
     * Processes this sniff, when one of its tokens is encountered.
37
     *
38
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
39
     * @param int                         $stackPtr  The position of the current token in
40
     *                                               the stack passed in $tokens.
41
     *
42
     * @return void
43
     */
44
    public function process(File $phpcsFile, $stackPtr)
3✔
45
    {
46
        $tokens = $phpcsFile->getTokens();
3✔
47

48
        /*
49
            We need to find all strings that will be in the eval
50
            to determine if the "new" keyword is being used.
51
        */
52

53
        $openBracket  = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1));
3✔
54
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
55

56
        $strings = [];
3✔
57
        $vars    = [];
3✔
58

59
        for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
3✔
60
            if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true) {
3✔
61
                $strings[$i] = $tokens[$i]['content'];
3✔
62
            } else if ($tokens[$i]['code'] === T_VARIABLE) {
3✔
63
                $vars[$i] = $tokens[$i]['content'];
3✔
64
            }
1✔
65
        }
1✔
66

67
        /*
68
            We now have some variables that we need to expand into
69
            the strings that were assigned to them, if any.
70
        */
71

72
        foreach ($vars as $varPtr => $varName) {
3✔
73
            while (($prev = $phpcsFile->findPrevious(T_VARIABLE, ($varPtr - 1))) !== false) {
3✔
74
                // Make sure this is an assignment of the variable. That means
75
                // it will be the first thing on the line.
76
                $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true);
3✔
77
                if ($tokens[$prevContent]['line'] === $tokens[$prev]['line']) {
3✔
78
                    $varPtr = $prevContent;
3✔
79
                    continue;
3✔
80
                }
81

82
                if ($tokens[$prev]['content'] !== $varName) {
3✔
83
                    // This variable has a different name.
84
                    $varPtr = $prevContent;
3✔
85
                    continue;
3✔
86
                }
87

88
                // We found one.
89
                break;
3✔
90
            }//end while
91

92
            if ($prev !== false) {
3✔
93
                // Find all strings on the line.
94
                $lineEnd = $phpcsFile->findNext(T_SEMICOLON, ($prev + 1));
3✔
95
                for ($i = ($prev + 1); $i < $lineEnd; $i++) {
3✔
96
                    if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true) {
3✔
97
                        $strings[$i] = $tokens[$i]['content'];
3✔
98
                    }
1✔
99
                }
1✔
100
            }
1✔
101
        }//end foreach
1✔
102

103
        foreach ($strings as $string) {
3✔
104
            // If the string has "new" in it, it is not allowed.
105
            // We don't bother checking if the word "new" is printed to screen
106
            // because that is unlikely to happen. We assume the use
107
            // of "new" is for object instantiation.
108
            if (strstr($string, ' new ') !== false) {
3✔
109
                $error = 'Do not use eval() to create objects dynamically; use reflection instead';
3✔
110
                $phpcsFile->addWarning($error, $stackPtr, 'Found');
3✔
111
            }
1✔
112
        }
1✔
113

114
    }//end process()
2✔
115

116

117
    /**
118
     * Provide the version number in which the sniff was deprecated.
119
     *
120
     * @return string
121
     */
122
    public function getDeprecationVersion()
×
123
    {
124
        return 'v3.9.0';
×
125

126
    }//end getDeprecationVersion()
127

128

129
    /**
130
     * Provide the version number in which the sniff will be removed.
131
     *
132
     * @return string
133
     */
134
    public function getRemovalVersion()
×
135
    {
136
        return 'v4.0.0';
×
137

138
    }//end getRemovalVersion()
139

140

141
    /**
142
     * Provide a custom message to display with the deprecation.
143
     *
144
     * @return string
145
     */
146
    public function getDeprecationMessage()
×
147
    {
148
        return 'The MySource standard will be removed completely in v4.0.0.';
×
149

150
    }//end getDeprecationMessage()
151

152

153
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc