-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/php/PHPMD/Rule/CleanCode/ErrorControlOperator.php
This file contains 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,49 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Rule\CleanCode; | ||
|
||
use PHPMD\AbstractNode; | ||
use PHPMD\AbstractRule; | ||
use PHPMD\Rule\ClassAware; | ||
use PHPMD\Rule\FunctionAware; | ||
use PHPMD\Rule\MethodAware; | ||
|
||
/** | ||
* Error Control Operators Rule | ||
* | ||
* This rule detects usage of error control operator (@). | ||
* | ||
* @author Kamil Szymanaski <kamil.szymanski@gmail.com> | ||
*/ | ||
class ErrorControlOperator extends AbstractRule implements MethodAware, FunctionAware, ClassAware | ||
{ | ||
/** | ||
* Loops trough all class or function nodes and looks for '@' sign. | ||
* | ||
* @param AbstractNode $node | ||
* @return void | ||
*/ | ||
public function apply(AbstractNode $node) | ||
{ | ||
foreach ($node->findChildrenOfType('UnaryExpression') as $unaryExpression) { | ||
if ($unaryExpression->getImage() === '@') { | ||
$this->addViolation($node, array($unaryExpression->getBeginLine())); | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/php/PHPMD/Rule/CleanCode/ErrorControlOperatorTest.php
This file contains 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,47 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Rule\CleanCode; | ||
|
||
use PHPMD\AbstractTest; | ||
|
||
class ErrorControlOperatorTest extends AbstractTest | ||
{ | ||
/** | ||
* testAppliedToFunctions | ||
* | ||
* @return void | ||
*/ | ||
public function testAppliedToFunctions() | ||
{ | ||
$rule = new ErrorControlOperator(); | ||
$rule->setReport($this->getReportMock(3)); | ||
$rule->apply($this->getFunction()); | ||
} | ||
|
||
/** | ||
* testAppliedToClassesAndMethods | ||
* | ||
* @return void | ||
*/ | ||
public function testAppliedToClassesAndMethods() | ||
{ | ||
$rule = new ErrorControlOperator(); | ||
$rule->setReport($this->getReportMock(6)); | ||
$rule->apply($this->getClass()); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...st/resources/files/Rule/CleanCode/ErrorControlOperator/testAppliedToClassesAndMethods.php
This file contains 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,51 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testAppliedToClassesAndMethods | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $baz = 'baz'; | ||
|
||
/** | ||
* @foo to be skipped | ||
*/ | ||
public function testAppliedToClassesAndMethods() | ||
{ | ||
$foo = @$this->fooBar(); | ||
++$foo; | ||
@!$foo = 1 / 0; | ||
if (@is_readable(__FILE__)) { | ||
$bar = new DateTime('now'); | ||
@$baz = $bar; | ||
} | ||
$bar = !$foo; | ||
} | ||
|
||
/** | ||
* @$foo to be skipped | ||
*/ | ||
private function fooBar() | ||
{ | ||
/** ++$i shouldn't work */ | ||
@$foo = $this->baz / 0; | ||
@$baz = !$foo; | ||
|
||
return 2; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/resources/files/Rule/CleanCode/ErrorControlOperator/testAppliedToFunctions.php
This file contains 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,27 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
function testAppliedToFunctions() | ||
{ | ||
@$foo = debug_backtrace(); | ||
|
||
@file_exists(__FILE__); | ||
|
||
++$foo; | ||
!$foo; | ||
@$average = 3 / 0; | ||
} |