Skip to content

Commit 7663364

Browse files
author
Vincent Langlet
committed
✨ Add Error/Deprecated messages sniffs
1 parent 8e3b218 commit 7663364

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Sniffs\Errors;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Checks whether exception messages are concatenated with sprintf
10+
*/
11+
class ExceptionMessageSniff implements Sniff
12+
{
13+
/**
14+
* Registers the tokens that this sniff wants to listen for.
15+
*
16+
* @return array
17+
*/
18+
public function register()
19+
{
20+
return array(
21+
T_THROW,
22+
);
23+
}
24+
25+
/**
26+
* Called when one of the token types that this sniff is listening for is found.
27+
*
28+
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
29+
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack
30+
* where the token was found.
31+
*
32+
* @return void|int Optionally returns a stack pointer. The sniff will not be
33+
* called again on the current file until the returned stack
34+
* pointer is reached. Return (count($tokens) + 1) to skip
35+
* the rest of the file.
36+
*/
37+
public function process(File $phpcsFile, $stackPtr)
38+
{
39+
$tokens = $phpcsFile->getTokens();
40+
$opener = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
41+
$concat = $phpcsFile->findNext(
42+
T_STRING_CONCAT,
43+
$tokens[$opener]['parenthesis_opener'],
44+
$tokens[$opener]['parenthesis_closer']
45+
);
46+
47+
if ($concat) {
48+
$error = 'Exception and error message strings must be concatenated using sprintf';
49+
$phpcsFile->addError($error, $stackPtr, 'Invalid');
50+
}
51+
}
52+
53+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Sniffs\Errors;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Checks whether E_USER_DEPRECATED errors are silenced by default.
10+
*/
11+
class UserDeprecatedSniff implements Sniff
12+
{
13+
/**
14+
* Registers the tokens that this sniff wants to listen for.
15+
*
16+
* @return array
17+
*/
18+
public function register()
19+
{
20+
return array(
21+
T_STRING,
22+
);
23+
}
24+
25+
/**
26+
* Called when one of the token types that this sniff is listening for is found.
27+
*
28+
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
29+
* @param int $stackPtr The position in the PHP_CodeSniffer file's token
30+
* stack where the token was found.
31+
*
32+
* @return void|int Optionally returns a stack pointer. The sniff will not be
33+
* called again on the current file until the returned stack
34+
* pointer is reached. Return (count($tokens) + 1) to skip
35+
* the rest of the file.
36+
*/
37+
public function process(File $phpcsFile, $stackPtr)
38+
{
39+
$tokens = $phpcsFile->getTokens();
40+
41+
if ('trigger_error' !== $tokens[$stackPtr]['content']) {
42+
return;
43+
}
44+
45+
$pos = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
46+
$opener = $tokens[$pos]['parenthesis_opener'];
47+
$closer = $tokens[$pos]['parenthesis_closer'];
48+
49+
do {
50+
$string = $phpcsFile->findNext(T_STRING, $opener, $closer);
51+
52+
if (false === $string) {
53+
break;
54+
}
55+
56+
if ('E_USER_DEPRECATED' === $tokens[$string]['content'] && '@' !== $tokens[$stackPtr - 1]['content']) {
57+
$error = 'Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator';
58+
$phpcsFile->addError($error, $stackPtr, 'Invalid');
59+
60+
break;
61+
} else {
62+
$opener = $string + 1;
63+
}
64+
} while ($opener < $closer);
65+
}
66+
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class Errors
4+
{
5+
/**
6+
* @param bool $bool
7+
* @param string $option
8+
*
9+
* @throws \RuntimeException
10+
*/
11+
public function test($bool, $option)
12+
{
13+
if ($bool) {
14+
throw new \RuntimeException(sprintf('Unrecognized option "%s"', $option));
15+
}
16+
17+
throw new \RuntimeException('Unrecognized option '.$option);
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Tests\Errors;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the ExceptionMessage sniff.
9+
*
10+
* @group Symfony3Custom
11+
*/
12+
class ExceptionMessageUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @return array<int, int>
21+
*/
22+
public function getErrorList()
23+
{
24+
return array(
25+
17 => 1,
26+
);
27+
}
28+
29+
/**
30+
* Returns the lines where warnings should occur.
31+
*
32+
* The key of the array should represent the line number and the value
33+
* should represent the number of errors that should occur on that line.
34+
*
35+
* @return array(int => int)
36+
*/
37+
public function getWarningList()
38+
{
39+
return array();
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class Errors
4+
{
5+
/**
6+
* @return string
7+
*
8+
* @deprecated
9+
*/
10+
public function someGoodDeprecatedMethod()
11+
{
12+
@trigger_error(
13+
sprintf(
14+
'The %s() method is deprecated',
15+
__METHOD__
16+
),
17+
E_USER_DEPRECATED
18+
);
19+
20+
return Baz::someMethod();
21+
}
22+
23+
/**
24+
* @return string
25+
*
26+
* @deprecated
27+
*/
28+
public function someBadDeprecatedMethod()
29+
{
30+
trigger_error(
31+
sprintf(
32+
'The %s() method is deprecated',
33+
__METHOD__
34+
),
35+
E_USER_DEPRECATED
36+
);
37+
38+
return Baz::someMethod();
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony3Custom\Tests\Errors;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
/**
8+
* Unit test class for the UserDeprecated sniff.
9+
*
10+
* @group Symfony3Custom
11+
*/
12+
class UserDeprecatedUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* Returns the lines where errors should occur.
16+
*
17+
* The key of the array should represent the line number and the value
18+
* should represent the number of errors that should occur on that line.
19+
*
20+
* @return array<int, int>
21+
*/
22+
public function getErrorList()
23+
{
24+
return array(
25+
30 => 1,
26+
);
27+
}
28+
29+
/**
30+
* Returns the lines where warnings should occur.
31+
*
32+
* The key of the array should represent the line number and the value
33+
* should represent the number of errors that should occur on that line.
34+
*
35+
* @return array(int => int)
36+
*/
37+
public function getWarningList()
38+
{
39+
return array();
40+
}
41+
}

0 commit comments

Comments
 (0)