Passed
Push — master ( 40ca60...430c98 )
by Sebastian
02:56
created
src/Mailcode/Parser/Safeguard.php 3 patches
Indentation   +150 added lines, -150 removed lines patch added patch discarded remove patch
@@ -61,50 +61,50 @@  discard block
 block discarded – undo
61 61
     
62 62
     const ERROR_NOT_A_SINGLE_LINES_FORMATTER = 47806;
63 63
     
64
-   /**
65
-    * @var Mailcode_Parser
66
-    */
64
+    /**
65
+     * @var Mailcode_Parser
66
+     */
67 67
     protected $parser;
68 68
     
69
-   /**
70
-    * @var Mailcode_Collection
71
-    */
69
+    /**
70
+     * @var Mailcode_Collection
71
+     */
72 72
     protected $commands;
73 73
     
74
-   /**
75
-    * @var string
76
-    */
74
+    /**
75
+     * @var string
76
+     */
77 77
     protected $originalString;
78 78
     
79
-   /**
80
-    * @var Mailcode_Collection
81
-    */
79
+    /**
80
+     * @var Mailcode_Collection
81
+     */
82 82
     protected $collection;
83 83
     
84
-   /**
85
-    * Counter for the placeholders, global for all placeholders.
86
-    * @var integer
87
-    */
84
+    /**
85
+     * Counter for the placeholders, global for all placeholders.
86
+     * @var integer
87
+     */
88 88
     private static $counter = 0;
89 89
     
90
-   /**
91
-    * @var Mailcode_Parser_Safeguard_Placeholder[]
92
-    */
90
+    /**
91
+     * @var Mailcode_Parser_Safeguard_Placeholder[]
92
+     */
93 93
     protected $placeholders;
94 94
     
95
-   /**
96
-    * @var string
97
-    */
95
+    /**
96
+     * @var string
97
+     */
98 98
     protected $delimiter = '__';
99 99
     
100
-   /**
101
-    * @var string[]|NULL
102
-    */
100
+    /**
101
+     * @var string[]|NULL
102
+     */
103 103
     protected $placeholderStrings;
104 104
     
105
-   /**
106
-    * @var Mailcode_Parser_Safeguard_Formatter
107
-    */
105
+    /**
106
+     * @var Mailcode_Parser_Safeguard_Formatter
107
+     */
108 108
     protected $formatter;
109 109
     
110 110
     public function __construct(Mailcode_Parser $parser, string $subject)
@@ -113,25 +113,25 @@  discard block
 block discarded – undo
113 113
         $this->originalString = $subject;
114 114
     }
115 115
     
116
-   /**
117
-    * Retrieves the string the safeguard was created for.
118
-    * 
119
-    * @return string
120
-    */
116
+    /**
117
+     * Retrieves the string the safeguard was created for.
118
+     * 
119
+     * @return string
120
+     */
121 121
     public function getOriginalString() : string
122 122
     {
123 123
         return $this->originalString;
124 124
     }
125 125
     
126
-   /**
127
-    * Sets the delimiter character sequence used to prepend
128
-    * and append to the placeholders.
129
-    * 
130
-    * The delimiter's default is "__" (two underscores).
131
-    * 
132
-    * @param string $delimiter
133
-    * @return Mailcode_Parser_Safeguard
134
-    */
126
+    /**
127
+     * Sets the delimiter character sequence used to prepend
128
+     * and append to the placeholders.
129
+     * 
130
+     * The delimiter's default is "__" (two underscores).
131
+     * 
132
+     * @param string $delimiter
133
+     * @return Mailcode_Parser_Safeguard
134
+     */
135 135
     public function setDelimiter(string $delimiter) : Mailcode_Parser_Safeguard
136 136
     {
137 137
         if(empty($delimiter))
@@ -153,15 +153,15 @@  discard block
 block discarded – undo
153 153
         return $this->delimiter;
154 154
     }
155 155
     
156
-   /**
157
-    * Retrieves the safe string in which all commands have been replaced
158
-    * by placeholder strings.
159
-    *
160
-    * @return string
161
-    * @throws Mailcode_Exception 
162
-    *
163
-    * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
164
-    */
156
+    /**
157
+     * Retrieves the safe string in which all commands have been replaced
158
+     * by placeholder strings.
159
+     *
160
+     * @return string
161
+     * @throws Mailcode_Exception 
162
+     *
163
+     * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
164
+     */
165 165
     public function makeSafe() : string
166 166
     {
167 167
         $this->requireValidCollection();
@@ -169,16 +169,16 @@  discard block
 block discarded – undo
169 169
         return $this->makeSafePartial();
170 170
     }
