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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
93 changes: 93 additions & 0 deletions Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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 no try block detected when processing system resources
*/
class TryProcessSystemResourcesSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'All resources SHOULD be properly released.';

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

/**
* Searched functions.
*
* @var array
*/
protected $functions = [
'stream_',
'socket_',
'curl_'
];

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

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$isSystemResource = false;

$tokens = $phpcsFile->getTokens();
$match = $tokens[$stackPtr]['content'];

foreach ($this->functions as $function) {
if (strpos($match, $function) === false) {
continue;
}

$isSystemResource = true;
break;
}

if (false === $isSystemResource) {
// Probably no a system resource no check
return;
}

$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
);
}
}
45 changes: 45 additions & 0 deletions Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Foo\Bar;

use Exception;

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

}
}

public function exetcute()
{
// Rule find: Try block detected when processing system resources
$strChar = stream_get_contents(STDIN, 1);

try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
} 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);
}
36 changes: 36 additions & 0 deletions Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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 [
21 => 1,
38 => 1,
40 => 1,
42 => 1,
44 => 1
];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Exceptions.TryProcessSystemResources">
<severity>7</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Functions.DiscouragedFunction">
<severity>8</severity>
<type>warning</type>
Expand Down
4 changes: 4 additions & 0 deletions output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR: The specified sniff code "Magento2.Exceptions" is invalid

Run "phpcs --help" for usage information