EmptyCatchBlock::apply()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 3
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Rule\Design;
19
20
use PHPMD\AbstractNode;
21
use PHPMD\AbstractRule;
22
use PHPMD\Rule\FunctionAware;
23
use PHPMD\Rule\MethodAware;
24
25
/**
26
 * This rule class detects empty catch blocks
27
 *
28
 * @author    Grégoire Paris <[email protected]>
29
 * @author    Kamil Szymanski <[email protected]>
30
 */
31
class EmptyCatchBlock extends AbstractRule implements MethodAware, FunctionAware
32
{
33
    /**
34
     * This method checks if a given function or method contains an empty catch block
35
     * and emits a rule violation when it exists.
36
     *
37
     * @param \PHPMD\AbstractNode $node
38
     * @return void
39
     */
40 7 View Code Duplication
    public function apply(AbstractNode $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42 7
        foreach ($node->findChildrenOfType('CatchStatement') as $catchBlock) {
43 4
            $scope = $catchBlock->getFirstChildOfType('ScopeStatement');
44 4
            if (count($scope->getChildren()) == 0) {
0 ignored issues
show
Documentation Bug introduced by
The method getChildren does not exist on object<PHPMD\Node\ASTNode>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
45 4
                $this->addViolation($catchBlock, array($node->getName()));
46
            }
47
        }
48 7
    }
49
}
50