RuleViolationTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 50
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructorExtractsClassNameFromGivenType() 0 10 1
A testConstructorExtractsClassNameFromGivenMethod() 10 10 1
A testConstructorExtractsMethodNameFromGivenMethod() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
19
20
/**
21
 * Test case for the {@link \PHPMD\RuleViolation} class.
22
 *
23
 * @since     0.2.5
24
 *
25
 * @covers \PHPMD\RuleViolation
26
 */
27
class RuleViolationTest extends AbstractTest
28
{
29
    /**
30
     * testConstructorExtractsClassNameFromGivenType
31
     *
32
     * @return void
33
     */
34
    public function testConstructorExtractsClassNameFromGivenType()
35
    {
36
        $rule = $this->getRuleMock();
37
38
        $node = $this->getClassMock();
39
        $node->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<PHPMD\Node\ClassNode>? 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...
40
            ->method('getName');
41
42
        new RuleViolation($rule, $node, 'foo');
43
    }
44
45
    /**
46
     * testConstructorExtractsClassNameFromGivenMethod
47
     *
48
     * @return void
49
     */
50 View Code Duplication
    public function testConstructorExtractsClassNameFromGivenMethod()
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...
51
    {
52
        $rule = $this->getRuleMock();
53
54
        $node = $this->getMethodMock();
55
        $node->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<PHPMD\Node\MethodNode>? 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...
56
            ->method('getParentName');
57
58
        new RuleViolation($rule, $node, 'foo');
59
    }
60
61
    /**
62
     * testConstructorExtractsMethodNameFromGivenMethod
63
     *
64
     * @return void
65
     */
66 View Code Duplication
    public function testConstructorExtractsMethodNameFromGivenMethod()
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...
67
    {
68
        $rule = $this->getRuleMock();
69
70
        $node = $this->getMethodMock();
71
        $node->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<PHPMD\Node\MethodNode>? 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...
72
            ->method('getName');
73
74
        new RuleViolation($rule, $node, 'foo');
75
    }
76
}
77