171 171
     
172
-   /**
173
-    * Like makeSafe(), but allows partial (invalid) commands: use this
174
-    * if the subject string may contain only part of the whole set of
175
-    * commands. 
176
-    * 
177
-    * Example: parsing a text with an opening if statement, without the 
178
-    * matching end statement.
179
-    * 
180
-    * @return string
181
-    */
172
+    /**
173
+     * Like makeSafe(), but allows partial (invalid) commands: use this
174
+     * if the subject string may contain only part of the whole set of
175
+     * commands. 
176
+     * 
177
+     * Example: parsing a text with an opening if statement, without the 
178
+     * matching end statement.
179
+     * 
180
+     * @return string
181
+     */
182 182
     public function makeSafePartial() : string
183 183
     {
184 184
         $replaces = $this->getReplaces();
@@ -228,10 +228,10 @@  discard block
 block discarded – undo
228 228
         throw $this->exceptionWrongFormatter(Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting::class);
229 229
     }
230 230
     
231
-   /**
232
-    * Enables the formatter that ensures that all commands that
233
-    * @return Mailcode_Parser_Safeguard_Formatter_SingleLines
234
-    */
231
+    /**
232
+     * Enables the formatter that ensures that all commands that
233
+     * @return Mailcode_Parser_Safeguard_Formatter_SingleLines
234
+     */
235 235
     public function selectSingleLinesFormatter() : Mailcode_Parser_Safeguard_Formatter_SingleLines
236 236
     {
237 237
         $formatter = $this->selectFormatter('SingleLines');
@@ -256,13 +256,13 @@  discard block
 block discarded – undo
256 256
         );
257 257
     }
258 258
     
259
-   /**
260
-    * Retrieves an associative array with pairs of
261
-    * [placeholder string => replacement text]. 
262
-    * 
263
-    * @param bool $highlighted
264
-    * @return string[]string
265
-    */
259
+    /**
260
+     * Retrieves an associative array with pairs of
261
+     * [placeholder string => replacement text]. 
262
+     * 
263
+     * @param bool $highlighted
264
+     * @return string[]string
265
+     */
266 266
     protected function getReplaces(bool $highlighted=false, bool $normalize=false) : array
267 267
     {
268 268
         $placeholders = $this->getPlaceholders();
@@ -313,12 +313,12 @@  discard block
 block discarded – undo
313 313
         return $placeholder->getReplacementText();
314 314
     }
315 315
     
316
-   /**
317
-    * Retrieves all placeholders that have to be added to
318
-    * the subject text.
319
-    * 
320
-    * @return \Mailcode\Mailcode_Parser_Safeguard_Placeholder[]
321
-    */
316
+    /**
317
+     * Retrieves all placeholders that have to be added to
318
+     * the subject text.
319
+     * 
320
+     * @return \Mailcode\Mailcode_Parser_Safeguard_Placeholder[]
321
+     */
322 322
     public function getPlaceholders()
323 323
     {
324 324
         if(isset($this->placeholders))
@@ -373,17 +373,17 @@  discard block
 block discarded – undo
373 373
         return str_replace($placeholderStrings, array_values($replaces), $string);
374 374
     }
375 375
     
376
-   /**
377
-    * Makes the string whole again after transforming or filtering it,
378
-    * by replacing the command placeholders with the original commands.
379
-    *
380
-    * @param string $string
381
-    * @return string
382
-    * @throws Mailcode_Exception
383
-    *
384
-    * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
385
-    * @see Mailcode_Parser_Safeguard::ERROR_COMMAND_PLACEHOLDER_MISSING
386
-    */
376
+    /**
377
+     * Makes the string whole again after transforming or filtering it,
378
+     * by replacing the command placeholders with the original commands.
379
+     *
380
+     * @param string $string
381
+     * @return string
382
+     * @throws Mailcode_Exception
383
+     *
384
+     * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
385
+     * @see Mailcode_Parser_Safeguard::ERROR_COMMAND_PLACEHOLDER_MISSING
386
+     */
387 387
     public function makeWhole(string $string) : string
388 388
     {
389 389
         return $this->restore(
@@ -393,15 +393,15 @@  discard block
 block discarded – undo
393 393
         );
394 394
     }
395 395
     
396
-   /**
397
-    * Like `makeWhole()`, but ignores missing command placeholders.
398
-    *
399
-    * @param string $string
400
-    * @return string
401
-    * @throws Mailcode_Exception
402
-    *
403
-    * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
404
-    */
396
+    /**
397
+     * Like `makeWhole()`, but ignores missing command placeholders.
398
+     *
399
+     * @param string $string
400
+     * @return string
401
+     * @throws Mailcode_Exception
402
+     *
403
+     * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
404
+     */
405 405
     public function makeWholePartial(string $string) : string
