RuleSetTest::createRuleSetFixture()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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
use PHPMD\Stubs\RuleStub;
21
22
/**
23
 * Test case for the {@link \PHPMD\RuleSet} class.
24
 *
25
 * @covers \PHPMD\RuleSet
26
 */
27
class RuleSetTest extends AbstractTest
28
{
29
    /**
30
     * testGetRuleByNameReturnsNullWhenNoMatchingRuleExists
31
     *
32
     * @return void
33
     */
34
    public function testGetRuleByNameReturnsNullWhenNoMatchingRuleExists()
35
    {
36
        $ruleSet = $this->createRuleSetFixture();
37
        $this->assertNull($ruleSet->getRuleByName(__FUNCTION__));
38
    }
39
40
    /**
41
     * testGetRuleByNameReturnsMatchingRuleInstance
42
     *
43
     * @return void
44
     */
45
    public function testGetRuleByNameReturnsMatchingRuleInstance()
46
    {
47
        $ruleSet = $this->createRuleSetFixture(__FUNCTION__, __CLASS__, __METHOD__);
48
        $rule = $ruleSet->getRuleByName(__CLASS__);
49
50
        $this->assertEquals(__CLASS__, $rule->getName());
51
    }
52
53
    /**
54
     * testApplyNotInvokesRuleWhenSuppressAnnotationExists
55
     *
56
     * @return void
57
     */
58
    public function testApplyNotInvokesRuleWhenSuppressAnnotationExists()
59
    {
60
        $ruleSet = $this->createRuleSetFixture(__FUNCTION__);
61
        $ruleSet->setReport($this->getReportMock(0));
62
        $ruleSet->apply($this->getClass());
63
64
        $this->assertNull($ruleSet->getRuleByName(__FUNCTION__)->node);
0 ignored issues
show
Bug introduced by
Accessing node on the interface PHPMD\Rule suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
65
    }
66
67
    /**
68
     * testApplyInvokesRuleWhenStrictModeIsSet
69
     *
70
     * @return void
71
     */
72
    public function testApplyInvokesRuleWhenStrictModeIsSet()
73
    {
74
        $ruleSet = $this->createRuleSetFixture(__FUNCTION__);
75
        $ruleSet->setReport($this->getReportMock(0));
76
        $ruleSet->setStrict();
77
78
        $class = $this->getClass();
79
        $ruleSet->apply($class);
80
81
        $this->assertSame($class, $ruleSet->getRuleByName(__FUNCTION__)->node);
0 ignored issues
show
Bug introduced by
Accessing node on the interface PHPMD\Rule suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
82
    }
83
84
    /**
85
     * Creates a rule set instance with a variable amount of appended rule
86
     * objects.
87
     *
88
     * @return \PHPMD\AbstractRule
89
     */
90
    private function createRuleSetFixture()
91
    {
92
        $ruleSet = new RuleSet();
93
        for ($i = 0; $i < func_num_args(); ++$i) {
94
            $rule = new RuleStub(func_get_arg($i));
95
96
            $ruleSet->addRule($rule);
97
        }
98
        return $ruleSet;
99
    }
100
}
101