• 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

69.23
/src/Standards/MySource/Sniffs/Objects/AssignThisSniff.php
1
<?php
2
/**
3
 * Ensures this is not assigned to any other var but self.
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\Objects;
13

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

18
class AssignThisSniff implements Sniff, DeprecatedSniff
19
{
20

21
    /**
22
     * A list of tokenizers this sniff supports.
23
     *
24
     * @var array
25
     */
26
    public $supportedTokenizers = ['JS'];
27

28

29
    /**
30
     * Returns an array of tokens this test wants to listen for.
31
     *
32
     * @return array<int|string>
33
     */
34
    public function register()
3✔
35
    {
36
        return [T_THIS];
3✔
37

38
    }//end register()
39

40

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

54
        // Ignore this.something and other uses of "this" that are not
55
        // direct assignments.
56
        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
3✔
57
        if ($tokens[$next]['code'] !== T_SEMICOLON) {
3✔
58
            if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
3✔
59
                return;
3✔
60
            }
61
        }
1✔
62

63
        // Something must be assigned to "this".
64
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
3✔
65
        if ($tokens[$prev]['code'] !== T_EQUAL) {
3✔
66
            return;
×
67
        }
68

69
        // A variable needs to be assigned to "this".
70
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true);
3✔
71
        if ($tokens[$prev]['code'] !== T_STRING) {
3✔
72
            return;
×
73
        }
74

75
        // We can only assign "this" to a var called "self".
76
        if ($tokens[$prev]['content'] !== 'self' && $tokens[$prev]['content'] !== '_self') {
3✔
77
            $error = 'Keyword "this" can only be assigned to a variable called "self" or "_self"';
3✔
78
            $phpcsFile->addError($error, $prev, 'NotSelf');
3✔
79
        }
1✔
80

81
    }//end process()
2✔
82

83

84
    /**
85
     * Provide the version number in which the sniff was deprecated.
86
     *
87
     * @return string
88
     */
89
    public function getDeprecationVersion()
×
90
    {
91
        return 'v3.9.0';
×
92

93
    }//end getDeprecationVersion()
94

95

96
    /**
97
     * Provide the version number in which the sniff will be removed.
98
     *
99
     * @return string
100
     */
101
    public function getRemovalVersion()
×
102
    {
103
        return 'v4.0.0';
×
104

105
    }//end getRemovalVersion()
106

107

108
    /**
109
     * Provide a custom message to display with the deprecation.
110
     *
111
     * @return string
112
     */
113
    public function getDeprecationMessage()
×
114
    {
115
        return 'The MySource standard will be removed completely in v4.0.0.';
×
116

117
    }//end getDeprecationMessage()
118

119

120
}//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