406 406
     {
407 407
         return $this->restore(
@@ -411,20 +411,20 @@  discard block
 block discarded – undo
411 411
         );
412 412
     }
413 413
 
414
-   /**
415
-    * Like `makeWhole()`, but replaces the commands with a syntax
416
-    * highlighted version, meant for human readable texts only.
417
-    * 
418
-    * Note: the commands lose their functionality (They cannot be 
419
-    * parsed from that string again).
420
-    *
421
-    * @param string $string
422
-    * @return string
423
-    * @throws Mailcode_Exception
424
-    *
425
-    * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
426
-    * @see Mailcode_Parser_Safeguard::ERROR_COMMAND_PLACEHOLDER_MISSING
427
-    */
414
+    /**
415
+     * Like `makeWhole()`, but replaces the commands with a syntax
416
+     * highlighted version, meant for human readable texts only.
417
+     * 
418
+     * Note: the commands lose their functionality (They cannot be 
419
+     * parsed from that string again).
420
+     *
421
+     * @param string $string
422
+     * @return string
423
+     * @throws Mailcode_Exception
424
+     *
425
+     * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
426
+     * @see Mailcode_Parser_Safeguard::ERROR_COMMAND_PLACEHOLDER_MISSING
427
+     */
428 428
     public function makeHighlighted(string $string) : string
429 429
     {
430 430
         return $this->restore(
@@ -434,15 +434,15 @@  discard block
 block discarded – undo
434 434
         );
435 435
     }
436 436
     
437
-   /**
438
-    * Like `makeHighlighted()`, but ignores missing command placeholders.
439
-    * 
440
-    * @param string $string
441
-    * @return string
442
-    * @throws Mailcode_Exception
443
-    *
444
-    * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
445
-    */
437
+    /**
438
+     * Like `makeHighlighted()`, but ignores missing command placeholders.
439
+     * 
440
+     * @param string $string
441
+     * @return string
442
+     * @throws Mailcode_Exception
443
+     *
444
+     * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
445
+     */
446 446
     public function makeHighlightedPartial(string $string) : string
447 447
     {
448 448
         return $this->restore(
@@ -452,11 +452,11 @@  discard block
 block discarded – undo
452 452
         );
453 453
     }
454 454
     
455
-   /**
456
-    * Retrieves the commands collection contained in the string.
457
-    * 
458
-    * @return Mailcode_Collection
459
-    */
455
+    /**
456
+     * Retrieves the commands collection contained in the string.
457
+     * 
458
+     * @return Mailcode_Collection
459
+     */
460 460
     public function getCollection() : Mailcode_Collection
461 461
     {
462 462
         if(isset($this->collection))
@@ -474,11 +474,11 @@  discard block
 block discarded – undo
474 474
         return $this->getCollection()->isValid();
475 475
     }
476 476
     
477
-   /**
478
-    * @throws Mailcode_Exception
479
-    * 
480
-    * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
481
-    */
477
+    /**
478
+     * @throws Mailcode_Exception
479
+     * 
480
+     * @see Mailcode_Parser_Safeguard::ERROR_INVALID_COMMANDS
481
+     */
482 482
     protected function requireValidCollection() : void
483 483
     {
484 484
         if($this->getCollection()->isValid())
@@ -497,11 +497,11 @@  discard block
 block discarded – undo
497 497
         );
498 498
     }
499 499
     
500
-   /**
501
-    * Retrieves a list of all placeholder IDs used in the text.
502
-    * 
503
-    * @return string[]
504
-    */
500
+    /**
501
+     * Retrieves a list of all placeholder IDs used in the text.
502
+     * 
503
+     * @return string[]
504
+     */
505 505
     public function getPlaceholderStrings() : array
506 506
     {
507 507
         if(is_array($this->placeholderStrings))
@@ -528,13 +528,13 @@  discard block
 block discarded – undo
528 528
         return in_array($subject, $ids);
529 529
     }
530 530
     
531
-   /**
532
-    * Retrieves a placeholder instance by its ID.
533
-    * 
534
-    * @param int $id
535
-    * @throws Mailcode_Exception If the placeholder was not found.
536
-    * @return Mailcode_Parser_Safeguard_Placeholder
537
-    */
531
+    /**
532
+     * Retrieves a placeholder instance by its ID.
533
+     * 
534
+     * @param int $id
535
+     * @throws Mailcode_Exception If the placeholder was not found.
536
+     * @return Mailcode_Parser_Safeguard_Placeholder
537
+     */
538 538
     public function getPlaceholderByID(int $id) : Mailcode_Parser_Safeguard_Placeholder
