Completed
Push — php-7.0/new-generator-return-e... ( 92f48b )
by Juliette
02:57
created

NewGeneratorReturnSniff::process()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 38
Code Lines 19

Duplication

Lines 4
Ratio 10.53 %

Importance

Changes 0
Metric Value
dl 4
loc 38
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 9
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\NewGeneratorReturnSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\NewGeneratorReturnSniff.
18
 *
19
 * As of PHP 7.0, a return statement can be used within a generator for a final expression to be returned.
20
 *
21
 * PHP version 7.0
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Juliette Reinders Folmer <[email protected]>
26
 */
27
class NewGeneratorReturnSniff extends Sniff
28
{
29
30
    /**
31
     * Returns an array of tokens this test wants to listen for.
32
     *
33
     * @return array
34
     */
35
    public function register()
36
    {
37
        return array(
38
            // phpcs:ignore PHPCompatibility.PHP.NewConstants.t_yieldFound - PHPCS backfills the token.
39
            T_YIELD,
40
            T_YIELD_FROM,
41
        );
42
    }
43
44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
     * @param int                   $stackPtr  The position of the current token in the
49
     *                                         stack passed in $tokens.
50
     *
51
     * @return void|int Void or a stack pointer to skip forward.
52
     */
53
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        if ($this->supportsBelow('5.6') !== true) {
56
            return;
57
        }
58
59
        $function = $phpcsFile->getCondition($stackPtr, T_FUNCTION);
60
        if ($function === false) {
61
            // Try again, but now for a closure.
62
            $function = $phpcsFile->getCondition($stackPtr, T_CLOSURE);
63
        }
64
65
        if ($function === false) {
66
            // Yield outside function scope, fatal error, but not our concern.
67
            return;
68
        }
69
70
        $tokens = $phpcsFile->getTokens();
71
72 View Code Duplication
        if (isset($tokens[$function]['scope_opener'], $tokens[$function]['scope_closer']) === false) {
73
            // Can't reliably determine start/end of function scope.
74
            return;
75
        }
76
77
        $hasReturn = $phpcsFile->findNext(T_RETURN, ($tokens[$function]['scope_opener'] + 1), $tokens[$function]['scope_closer']);
78
        if ($hasReturn === false) {
79
            return;
80
        }
81
82
        $phpcsFile->addError(
83
            'Returning a final expression from a generator was not supported in PHP 5.6 or earlier',
84
            $hasReturn,
85
            'ReturnFound'
86
        );
87
88
        // Don't examine this function again.
89
        return $tokens[$function]['scope_closer'];
90
    }
91
}
92