Passed
Push — master ( 40ca60...430c98 )
by Sebastian
02:56
created

analyzeAppend()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 24
rs 9.9332
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Detects whether the placeholder needs newlines characters
16
 * prepended or appended.
17
 *
18
 * @package Mailcode
19
 * @subpackage Parser
20
 * @author Sebastian Mordziol <[email protected]>
21
 * 
22
 * @property Mailcode_Parser_Safeguard_Formatter_SingleLines $formatter
23
 */
24
class Mailcode_Parser_Safeguard_Formatter_SingleLines_Location extends Mailcode_Parser_Safeguard_FormatterLocation
25
{
26
   /**
27
    * @var int
28
    */
29
    protected $eolLength;
30
    
31
   /**
32
    * @var string
33
    */
34
    protected $eol;
35
    
36
   /**
37
    * @var boolean
38
    */
39
    protected $prepend = false;
40
41
   /**
42
    * @var boolean
43
    */
44
    protected $append = false;
45
    
46
    protected function init() : void
47
    {
48
        $this->eolLength = $this->formatter->getEOLLength();
49
        $this->eol = $this->formatter->getEOLChar();
50
    
51
        $this->analyzePrepend();
52
        $this->analyzeAppend();
53
    }
54
    
55
   /**
56
    * Whether an EOL character needs to be appended or prepended.
57
    *  
58
    * @return bool
59
    */
60
    public function requiresAdjustment() : bool
61
    {
62
        return $this->requiresAppend() || $this->requiresPrepend();
63
    }
64
    
65
    public function requiresPrepend() : bool
66
    {
67
        return $this->prepend;
68
    }
69
    
70
    public function requiresAppend() : bool
71
    {
72
        return $this->append;
73
    }
74
    
75
    protected function analyzePrepend() : void
76
    {
77
        $position = $this->location->getStartPosition();
78
        
79
        // we're at the beginning of the string
80
        if($position == 0)
81
        {
82
            return;
83
        }
84
        
85
        $prevPos = $position - $this->eolLength;
86
        
87
        if($prevPos < 0)
88
        {
89
            $prevPos = 0;
90
        }
91
        
92
        $prev = mb_substr($this->location->getSubjectString(), $prevPos, $this->eolLength);
93
        
94
        if($prev !== $this->formatter->getEOLChar())
95
        {
96
            $this->prepend = true;
97
        }
98
    }
99
    
100
    protected function analyzeAppend() : void
101
    {
102
        $subjectLength = $this->location->getSubjectLength();
103
        
104
        $position = $this->location->getEndPosition();
105
        
106
        // we're at the end of the string
107
        if($position >= $subjectLength)
108
        {
109
            return;
110
        }
111
        
112
        $nextPos = $position + $this->eolLength;
113
        
114
        if($nextPos > $subjectLength)
115
        {
116
            $nextPos = $subjectLength - $this->eolLength;
117
        }
118
        
119
        $next = mb_substr($this->location->getSubjectString(), $nextPos, $this->eolLength);
120
        
121
        if($next !== $this->formatter->getEOLChar())
122
        {
123
            $this->append = true;
124
        }
125
    }
126
    
127
    protected function getAdjustedText() : string
128
    {
129
        $prepend = '';
130
        $append = '';
131
        
132
        if($this->requiresPrepend())
133
        {
134
            $prepend = $this->eol;
135
        }
136
        
137
        if($this->requiresAppend())
138
        {
139
            $append = $this->eol;
140
        }
141
        
142
        return $prepend.$this->location->getPlaceholderString().$append;
143
    }
144
}
145