Skip to content

Commit 7155434

Browse files
author
Lars Roettig
committed
#21: Impelement rule
1 parent b79055f commit 7155434

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento2\Sniffs\Exceptions;
8+
9+
use function array_slice;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
/**
14+
* Detects no try block detected when processing system resources
15+
*/
16+
class TryProcessSystemResourcesSniff implements Sniff
17+
{
18+
/**
19+
* String representation of warning.
20+
*
21+
* @var string
22+
*/
23+
protected $warningMessage = 'All resources SHOULD be properly released.';
24+
25+
/**
26+
* Warning violation code.
27+
*
28+
* @var string
29+
*/
30+
protected $warningCode = ' TryProcessSystem';
31+
32+
/**
33+
* Searched functions.
34+
*
35+
* @var array
36+
*/
37+
protected $functions = [
38+
'stream_',
39+
'socket_',
40+
'curl_'
41+
];
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function register()
47+
{
48+
return [T_STRING];
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
public function process(File $phpcsFile, $stackPtr)
55+
{
56+
$isSystemResource = false;
57+
58+
$tokens = $phpcsFile->getTokens();
59+
$match = $tokens[$stackPtr]['content'];
60+
61+
foreach ($this->functions as $function) {
62+
if (strpos($match, $function) === false) {
63+
continue;
64+
}
65+
66+
$isSystemResource = true;
67+
break;
68+
}
69+
70+
if (false === $isSystemResource) {
71+
// Probably no a system resource no check
72+
return;
73+
}
74+
75+
$tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1);
76+
77+
if ($tryPosition !== false) {
78+
$tryTag = $tokens[$tryPosition];
79+
$start = $tryTag['scope_opener'];
80+
$end = $tryTag['scope_closer'];
81+
if ($stackPtr > $start && $stackPtr < $end) {
82+
// element is warped by try no check required
83+
return;
84+
}
85+
}
86+
87+
$phpcsFile->addWarning(
88+
$this->warningMessage,
89+
$stackPtr,
90+
$this->warningCode
91+
);
92+
}
93+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Foo\Bar;
4+
5+
use Exception;
6+
7+
class StreamHandler
8+
{
9+
public function handleExpection()
10+
{
11+
try {
12+
$strChar = stream_get_contents(STDIN, 1);
13+
} catch (Exception $exception) {
14+
15+
}
16+
}
17+
18+
public function exetcute()
19+
{
20+
// Rule find: Try block detected when processing system resources
21+
$strChar = stream_get_contents(STDIN, 1);
22+
23+
try {
24+
$ch = curl_init();
25+
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
26+
curl_setopt($ch, CURLOPT_HEADER, 0);
27+
curl_exec($ch);
28+
curl_close($ch);
29+
} catch (Exception $exception) {
30+
31+
}
32+
}
33+
}
34+
35+
function executeStream()
36+
{
37+
// Rule find: Try block detected when processing system resources
38+
$strChar = stream_get_contents(STDIN, 1);
39+
// Rule find: Try block detected when processing system resources
40+
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
41+
// Rule find: Try block detected when processing system resources
42+
socket_bind($sock);
43+
// Rule find: Try block detected when processing system resources
44+
socket_close($sock);
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Exceptions;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class ThrowCatchUnitTest
12+
*/
13+
class TryProcessSystemResourcesUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
protected function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected function getWarningList()
27+
{
28+
return [
29+
21 => 1,
30+
38 => 1,
31+
40 => 1,
32+
42 => 1,
33+
44 => 1
34+
];
35+
}
36+
}

Magento2/ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
125125
<severity>8</severity>
126126
<type>warning</type>
127127
</rule>
128+
<rule ref="Magento2.Exceptions.TryProcessSystemResources">
129+
<severity>7</severity>
130+
<type>warning</type>
131+
</rule>
128132
<rule ref="Magento2.Functions.DiscouragedFunction">
129133
<severity>8</severity>
130134
<type>warning</type>

output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ERROR: The specified sniff code "Magento2.Exceptions" is invalid
2+
3+
Run "phpcs --help" for usage information
4+

0 commit comments

Comments
 (0)