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

keradus / PHP-CS-Fixer / 17319949156

29 Aug 2025 09:20AM UTC coverage: 94.696% (-0.05%) from 94.744%
17319949156

push

github

keradus
CS

28333 of 29920 relevant lines covered (94.7%)

45.63 hits per line

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

95.24
/src/Fixer/Operator/NoUselessNullsafeOperatorFixer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz RumiƄski <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Fixer\Operator;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\FixerDefinition\FixerDefinition;
19
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
20
use PhpCsFixer\FixerDefinition\VersionSpecification;
21
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
22
use PhpCsFixer\Tokenizer\Token;
23
use PhpCsFixer\Tokenizer\Tokens;
24

25
/**
26
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
27
 */
28
final class NoUselessNullsafeOperatorFixer extends AbstractFixer
29
{
30
    public function getDefinition(): FixerDefinitionInterface
31
    {
32
        return new FixerDefinition(
3✔
33
            'There should not be useless Null-safe operator `?->` used.',
3✔
34
            [
3✔
35
                new VersionSpecificCodeSample(
3✔
36
                    '<?php
3✔
37
class Foo extends Bar
38
{
39
    public function test() {
40
        echo $this?->parentMethod();
41
    }
42
}
43
',
3✔
44
                    new VersionSpecification(8_00_00)
3✔
45
                ),
3✔
46
            ]
3✔
47
        );
3✔
48
    }
49

50
    public function isCandidate(Tokens $tokens): bool
51
    {
52
        return \PHP_VERSION_ID >= 8_00_00 && $tokens->isAllTokenKindsFound([\T_VARIABLE, \T_NULLSAFE_OBJECT_OPERATOR]);
3✔
53
    }
54

55
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
56
    {
57
        for ($index = $tokens->count() - 1; $index >= 0; --$index) {
3✔
58
            if (!$tokens[$index]->isGivenKind(\T_NULLSAFE_OBJECT_OPERATOR)) {
3✔
59
                continue;
3✔
60
            }
61

62
            $nullsafeObjectOperatorIndex = $index;
3✔
63
            $index = $tokens->getPrevMeaningfulToken($index);
3✔
64

65
            if (!$tokens[$index]->isGivenKind(\T_VARIABLE)) {
3✔
66
                continue;
1✔
67
            }
68

69
            if ('$this' !== strtolower($tokens[$index]->getContent())) {
3✔
70
                continue;
×
71
            }
72

73
            $tokens[$nullsafeObjectOperatorIndex] = new Token([\T_OBJECT_OPERATOR, '->']);
3✔
74
        }
75
    }
76
}
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

© 2026 Coveralls, Inc