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

PHPCSStandards / PHPCSExtra / 15639493787

13 Jun 2025 04:35PM UTC coverage: 97.39% (-2.5%) from 99.85%
15639493787

Pull #367

github

web-flow
Merge 844746ea8 into 10343591c
Pull Request #367: Update for compatibility with PHPCS 4.0

55 of 140 new or added lines in 8 files covered. (39.29%)

1 existing line in 1 file now uncovered.

3358 of 3448 relevant lines covered (97.39%)

3.58 hits per line

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

89.47
/Universal/Sniffs/PHP/NoFQNTrueFalseNullSniff.php
1
<?php
2
/**
3
 * PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
4
 *
5
 * @package   PHPCSExtra
6
 * @copyright 2020 PHPCSExtra Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCSStandards/PHPCSExtra
9
 */
10

11
namespace PHPCSExtra\Universal\Sniffs\PHP;
12

13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Sniffs\Sniff;
15
use PHP_CodeSniffer\Util\Tokens;
16

17
/**
18
 * Forbids the use of `true`/`false`/`null` as fully qualified constants.
19
 *
20
 * @since 1.3.0
21
 */
22
final class NoFQNTrueFalseNullSniff implements Sniff
23
{
24

25
    /**
26
     * Registers the tokens that this sniff wants to listen for.
27
     *
28
     * @since 1.3.0
29
     *
30
     * @return array<int|string>
31
     */
32
    public function register()
4✔
33
    {
34
        return [
2✔
35
            // PHPCS 3.x on PHP < 8.0.
36
            \T_TRUE,
4✔
37
            \T_FALSE,
4✔
38
            \T_NULL,
4✔
39

40
            // PHPCS 3.x on PHP >= 8.0.
41
            \T_STRING,
4✔
42

43
            // PHPCS 4.x.
44
            \T_NAME_FULLY_QUALIFIED,
4✔
45
        ];
4✔
46
    }
47

48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @since 1.3.0
52
     *
53
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
54
     * @param int                         $stackPtr  The position of the current token
55
     *                                               in the stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(File $phpcsFile, $stackPtr)
4✔
60
    {
61
        $tokens    = $phpcsFile->getTokens();
4✔
62
        $content   = $tokens[$stackPtr]['content'];
4✔
63
        $contentLC = \strtolower($content);
4✔
64

65
        if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
4✔
66
            // PHPCS 4.x.
NEW
67
            if ($contentLC !== '\true' && $contentLC !== '\false' && $contentLC !== '\null') {
×
NEW
68
                return;
×
69
            }
70
        } else {
71
            // PHPCS 3.x.
72
            if ($contentLC !== 'true' && $contentLC !== 'false' && $contentLC !== 'null') {
4✔
73
                return;
4✔
74
            }
75

76
            $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
4✔
77
            if ($tokens[$prev]['code'] !== \T_NS_SEPARATOR) {
4✔
78
                return;
4✔
79
            }
80

81
            $prevPrev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
4✔
82
            if ($tokens[$prevPrev]['code'] === \T_STRING || $tokens[$prevPrev]['code'] === \T_NAMESPACE) {
4✔
83
                return;
4✔
84
            }
85

86
            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
4✔
87
            if ($tokens[$next]['code'] === \T_NS_SEPARATOR) {
4✔
NEW
88
                return;
×
89
            }
90
        }
91

92
        $fix = $phpcsFile->addFixableError(
4✔
93
            'The special PHP constant "%s" should not be fully qualified.',
4✔
94
            $stackPtr,
4✔
95
            'Found',
4✔
96
            [$contentLC]
4✔
97
        );
4✔
98

99
        if ($fix === true) {
4✔
100
            if ($tokens[$stackPtr]['code'] === \T_NAME_FULLY_QUALIFIED) {
4✔
101
                // PHPCS 4.x.
NEW
102
                $phpcsFile->fixer->replaceToken($stackPtr, \ltrim($tokens[$stackPtr]['content'], '\\'));
×
103
            } else {
104
                // PHPCS 3.x.
105
                $phpcsFile->fixer->replaceToken($prev, '');
4✔
106
            }
107
        }
2✔
108
    }
2✔
109
}
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