-
Notifications
You must be signed in to change notification settings - Fork 160
#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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7155434
#21: Impelement rule
373fe13
Merge remote-tracking branch 'origin/develop' into 21-system-resources
d49b390
#21 Review Fixes
b18cd92
Merge branch 'develop' into 21-system-resources
lenaorobei 5f8ca0e
#21 Requested code review chnages
94684c7
Merge branch 'develop' into 21-system-resources
859e563
magento/magento-coding-standard#21: [New Rule] No try block detected …
lenaorobei d3cacbe
Merge branch 'develop' of github.com:magento/magento-coding-standard …
lenaorobei 8a7ff83
magento/magento-coding-standard#21: [New Rule] No try block detected …
lenaorobei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; | ||
larsroettig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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
45
Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
larsroettig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// 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
36
Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ERROR: The specified sniff code "Magento2.Exceptions" is invalid | ||
larsroettig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Run "phpcs --help" for usage information | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.