Skip to content

#21: Impelement rule #77

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

Merged
merged 9 commits into from
Aug 16, 2019
79 changes: 79 additions & 0 deletions Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento. 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;

/**
* Detects missing try-catch block when processing system resources.
*/
class TryProcessSystemResourcesSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'The code must be wrapped with a try block if the method uses system resources.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'MissingTryCatch';

/**
* Search for functions that start with.
*
* @var array
*/
protected $functions = [
'stream_',
'socket_',
];

/**
* @inheritDoc
*/
public function register()
{
return [T_STRING];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

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

if ($tryPosition !== false) {
$tryTag = $tokens[$tryPosition];
$start = $tryTag['scope_opener'];
$end = $tryTag['scope_closer'];
if ($stackPtr > $start && $stackPtr < $end) {
// element is warped by try no check required
return;
}
}

$phpcsFile->addWarning(
$this->warningMessage,
$stackPtr,
$this->warningCode
);
}
}
}
29 changes: 29 additions & 0 deletions Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Foo\Bar;

use Exception;

class StreamHandler
{
public function handleException()
{
try {
$strChar = stream_get_contents(STDIN, 1);
} catch (Exception $exception) {

}
}
}

function executeStream()
{
// Rule find: Try block detected when processing system resources
$strChar = stream_get_contents(STDIN, 1);
// Rule find: Try block detected when processing system resources
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Rule find: Try block detected when processing system resources
socket_bind($sock);
// Rule find: Try block detected when processing system resources
socket_close($sock);
}
35 changes: 35 additions & 0 deletions Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Exceptions;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Class ThrowCatchUnitTest
*/
class TryProcessSystemResourcesUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
protected function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
protected function getWarningList()
{
return [
22 => 1,
24 => 1,
26 => 1,
28 => 1
];
}
}
8 changes: 8 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Exceptions.TryProcessSystemResources">
<severity>8</severity>
<type>warning</type>
<exclude-pattern>*/_files/*</exclude-pattern>
<exclude-pattern>*/Fixtures/*</exclude-pattern>
<exclude-pattern>*/Test/*</exclude-pattern>
<exclude-pattern>*Test.php</exclude-pattern>
</rule>
<rule ref="Magento2.Functions.DiscouragedFunction">
<severity>8</severity>
<type>warning</type>
Expand Down