XML::quoteMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Copyright 2014 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2014 Fabian Grutschus. All rights reserved.
33
 * @license   BSD
34
 * @link      http://github.com/fabiang/xmpp
35
 */
36
37
namespace Fabiang\Xmpp\Util;
38
39
/**
40
 * XML utility methods.
41
 *
42
 * @package Xmpp\Util
43
 */
44
class XML
45
{
46
47
    /**
48
     * Quote XML string.
49
     *
50
     * @param string $string   String to be quoted
51
     * @param string $encoding Encoding used for quotation
52
     * @return string
53
     */
54 5
    public static function quote($string, $encoding = 'UTF-8')
55
    {
56 5
        $flags = ENT_QUOTES;
57
58 5
        if (defined('ENT_XML1')) {
59 4
            $flags |= ENT_XML1;
60 4
        }
61
62 5
        return htmlspecialchars($string, $flags, $encoding);
63
    }
64
65
    /**
66
     * Replace variables in a string and quote them before.
67
     *
68
     * <b>Hint:</b> this function works like <code>sprintf</code>
69
     *
70
     * @param string $message
71
     * @param mixed  $args
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
     * @param mixed  $...
0 ignored issues
show
Bug introduced by
There is no parameter named $.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     * @return string
74
     */
75 6
    public static function quoteMessage($message)
76
    {
77 6
        $variables = func_get_args();
78
79
        // shift message variable
80 6
        array_shift($variables);
81
82
        // workaround for `static` call in a closure
83 6
        $class = __CLASS__;
84
85 6
        return vsprintf(
86 6
            $message,
87 6
            array_map(
88 6
                function ($var) use ($class) {
89 3
                    return $class::quote($var);
90 6
                },
91
                $variables
92 6
            )
93 6
        );
94
    }
95
96
    /**
97
     * Generate a unique id.
98
     *
99
     * @return string
100
     */
101 3
    public static function generateId()
102
    {
103 3
        return static::quote('fabiang_xmpp_' . uniqid());
104
    }
105
106
    /**
107
     * Encode a string with Base64 and quote it.
108
     *
109
     * @param string $data
110
     * @param string $encoding
111
     * @return string
112
     */
113 3
    public static function base64Encode($data, $encoding = 'UTF-8')
114
    {
115 3
        return static::quote(base64_encode($data), $encoding);
116
    }
117
118
    /**
119
     * Decode a Base64 encoded string.
120
     *
121
     * @param string $data
122
     * @return string
123
     */
124 3
    public static function base64Decode($data)
125
    {
126 3
        return base64_decode($data);
127
    }
128
}
129