Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid false positive with 'use function ...' lines #478

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Exceptions;

use function array_slice;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

/**
* Detects missing try-catch block when processing system resources.
@@ -43,7 +45,10 @@ class TryProcessSystemResourcesSniff implements Sniff
*/
public function register()
{
return [T_STRING];
return [
\T_USE,
\T_STRING,
];
}

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

if ($tokens[$stackPtr]['code'] === \T_USE) {
$previousToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
$semicolon = $phpcsFile->findNext(T_SEMICOLON, $stackPtr + 1);
if ($previousToken !== false
&& \in_array($tokens[$previousToken]['code'], [\T_OPEN_TAG, \T_SEMICOLON], true)
&& $nextToken !== false
&& $tokens[$nextToken]['code'] === \T_STRING
&& $tokens[$nextToken]['content'] === 'function'
&& $semicolon !== false
) {
// We have found a 'use function ...' statement; skip over this.
return $semicolon;
}

// This is not a 'use function ...' statement.
return;
}

foreach ($this->functions as $function) {
if (strpos($tokens[$stackPtr]['content'], $function) !== 0) {
continue;
}

$tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1);

if ($tryPosition !== false) {
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ namespace Foo\Bar;

use Exception;

use function stream_get_contents;

class StreamHandler
{
public function handleException()
@@ -26,4 +28,8 @@ function executeStream()
socket_bind($sock);
// Rule find: Try block detected when processing system resources
socket_close($sock);

$func = function () use (&$strChar) {
$strChar = stream_get_contents(STDIN, 1);
}
}
Original file line number Diff line number Diff line change
@@ -26,10 +26,11 @@ protected function getErrorList()
protected function getWarningList()
{
return [
22 => 1,
24 => 1,
26 => 1,
28 => 1
28 => 1,
30 => 1,
33 => 1,
];
}
}
Loading
Oops, something went wrong.