Skip to content

Commit

Permalink
Closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
eeree committed Jun 14, 2017
1 parent fbecb1b commit c87e7cf
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/php/PHPMD/Rule/CleanCode/ErrorControlOperator.php
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 src/test/php/PHPMD/Rule/CleanCode/ErrorControlOperatorTest.php
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());
}
}
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;
}
}
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;
}

0 comments on commit c87e7cf

Please sign in to comment.