TraitPublicMethodTest::getPublicMethodNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace MS\PHPMD\Tests\Unit\CleanCode;
4
5
use MS\PHPMD\Rule\CleanCode\TraitPublicMethod;
6
use MS\PHPMD\Tests\Unit\AbstractApplyTest;
7
8
/**
9
 * Class TraitPublicMethodTest
10
 *
11
 * @package MS\PHPMD\Tests\Unit\CleanCode
12
 */
13
class TraitPublicMethodTest extends AbstractApplyTest
14
{
15
    /**
16
     * @covers MS\PHPMD\Rule\CleanCode\TraitPublicMethod
17
     *
18
     * @SuppressWarnings(PHPMD.StaticAccess)
19
     */
20
    public function testFindOnePublicMethodInTrait()
21
    {
22
        $traitNode = \Mockery::mock('PHPMD\Node\TraitNode');
23
        $node = $this->getPublicMethodNode($traitNode);
24
25
        $this->assertRule($node, 1);
26
    }
27
28
    /**
29
     * @covers MS\PHPMD\Rule\CleanCode\TraitPublicMethod
30
     *
31
     * @SuppressWarnings(PHPMD.StaticAccess)
32
     */
33
    public function testFindNoViolation()
34
    {
35
        $classNode = \Mockery::mock('PHPMD\Node\ClassNode');
36
        $node = $this->getPublicMethodNode($classNode);
37
38
        $this->assertRule($node, 0);
39
    }
40
41
    /**
42
     * @covers MS\PHPMD\Rule\CleanCode\TraitPublicMethod
43
     *
44
     * @SuppressWarnings(PHPMD.StaticAccess)
45
     */
46
    public function testFindPrivateMethodInTrait()
47
    {
48
        $classNode = \Mockery::mock('PHPMD\Node\TraitNode');
49
        $node = $this->getPrivateMethodNode($classNode);
50
51
        $this->assertRule($node, 0);
52
    }
53
54
    /**
55
     * @return TraitPublicMethod
56
     */
57
    protected function getRule()
58
    {
59
        $rule = new TraitPublicMethod();
60
61
        return $rule;
62
    }
63
64
    /**
65
     * @param mixed $parentType
66
     *
67
     * @return \Mockery\MockInterface
68
     */
69 View Code Duplication
    protected function getPublicMethodNode($parentType)
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...
70
    {
71
        $node = parent::getMethodNode('UserComparator', 'doThings');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getMethodNode() instead of getPublicMethodNode()). Are you sure this is correct? If so, you might want to change this to $this->getMethodNode().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
72
        $node->shouldReceive('getParentType')->andReturn($parentType);
73
        $node->shouldReceive('isPublic')->andReturn(true);
74
75
        return $node;
76
    }
77
78
    /**
79
     * @param mixed $parentType
80
     *
81
     * @return \Mockery\MockInterface
82
     */
83 View Code Duplication
    protected function getPrivateMethodNode($parentType)
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...
84
    {
85
        $node = parent::getMethodNode('UserComparator', 'doThings');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getMethodNode() instead of getPrivateMethodNode()). Are you sure this is correct? If so, you might want to change this to $this->getMethodNode().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
86
        $node->shouldReceive('getParentType')->andReturn($parentType);
87
        $node->shouldReceive('isPublic')->andReturn(false);
88
89
        return $node;
90
    }
91
}
92