539 539
     {
540 540
         $placeholders = $this->getPlaceholders();
@@ -557,13 +557,13 @@  discard block
 block discarded – undo
557 557
         );
558 558
     }
559 559
     
560
-   /**
561
-    * Retrieves a placeholder instance by its replacement text.
562
-    * 
563
-    * @param string $string
564
-    * @throws Mailcode_Exception
565
-    * @return Mailcode_Parser_Safeguard_Placeholder
566
-    */
560
+    /**
561
+     * Retrieves a placeholder instance by its replacement text.
562
+     * 
563
+     * @param string $string
564
+     * @throws Mailcode_Exception
565
+     * @return Mailcode_Parser_Safeguard_Placeholder
566
+     */
567 567
     public function getPlaceholderByString(string $string) : Mailcode_Parser_Safeguard_Placeholder
568 568
     {
569 569
         $placeholders = $this->getPlaceholders();
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
     */
135 135
     public function setDelimiter(string $delimiter) : Mailcode_Parser_Safeguard
136 136
     {
137
-        if(empty($delimiter))
137
+        if (empty($delimiter))
138 138
         {
139 139
             throw new Mailcode_Exception(
140 140
                 'Empty delimiter',
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
         $safe = str_replace(array_values($replaces), array_keys($replaces), $this->originalString);
187 187
 
188 188
         // If a formatter has been selected, let it modify the string.
189
-        if(isset($this->formatter))
189
+        if (isset($this->formatter))
190 190
         {
191 191
             $safe = $this->formatter->format($safe);
192 192
         }
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
     {
199 199
         $class = 'Mailcode\Mailcode_Parser_Safeguard_Formatter_'.$formatterID;
200 200
         
201
-        if(class_exists($class))
201
+        if (class_exists($class))
202 202
         {
203 203
             $this->formatter = new $class($this);
204 204
             
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
     {
221 221
         $formatter = $this->selectFormatter('HTMLHighlighting');
222 222
         
223
-        if($formatter instanceof Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting)
223
+        if ($formatter instanceof Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting)
224 224
         {
225 225
             return $formatter;
226 226
         }
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
     {
237 237
         $formatter = $this->selectFormatter('SingleLines');
238 238
         
239
-        if($formatter instanceof Mailcode_Parser_Safeguard_Formatter_SingleLines)
239
+        if ($formatter instanceof Mailcode_Parser_Safeguard_Formatter_SingleLines)
240 240
         {
241 241
             return $formatter;
242 242
         }
@@ -263,22 +263,22 @@  discard block
 block discarded – undo
263 263
     * @param bool $highlighted
264 264
     * @return string[]string
265 265
     */
266
-    protected function getReplaces(bool $highlighted=false, bool $normalize=false) : array
266
+    protected function getReplaces(bool $highlighted = false, bool $normalize = false) : array
267 267
     {
268 268
         $placeholders = $this->getPlaceholders();
269 269
         
270 270
         $replaces = array();
271 271
         
272
-        foreach($placeholders as $placeholder)
272
+        foreach ($placeholders as $placeholder)
273 273
         {
274 274
             $replace = '';
275 275
             $needle = $placeholder->getReplacementText();
276 276
             
277
-            if($highlighted)
277
+            if ($highlighted)
278 278
             {
279 279
                 $formattedNeedle = $this->formatter->getReplaceNeedle($placeholder);
280 280
                 
281
-                if($formattedNeedle !== $needle)
281
+                if ($formattedNeedle !== $needle)
282 282
                 {
283 283
                     $replaces[$formattedNeedle] = $placeholder->getHighlightedText();
284 284
                     $replace = $placeholder->getNormalizedText();
@@ -288,7 +288,7 @@  discard block
 block discarded – undo
288 288
                     $replace = $placeholder->getHighlightedText();
289 289
                 }
290 290
             }
291
-            else if($normalize)
291
+            else if ($normalize)
292 292
             {
293 293
                 $replace = $placeholder->getNormalizedText();
294 294
             }
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
     
306 306
     protected function getReplaceNeedle(Mailcode_Parser_Safeguard_Placeholder $placeholder) : string
307 307
     {
308
-        if(isset($this->formatter))
308
+        if (isset($this->formatter))
309 309
         {
310 310
             return $this->formatter->getReplaceNeedle($placeholder);
311 311
         }
@@ -321,7 +321,7 @@  discard block
 block discarded – undo
321 321
     */
322 322
     public function getPlaceholders()
323 323
     {
324
-        if(isset($this->placeholders))
324
+        if (isset($this->placeholders))
325 325
         {
326 326
             return $this->placeholders;
327 327
         }
@@ -330,7 +330,7 @@  discard block
 block discarded – undo
330 330
         
331 331
         $cmds = $this->getCollection()->getGroupedByHash();
332 332
         
333
-        foreach($cmds as $command)
333
+        foreach ($cmds as $command)
334 334
         {
335 335
             self::$counter++;
336 336
             
@@ -344,9 +344,9 @@  discard block
 block discarded – undo
344 344
         return $this->placeholders;
345 345
     }
346 346
     
347
-    protected function restore(string $string, bool $partial=false, bool $highlighted=false) : string
347
+    protected function restore(string $string, bool $partial = false, bool $highlighted = false) : string
348 348
     {
349
-        if(!$partial)
349
+        if (!$partial)
350 350
         {
351 351
             $this->requireValidCollection();
352 352
         }
@@ -355,9 +355,9 @@  discard block
 block discarded – undo
355 355
         
356 356
         $placeholderStrings = array_keys($replaces);
357 357
         
358
-        foreach($placeholderStrings as $search)
358
+        foreach ($placeholderStrings as $search)
359 359
         {
360
-            if(!$partial && !$highlighted && !strstr($string, $search))
360
+            if (!$partial && !$highlighted && !strstr($string, $search))
361 361
             {
362 362
                 throw new Mailcode_Exception(
363 363
                     'Command placeholder not found',
@@ -459,7 +459,7 @@  discard block
 block discarded – undo
459 459
     */
460 460
     public function getCollection() : Mailcode_Collection
461 461
     {
462
-        if(isset($this->collection))
462
+        if (isset($this->collection))
463 463
         {
464 464
             return $this->collection;
465 465
         }
@@ -481,7 +481,7 @@  discard block
 block discarded – undo
481 481
     */
482 482
     protected function requireValidCollection() : void
483 483
     {
484
-        if($this->getCollection()->isValid())
484
+        if ($this->getCollection()->isValid())
485 485
         {
486 486
             return;
487 487
         }
@@ -504,7 +504,7 @@  discard block
 block discarded – undo
504 504
     */
505 505
     public function getPlaceholderStrings() : array
506 506
     {
507
-        if(is_array($this->placeholderStrings))
507
+        if (is_array($this->placeholderStrings))
508 508
         {
509 509
             return $this->placeholderStrings;
510 510
         }
@@ -513,7 +513,7 @@  discard block
 block discarded – undo
513 513
         
514 514
         $this->placeholderStrings = array();
515 515
         
516
-        foreach($placeholders as $placeholder)
516
+        foreach ($placeholders as $placeholder)
517 517
         {
518 518
             $this->placeholderStrings[] = $placeholder->getReplacementText();
519 519
         }
@@ -539,9 +539,9 @@  discard block
 block discarded – undo
539 539
     {
540 540
         $placeholders = $this->getPlaceholders();
541 541
         
542
-        foreach($placeholders as $placeholder)
542
+        foreach ($placeholders as $placeholder)
543 543
         {
544
-            if($placeholder->getID() === $id)
544
+            if ($placeholder->getID() === $id)
545 545
             {
546 546
                 return $placeholder;
547 547
             }
@@ -568,9 +568,9 @@  discard block
 block discarded – undo
568 568
     {
569 569
         $placeholders = $this->getPlaceholders();
570 570
         
571
-        foreach($placeholders as $placeholder)
571
+        foreach ($placeholders as $placeholder)
572 572
         {
573
-            if($placeholder->getReplacementText() === $string)
573
+            if ($placeholder->getReplacementText() === $string)
574 574
             {
575 575
                 return $placeholder;
576 576
             }
Please login to merge, or discard this patch.
Braces   +3 added lines, -6 removed lines patch added patch discarded remove patch
@@ -282,17 +282,14 @@
 block discarded – undo
282 282
                 {
283 283
                     $replaces[$formattedNeedle] = $placeholder->getHighlightedText();
284 284
                     $replace = $placeholder->getNormalizedText();
285
-                }
286
-                else
285
+                } else
287 286
                 {
288 287
                     $replace = $placeholder->getHighlightedText();
289 288
                 }
290
-            }
291
-            else if($normalize)
289
+            } else if($normalize)
292 290
             {
293 291
                 $replace = $placeholder->getNormalizedText();
294
-            }
295
-            else
292
+            } else
296 293
             {
297 294
                 $replace = $placeholder->getOriginalText();
298 295
             }
Please login to merge, or discard this patch.