/
ReturnTypeSniff.php
106 lines (89 loc) · 2.92 KB
/
ReturnTypeSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* This file is part of the Symfony-coding-standard (phpcs standard)
*
* PHP version 5
*
* @category PHP
* @package Symfony-coding-standard
* @author Authors <Symfony-coding-standard@djoos.github.com>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/djoos/Symfony-coding-standard
*/
namespace Symfony\Sniffs\Functions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Checks whether a function returns the appropriate return type
*
* @category PHP
* @package Symfony-coding-standard
* @author wicliff wolda <wicliff.wolda@gmail.com>
* @license http://spdx.org/licenses/MIT MIT License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ReturnTypeSniff implements Sniff
{
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
return [
T_FUNCTION,
];
}
/**
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
* pointer is reached. Return (count($tokens) + 1) to skip
* the rest of the file.
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$methodProperties = $phpcsFile->getMethodProperties($stackPtr);
$type = $methodProperties['return_type_token'];
if (false === $type || 'void' !== strtolower($tokens[$type]['content'])) {
return;
}
$next = $stackPtr;
do {
if (!isset($tokens[$stackPtr]['scope_closer'])) {
break;
}
$next = $phpcsFile->findNext(
T_RETURN,
$next + 1,
$tokens[$stackPtr]['scope_closer']
);
if (false === $next) {
break;
}
$conditions = $tokens[$next]['conditions'];
$lastScope = key(array_slice($conditions, -1, null, true));
if ($stackPtr !== $lastScope) {
continue;
}
if (T_SEMICOLON !== $tokens[$next + 1]['code']) {
$error = 'Use return null; when a function explicitly ';
$error .= 'returns null values and use return; ';
$error .= 'when the function returns void values';
$phpcsFile->addError(
$error,
$next,
'Invalid'
);
}
} while (true);
}
}