Failed Conditions
Push — psr2 ( e9eace...d4d8fb )
by Andreas
14:17 queued 07:54
created

Preformatted   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A writeCall() 0 4 1
A writeCalls() 0 4 1
A finalise() 0 9 1
C process() 0 26 7
1
<?php
2
3
namespace dokuwiki\Parsing\Handler;
4
5
class Preformatted implements ReWriterInterface
6
{
7
8
    /** @var CallWriterInterface original call writer */
9
    protected $callWriter;
10
11
    protected $calls = array();
12
    protected $pos;
13
    protected $text ='';
14
15
    /**
16
     * @inheritdoc
17
     */
18
    public function __construct(CallWriterInterface $CallWriter)
19
    {
20
        $this->callWriter = $CallWriter;
21
    }
22
23
    /** @inheritdoc */
24
    public function writeCall($call)
25
    {
26
        $this->calls[] = $call;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     * Probably not needed but just in case...
32
     */
33
    public function writeCalls($calls)
34
    {
35
        $this->calls = array_merge($this->calls, $calls);
36
    }
37
38
    /** @inheritdoc */
39
    public function finalise()
40
    {
41
        $last_call = end($this->calls);
42
        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
43
44
        $this->process();
45
        $this->callWriter->finalise();
46
        unset($this->callWriter);
47
    }
48
49
    /** @inheritdoc */
50
    public function process()
51
    {
52
        foreach ($this->calls as $call) {
53
            switch ($call[0]) {
54
                case 'preformatted_start':
55
                    $this->pos = $call[2];
56
                    break;
57
                case 'preformatted_newline':
58
                    $this->text .= "\n";
59
                    break;
60
                case 'preformatted_content':
61
                    $this->text .= $call[1][0];
62
                    break;
63
                case 'preformatted_end':
64
                    if (trim($this->text)) {
65
                        $this->callWriter->writeCall(array('preformatted', array($this->text), $this->pos));
66
                    }
67
                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
68
                    $this->callWriter->writeCall(array('eol', array(), $this->pos));
69
                    $this->callWriter->writeCall(array('eol', array(), $this->pos));
70
                    break;
71
            }
72
        }
73
74
        return $this->callWriter;
75
    }
76
}
77