Passed
Push — master ( b87bd2...81711e )
by Sebastian
03:45
created

handle_subjectModified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Safeguard_Placeholder_Locator} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Placeholder_Locator
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Utility that can localize all instances of a command placeholder in 
16
 * a string, with the possibility to transform the placeholders at will.
17
 * 
18
 * It is used by the safeguard formatters to modify the placeholders
19
 * when making a safeguarded string whole again. 
20
 * 
21
 * For example, the SingleLines formatter will add the newlines in front 
22
 * and back of logic commands to ensure they are all on a single line.
23
 * The locator allows doing this while shifting the placeholders around
24
 * with the added string lengths.  
25
 *
26
 * @package Mailcode
27
 * @subpackage Parser
28
 * @author Sebastian Mordziol <[email protected]>
29
 */
30
class Mailcode_Parser_Safeguard_Placeholder_Locator
31
{
32
   /**
33
    * @var string
34
    */
35
    private $subject;
36
    
37
   /**
38
    * @var Mailcode_Parser_Safeguard_Placeholder
39
    */
40
    private $placeholder;
41
    
42
   /**
43
    * @var Mailcode_Parser_Safeguard_Placeholder_Locator_Location[]
44
    */
45
    private $instances = array();
46
    
47
    public function __construct(Mailcode_Parser_Safeguard_Placeholder $placeholder, string $subject)
48
    {
49
        $this->placeholder = $placeholder;
50
        $this->subject = $subject;
51
        
52
        $this->localizeInstances();
53
    }
54
    
55
   /**
56
    * Attempts to find the placeholder's instances in the
57
    * target string, and returns a location instance for
58
    * each, which allows accessing their exact position.
59
    */
60
    private function localizeInstances() : void
61
    {
62
        $lastPos = 0;
63
        $needle = $this->placeholder->getReplacementText();
64
        $index = 0;
65
        
66
        while(($lastPos = mb_strpos($this->subject, $needle, $lastPos)) !== false)
67
        {
68
            $length = mb_strlen($needle);
69
            
70
            $this->instances[] = new Mailcode_Parser_Safeguard_Placeholder_Locator_Location(
71
                $this,
72
                $this->placeholder,
73
                $index,
74
                $lastPos,
75
                $length
76
            );
77
            
78
            $lastPos = $lastPos + mb_strlen($needle);
79
            
80
            $index++;
81
        }
82
    }
83
    
84
   /**
85
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location[]
86
    */
87
    public function getLocations() : array
88
    {
89
        return $this->instances;
90
    }
91
    
92
   /**
93
    * Retrieves a location by its index in the locations list (in 
94
    * ascending order as found in the subject string, zero based.)
95
    * 
96
    * @param int $index
97
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location|NULL
98
    */
99
    public function getLocationByIndex(int $index) : ?Mailcode_Parser_Safeguard_Placeholder_Locator_Location
100
    {
101
        if(isset($this->instances[$index]))
102
        {
103
            return $this->instances[$index];
104
        }
105
        
106
        return null;
107
    }
108
    
109
   /**
110
    * Replaces the placeholder at the location with the specified
111
    * replacement text.
112
    * 
113
    * NOTE: The replacement text MUST contain the original placeholder
114
    * for this to work. An exception will be triggered otherwise.
115
    * 
116
    * @param Mailcode_Parser_Safeguard_Placeholder_Locator_Location $location
117
    * @param string $replacementText
118
    * @throws Mailcode_Exception
119
    * 
120
    * @see Mailcode_Parser_Safeguard_Placeholder_Locator_Replacer::ERROR_PLACEHOLDER_STRING_MISSING
121
    */
122
    public function replaceWith(Mailcode_Parser_Safeguard_Placeholder_Locator_Location $location, string $replacementText) : void
123
    {
124
        $replacer = new Mailcode_Parser_Safeguard_Placeholder_Locator_Replacer(
125
            $this,
126
            $location, 
127
            $replacementText,
128
            $this->subject
129
        );
130
        
131
        $replacer->replace();
132
    }
133
    
134
    public function handle_subjectModified(string $subject, Mailcode_Parser_Safeguard_Placeholder_Locator_Replacer $replacer)
0 ignored issues
show
Unused Code introduced by
The parameter $replacer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

134
    public function handle_subjectModified(string $subject, /** @scrutinizer ignore-unused */ Mailcode_Parser_Safeguard_Placeholder_Locator_Replacer $replacer)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        $this->subject = $subject;
137
    }
138
    
139
   /**
140
    * Retrieves the subject string, with all changes that were made, if any. 
141
    *  
142
    * @return string
143
    */
144
    public function getSubjectString() : string
145
    {
146
        return $this->subject;
147
    }
148
}
149