Completed
Push — master ( cac8ef...37b486 )
by Juliette
19s
created

NewExceptionsFromToStringSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 76
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 36 6
1
<?php
2
/**
3
 * PHPCompatibility, an external standard for PHP_CodeSniffer.
4
 *
5
 * @package   PHPCompatibility
6
 * @copyright 2012-2019 PHPCompatibility Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCompatibility/PHPCompatibility
9
 */
10
11
namespace PHPCompatibility\Sniffs\FunctionDeclarations;
12
13
use PHPCompatibility\Sniff;
14
use PHP_CodeSniffer_File as File;
15
16
/**
17
 * As of PHP 7.4, throwing exceptions from a __toString() method is allowed.
18
 *
19
 * @link https://wiki.php.net/rfc/tostring_exceptions
20
 *
21
 * PHP version 7.4
22
 *
23
 * @since 9.2.0
24
 */
25
class NewExceptionsFromToStringSniff extends Sniff
26
{
27
28
    /**
29
     * Valid scopes for the __toString() method to live in.
30
     *
31
     * @since 9.2.0
32
     *
33
     * @var array
34
     */
35
    public $ooScopeTokens = array(
36
        'T_CLASS'      => true,
37
        'T_TRAIT'      => true,
38
        'T_ANON_CLASS' => true,
39
    );
40
41
    /**
42
     * Returns an array of tokens this test wants to listen for.
43
     *
44
     * @since 9.2.0
45
     *
46
     * @return array
47
     */
48
    public function register()
49
    {
50
        return array(\T_FUNCTION);
51
    }
52
53
    /**
54
     * Processes this test, when one of its tokens is encountered.
55
     *
56
     * @since 9.2.0
57
     *
58
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
59
     * @param int                   $stackPtr  The position of the current token
60
     *                                         in the stack passed in $tokens.
61
     *
62
     * @return void
63
     */
64
    public function process(File $phpcsFile, $stackPtr)
65
    {
66
        if ($this->supportsBelow('7.3') === false) {
67
            return;
68
        }
69
70
        $tokens = $phpcsFile->getTokens();
71
        if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
72
            // Abstract function, interface function, live coding or parse error.
73
            return;
74
        }
75
76
        $functionName = $phpcsFile->getDeclarationName($stackPtr);
77
        if (strtolower($functionName) !== '__tostring') {
78
            // Not the right function.
79
            return;
80
        }
81
82
        if ($this->validDirectScope($phpcsFile, $stackPtr, $this->ooScopeTokens) === false) {
83
            // Function, not method.
84
            return;
85
        }
86
87
        $hasThrow = $phpcsFile->findNext(\T_THROW, ($tokens[$stackPtr]['scope_opener'] + 1), $tokens[$stackPtr]['scope_closer']);
88
89
        if ($hasThrow === false) {
90
            // No exception is being thrown.
91
            return;
92
        }
93
94
        $phpcsFile->addError(
95
            'Throwing exceptions from __toString() was not allowed prior to PHP 7.4',
96
            $hasThrow,
97
            'Found'
98
        );
99
    }
100
}
101