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

webimpress / coding-standard / 4086684577

pending completion
4086684577

Pull #178

github

GitHub
Merge 75aa3b533 into 18aa29088
Pull Request #178: Bump phpunit/phpunit from 9.5.20 to 9.6.0

6985 of 6999 relevant lines covered (99.8%)

1.13 hits per line

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

91.3
/src/WebimpressCodingStandard/Sniffs/NamingConventions/ExceptionSniff.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace WebimpressCodingStandard\Sniffs\NamingConventions;
6

7
use PHP_CodeSniffer\Files\File;
8
use PHP_CodeSniffer\Sniffs\Sniff;
9
use ReflectionClass;
10
use Throwable;
11
use WebimpressCodingStandard\Helper\NamingTrait;
12

13
use function class_exists;
14

15
use const T_CLASS;
16
use const T_NAMESPACE;
17
use const T_NS_SEPARATOR;
18
use const T_OPEN_CURLY_BRACKET;
19
use const T_SEMICOLON;
20
use const T_STRING;
21

22
class ExceptionSniff implements Sniff
23
{
24
    use NamingTrait;
25

26
    /**
27
     * @var string
28
     */
29
    public $prefix = '';
30

31
    /**
32
     * @var string
33
     */
34
    public $suffix = 'Exception';
35

36
    /**
37
     * @return int[]
38
     */
39
    public function register()
40
    {
41
        return [T_CLASS];
1✔
42
    }
43

44
    /**
45
     * @param int $stackPtr
46
     */
47
    public function process(File $phpcsFile, $stackPtr)
48
    {
49
        $tokens = $phpcsFile->getTokens();
1✔
50
        $name = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
1✔
51
        $string = $tokens[$name]['content'];
1✔
52

53
        $fqn = $this->getNamespace($phpcsFile, $stackPtr) . '\\' . $string;
1✔
54

55
        try {
56
            if (! class_exists($fqn)) {
1✔
57
                return;
1✔
58
            }
59
        } catch (Throwable $e) {
×
60
            // Unable to load class for some reason - non existing parent class?
61
            return;
×
62
        }
63

64
        $reflection = new ReflectionClass($fqn);
1✔
65
        if (! $reflection->isSubclassOf(Throwable::class)) {
1✔
66
            return;
1✔
67
        }
68

69
        $this->check($phpcsFile, $name, 'Exception class');
1✔
70
    }
71

72
    private function getNamespace(File $phpcsFile, int $stackPtr) : string
73
    {
74
        $namespace = '';
1✔
75
        if ($i = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr - 1)) {
1✔
76
            $tokens = $phpcsFile->getTokens();
1✔
77
            while (++$i) {
1✔
78
                switch ($tokens[$i]['code']) {
1✔
79
                    case T_SEMICOLON:
80
                    case T_OPEN_CURLY_BRACKET:
81
                        break 2;
1✔
82
                    case T_STRING:
83
                    case T_NS_SEPARATOR:
1✔
84
                        $namespace .= $tokens[$i]['content'];
1✔
85
                        break;
1✔
86
                }
87
            }
88
        }
89

90
        return $namespace;
1✔
91
    }
92
}
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