-
-
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.
Merge pull request #496 from eeree/issue-62
Add rule for PHP's @ operator
- Loading branch information
Showing
9 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
<?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> | ||
* @link http://php.net/manual/en/language.operators.errorcontrol.php | ||
*/ | ||
class ErrorControlOperator extends AbstractRule implements MethodAware, FunctionAware | ||
{ | ||
/** | ||
* 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())); | ||
} | ||
} | ||
} | ||
} |
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
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
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
67 changes: 67 additions & 0 deletions
67
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,67 @@ | ||
<?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; | ||
|
||
/** | ||
* Error Control Operator Test | ||
* | ||
* @coversDefaultClass \PHPMD\Rule\CleanCode\ErrorControlOperator | ||
*/ | ||
class ErrorControlOperatorTest extends AbstractTest | ||
{ | ||
/** | ||
* Tests that the rule does not apply to unary operators in functions | ||
* | ||
* @return void | ||
* @covers ::apply | ||
*/ | ||
public function testDoesNotApplyToOtherUnaryOperatorsInFunction() | ||
{ | ||
$rule = new ErrorControlOperator(); | ||
$rule->setReport($this->getReportWithOneViolation()); | ||
$rule->apply($this->getFunction()); | ||
} | ||
|
||
/** | ||
* Tests that the rule applies error control operators to functions | ||
* | ||
* @return void | ||
* @covers ::apply | ||
*/ | ||
public function testAppliesToErrorControlOperatorInFunction() | ||
{ | ||
$rule = new ErrorControlOperator(); | ||
$rule->setReport($this->getReportMock(3)); | ||
$rule->apply($this->getFunction()); | ||
} | ||
|
||
/** | ||
* Tests that the rule applies error control operators to classes and methods | ||
* | ||
* @return void | ||
* @covers ::apply | ||
*/ | ||
public function testAppliedToClassesAndMethods() | ||
{ | ||
$rule = new ErrorControlOperator(); | ||
$rule->setReport($this->getReportMock(6)); | ||
$rule->apply($this->getClass()); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...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,55 @@ | ||
<?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 | ||
*/ | ||
class testAppliedToClassesAndMethods | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $baz = 'baz'; | ||
|
||
/** | ||
* testAppliedToClassesAndMethods | ||
*/ | ||
public function testAppliedToClassesAndMethods() | ||
{ | ||
$foo = @$this->fooBar(); | ||
++$foo; | ||
@!$baz = 1 / 0; | ||
if (@is_readable(__FILE__)) { | ||
$bar = new DateTime('now'); | ||
@$baz = $bar; | ||
} | ||
$this->baz = !$foo; | ||
} | ||
|
||
/** | ||
* fooBar | ||
* | ||
* @return int | ||
*/ | ||
private function fooBar() | ||
{ | ||
@$foo = $this->baz / 0; | ||
@$baz = !$foo; | ||
|
||
return 2; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...files/Rule/CleanCode/ErrorControlOperator/testAppliesToErrorControlOperatorInFunction.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,30 @@ | ||
<?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/ | ||
*/ | ||
|
||
/** | ||
* testAppliesToErrorControlOperatorInFunction | ||
* | ||
* @return array | ||
*/ | ||
function testAppliesToErrorControlOperatorInFunction() | ||
{ | ||
$foo = @debug_backtrace(); | ||
$bar = @is_dir(__FILE__); | ||
@$average = 3 / 0; | ||
|
||
return [$foo, $bar, $average]; | ||
} |
31 changes: 31 additions & 0 deletions
31
...s/Rule/CleanCode/ErrorControlOperator/testDoesNotApplyToOtherUnaryOperatorsInFunction.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,31 @@ | ||
<?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/ | ||
*/ | ||
|
||
/** | ||
* testDoesNotApplyToOtherUnaryOperatorsInFunction | ||
* | ||
* @return bool | ||
*/ | ||
function testDoesNotApplyToOtherUnaryOperatorsInFunction() | ||
{ | ||
@$foo = is_dir(__DIR__); | ||
$bar = !$foo; | ||
++$bar; | ||
$baz = ++$bar; | ||
|
||
return !--$baz; | ||
} |