Skip to content

Commit f14d045

Browse files
authored
Merge pull request magento#77 from magento/21-system-resources
magento#21: Impelement rule
2 parents 925b97b + 8a7ff83 commit f14d045

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Exceptions;
7+
8+
use function array_slice;
9+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
use PHP_CodeSniffer\Files\File;
11+
12+
/**
13+
* Detects missing try-catch block when processing system resources.
14+
*/
15+
class TryProcessSystemResourcesSniff implements Sniff
16+
{
17+
/**
18+
* String representation of warning.
19+
*
20+
* @var string
21+
*/
22+
protected $warningMessage = 'The code must be wrapped with a try block if the method uses system resources.';
23+
24+
/**
25+
* Warning violation code.
26+
*
27+
* @var string
28+
*/
29+
protected $warningCode = 'MissingTryCatch';
30+
31+
/**
32+
* Search for functions that start with.
33+
*
34+
* @var array
35+
*/
36+
protected $functions = [
37+
'stream_',
38+
'socket_',
39+
];
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function register()
45+
{
46+
return [T_STRING];
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function process(File $phpcsFile, $stackPtr)
53+
{
54+
$tokens = $phpcsFile->getTokens();
55+
56+
foreach ($this->functions as $function) {
57+
if (strpos($tokens[$stackPtr]['content'], $function) !== 0) {
58+
continue;
59+
}
60+
$tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1);
61+
62+
if ($tryPosition !== false) {
63+
$tryTag = $tokens[$tryPosition];
64+
$start = $tryTag['scope_opener'];
65+
$end = $tryTag['scope_closer'];
66+
if ($stackPtr > $start && $stackPtr < $end) {
67+
// element is warped by try no check required
68+
return;
69+
}
70+
}
71+
72+
$phpcsFile->addWarning(
73+
$this->warningMessage,
74+
$stackPtr,
75+
$this->warningCode
76+
);
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Foo\Bar;
4+
5+
use Exception;
6+
7+
class StreamHandler
8+
{
9+
public function handleException()
10+
{
11+
try {
12+
$strChar = stream_get_contents(STDIN, 1);
13+
} catch (Exception $exception) {
14+
15+
}
16+
}
17+
}
18+
19+
function executeStream()
20+
{
21+
// Rule find: Try block detected when processing system resources
22+
$strChar = stream_get_contents(STDIN, 1);
23+
// Rule find: Try block detected when processing system resources
24+
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
25+
// Rule find: Try block detected when processing system resources
26+
socket_bind($sock);
27+
// Rule find: Try block detected when processing system resources
28+
socket_close($sock);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
22 => 1,
30+
24 => 1,
31+
26 => 1,
32+
28 => 1
33+
];
34+
}
35+
}

Magento2/ruleset.xml

+8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@
153153
<severity>8</severity>
154154
<type>warning</type>
155155
</rule>
156+
<rule ref="Magento2.Exceptions.TryProcessSystemResources">
157+
<severity>8</severity>
158+
<type>warning</type>
159+
<exclude-pattern>*/_files/*</exclude-pattern>
160+
<exclude-pattern>*/Fixtures/*</exclude-pattern>
161+
<exclude-pattern>*/Test/*</exclude-pattern>
162+
<exclude-pattern>*Test.php</exclude-pattern>
163+
</rule>
156164
<rule ref="Magento2.Functions.DiscouragedFunction">
157165
<severity>8</severity>
158166
<type>warning</type>

0 commit comments

Comments
 (0)