TocifierTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 38.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 2
dl 26
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testProcess() 0 17 1
A testTOC() 12 12 1
A testDataHideFromTOC() 14 14 1
A testPrependAnchor() 0 9 1
A testSetId() 0 9 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 eNTiDi\Autotoc\Tests;
4
5
use eNTiDi\Autotoc\Tocifier;
6
use PHPUnit_Framework_TestCase;
7
8
class TocifierTest extends PHPUnit_Framework_TestCase
9
{
10
    public function testProcess()
11
    {
12
        $tocifier = new Tocifier(1234);
13
        $this->assertFalse($tocifier->process());
14
15
        $tocifier = new Tocifier('');
16
        $this->assertFalse($tocifier->process());
17
18
        $tocifier = new Tocifier(null);
19
        $this->assertFalse($tocifier->process());
20
21
        $tocifier = new Tocifier(['1234']);
0 ignored issues
show
Documentation introduced by
array('1234') is of type array<integer,string,{"0":"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...
22
        $this->assertFalse($tocifier->process());
23
24
        $tocifier = new Tocifier('1234');
25
        $this->assertTrue($tocifier->process());
26
    }
27
28
    public function testPrependAnchor()
29
    {
30
        $tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
31
        $this->assertEquals('', $tocifier->getHtml());
32
33
        $tocifier->setAugmentCallback(['\eNTiDi\Autotoc\Tocifier', 'prependAnchor']);
34
        $this->assertTrue($tocifier->process());
35
        $this->assertStringEqualsFile(__DIR__.'/html1', $tocifier->getHtml());
36
    }
37
38
    public function testSetId()
39
    {
40
        $tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
41
        $this->assertEquals('', $tocifier->getHtml());
42
43
        // The default augmenting method should already be setId
44
        $this->assertTrue($tocifier->process());
45
        $this->assertStringEqualsFile(__DIR__.'/html2', $tocifier->getHtml());
46
    }
47
48 View Code Duplication
    public function testTOC()
49
    {
50
        $tocifier = new Tocifier(file_get_contents(__DIR__.'/test1'));
51
        $this->assertEquals([], $tocifier->getTOC());
52
        $this->assertTrue($tocifier->process());
53
        $this->assertNotNull($tocifier->getTOC());
54
55
        ob_start();
56
        $tocifier->dumpTOC();
57
        $returned = ob_get_clean();
58
        $this->assertStringEqualsFile(__DIR__.'/toc1', $returned);
59
    }
60
61 View Code Duplication
    public function testDataHideFromTOC()
62
    {
63
        $tocifier = new Tocifier(file_get_contents(__DIR__.'/test2'));
64
        $this->assertEquals('', $tocifier->getHtml());
65
        $this->assertTrue($tocifier->process());
66
67
        // Check the augmented HTML is equal to the original one
68
        $this->assertStringEqualsFile(__DIR__.'/test2', $tocifier->getHtml());
69
70
        ob_start();
71
        $tocifier->dumpTOC();
72
        $returned = ob_get_clean();
73
        $this->assertEquals("\n", $returned);
74
    }
75
}
76