SprintfToPH   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 62
ccs 15
cts 15
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 27 2
A __construct() 0 2 1
1
<?php
2
3
4
namespace Matecat\SubFiltering\Filters;
5
6
7
use Matecat\SubFiltering\Commons\AbstractHandler;
8
use Matecat\SubFiltering\Enum\CTypeEnum;
9
use Matecat\SubFiltering\Filters\Sprintf\SprintfLocker;
10
11
class SprintfToPH extends AbstractHandler {
12
13
    private $source;
0 ignored issues
show
introduced by
The private property $source is not used, and could be removed.
Loading history...
14
    private $target;
0 ignored issues
show
introduced by
The private property $target is not used, and could be removed.
Loading history...
15
16 89
    public function __construct() {
17 89
        parent::__construct();
18 89
    }
19
20
    /**
21
     * TestSet:
22
     * <code>
23
     * |%-4d|%-4d|
24
     * |%':4d|
25
     * |%-':4d|
26
     * |%-'04d|
27
     * %02.2f
28
     * %02d
29
     * %1$s!
30
     * %08b
31
     * 20%-os - ignored
32
     * 20%-dir - ignored
33
     * 20%-zar - ignored
34
     *</code>
35
     *
36
     * @see
37
     * - https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
38
     * - https://en.cppreference.com/w/c/io/fprintf
39
     * - https://www.php.net/manual/en/function.sprintf.php
40
     * - https://www.w3resource.com/c-programming/stdio/c_library_method_sprintf.php
41
     *
42
     * @param $segment
43
     *
44
     * @return string
45
     */
46 87
    public function transform( $segment ) {
47
48 87
        $sprintfLocker = new SprintfLocker( $this->pipeline->getSource(), $this->pipeline->getTarget() );
49
50
        // placeholding
51 87
        $segment = $sprintfLocker->lock( $segment );
52
53
        // Octal parsing is disabled due to Hungarian percentages 20%-os
54 87
        $regex = '/(?:\x25\x25)|(\x25(?:(?:[1-9]\d*)\$|\((?:[^\)]+)\))?(?:\+)?(?:0|[+-]?\'[^$])?(?:-)?(?:\d+)?(?:\.(?:\d+))?((?:[hjlqtzL]{0,2}[ac-giopsuxAC-GOSUX]{1})(?![\d\w])|(?:#@[\w]+@)|(?:@)))/';
55
56
57 87
        preg_match_all( $regex, $segment, $vars, PREG_SET_ORDER );
58 87
        foreach ( $vars as $pos => $variable ) {
59
60
            //replace subsequent elements excluding already encoded
61 5
            $segment = preg_replace(
62 5
                    '/' . preg_quote( $variable[ 0 ], '/' ) . '/',
63 5
                    '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:' . base64_encode( $variable[ 0 ] ) . '"/>',
64
                    $segment,
65 5
                    1
66
            );
67
        }
68
69
        //revert placeholding
70 87
        $segment = $sprintfLocker->unlock( $segment );
71
72 87
        return $segment;
73
    }
74
75
}