Skip to content

Commit 53ea429

Browse files
committed
Avoid false positive with 'use function ...' lines
1 parent 7521587 commit 53ea429

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
namespace Magento2\Sniffs\Exceptions;
79

8-
use function array_slice;
9-
use PHP_CodeSniffer\Sniffs\Sniff;
1010
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use PHP_CodeSniffer\Util\Tokens;
1113

1214
/**
1315
* Detects missing try-catch block when processing system resources.
@@ -43,7 +45,10 @@ class TryProcessSystemResourcesSniff implements Sniff
4345
*/
4446
public function register()
4547
{
46-
return [T_STRING];
48+
return [
49+
\T_USE,
50+
\T_STRING,
51+
];
4752
}
4853

4954
/**
@@ -53,10 +58,30 @@ public function process(File $phpcsFile, $stackPtr)
5358
{
5459
$tokens = $phpcsFile->getTokens();
5560

61+
if ($tokens[$stackPtr]['code'] === \T_USE) {
62+
$previousToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
63+
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
64+
$semicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr + 1);
65+
if ($previousToken !== false
66+
&& \in_array($tokens[$previousToken]['code'], [\T_OPEN_TAG, \T_SEMICOLON], true)
67+
&& $nextToken !== false
68+
&& $tokens[$nextToken]['code'] === \T_STRING
69+
&& $tokens[$nextToken]['content'] === 'function'
70+
&& $semicolon !== false
71+
) {
72+
// We have found a 'use function ...' statement; skip over this.
73+
return $semicolon;
74+
}
75+
76+
// This is not a 'use function ...' statement.
77+
return;
78+
}
79+
5680
foreach ($this->functions as $function) {
5781
if (strpos($tokens[$stackPtr]['content'], $function) !== 0) {
5882
continue;
5983
}
84+
6085
$tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1);
6186

6287
if ($tryPosition !== false) {

Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Foo\Bar;
44

55
use Exception;
66

7+
use function stream_get_contents;
8+
79
class StreamHandler
810
{
911
public function handleException()
@@ -26,4 +28,8 @@ function executeStream()
2628
socket_bind($sock);
2729
// Rule find: Try block detected when processing system resources
2830
socket_close($sock);
31+
32+
$func = function () use (&$strChar) {
33+
$strChar = stream_get_contents(STDIN, 1);
34+
}
2935
}

Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ protected function getErrorList()
2626
protected function getWarningList()
2727
{
2828
return [
29-
22 => 1,
3029
24 => 1,
3130
26 => 1,
32-
28 => 1
31+
28 => 1,
32+
30 => 1,
33+
33 => 1,
3334
];
3435
}
3536
}

0 commit comments

Comments
 (0)