ChoicePagePartTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 48.28 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 42
loc 87
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetDefaultView() 0 6 1
A setUp() 0 4 1
A testAdaptForm() 21 21 1
A testGetDefaultAdminType() 0 4 1
A testSetGetExpanded() 7 7 1
A testSetGetMultiple() 7 7 1
A testSetGetChoices() 7 7 1
A testGettersAndSetters() 0 12 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
namespace Kunstmaan\FormBundle\Tests\Entity\PageParts;
4
5
use ArrayObject;
6
use Kunstmaan\FormBundle\Entity\PageParts\ChoicePagePart;
7
use Kunstmaan\FormBundle\Form\ChoicePagePartAdminType;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Form\FormBuilderInterface;
10
11
/**
12
 * Tests for ChoicePagePart
13
 */
14
class ChoicePagePartTest extends TestCase
15
{
16
    /**
17
     * @var ChoicePagePart
18
     */
19
    protected $object;
20
21
    /**
22
     * Sets up the fixture, for example, opens a network connection.
23
     * This method is called before a test is executed.
24
     */
25
    protected function setUp(): void
26
    {
27
        $this->object = new ChoicePagePart();
28
    }
29
30
    public function testGetDefaultView()
31
    {
32
        $stringValue = $this->object->getDefaultView();
33
        $this->assertNotNull($stringValue);
34
        $this->assertIsString($stringValue);
35
    }
36
37 View Code Duplication
    public function testAdaptForm()
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...
38
    {
39
        $object = $this->object;
40
        $object->setRequired(true);
41
42
        $formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')
43
            ->disableOriginalConstructor()
44
            ->getMock();
45
46
        $formBuilder->expects($this->any())
47
            ->method('getData')
48
            ->willReturn([]);
49
50
        $fields = new ArrayObject();
51
52
        $this->assertEquals(0, count($fields));
53
        $object->setErrorMessageRequired('invalid!');
54
        /* @var FormBuilderInterface $formBuilder */
55
        $object->adaptForm($formBuilder, $fields, 0);
56
        $this->assertTrue(count($fields) > 0);
57
    }
58
59
    public function testGetDefaultAdminType()
60
    {
61
        $this->assertEquals(ChoicePagePartAdminType::class, $this->object->getDefaultAdminType());
62
    }
63
64 View Code Duplication
    public function testSetGetExpanded()
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...
65
    {
66
        $object = $this->object;
67
        $this->assertFalse($object->getExpanded());
68
        $object->setExpanded(true);
69
        $this->assertTrue($object->getExpanded());
70
    }
71
72 View Code Duplication
    public function testSetGetMultiple()
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...
73
    {
74
        $object = $this->object;
75
        $this->assertFalse($object->getMultiple());
76
        $object->setMultiple(true);
77
        $this->assertTrue($object->getMultiple());
78
    }
79
80 View Code Duplication
    public function testSetGetChoices()
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...
81
    {
82
        $object = $this->object;
83
        $choices = array('test1' => 'test1', 'test2' => 'test2');
84
        $object->setChoices($choices);
0 ignored issues
show
Documentation introduced by
$choices is of type array<string,string,{"te...ing","test2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
        $this->assertEquals($choices, $object->getChoices());
86
    }
87
88
    public function testGettersAndSetters()
89
    {
90
        $object = $this->object;
91
        $value = 'test';
92
        $object->setEmptyValue($value);
93
        $object->setRequired(true);
94
        $object->setErrorMessageRequired('fix your code!');
95
96
        $this->assertEquals($value, $object->getEmptyValue());
97
        $this->assertTrue($object->getRequired());
98
        $this->assertEquals('fix your code!', $object->getErrorMessageRequired());
99
    }
100
}
101