Completed
Push — develop ( b74fa6...b70f25 )
by Stuart
02:44
created

string-functions.php ➔ vnsprintf()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 52
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 22
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 52
rs 8.6868

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright (c) 2016-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   MissingBits/StringFunctions
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://ganbarodigital.github.io/php-the-missing-bits
42
 */
43
44
if (!function_exists("vnsprintf")) {
45
46
/**
47
 * vsprintf()-compatible function, with support for named parameters in the
48
 * format string
49
 *
50
 * Internally, we convert named parameters into positional parameters. This
51
 * means that your format string:
52
 *
53
 * - must be a pure vsprintf()-compatible format string, or
54
 * - must use only named parameters, or
55
 * - must use a mix of named parameters and positional parameters
56
 *
57
 * You CANNOT mix named parameters and non-positional parameters.
58
 *
59
 * @param  string $format
60
 *         the format string to use
61
 * @param  array args
62
 *         the list of parameters to use
63
 * @return string
64
 *         the result of expanding $format
65
 */
66
function vnsprintf($format, $args)
67
{
68
    // we need to find all of the named parameters to expand
69
    $regex = "|(?<!%)(?:%)([^0-9].+)(?:\\\$)|U";
70
71
    $matches = [];
72
    $matchCount = preg_match_all($regex, $format, $matches, PREG_OFFSET_CAPTURE);
73
    if ($matchCount === 0) {
74
        // nothing to do
75
        return vsprintf($format, $args);
76
    }
77
78
    // if we get here, then we have some matches to expand
79
    // we're going to add them to the end of this array
80
    $messageData = $args;
81
82
    // this will keep track of where we've added the data
83
    $nextData = count($messageData);
84
    $paramKeys = array_fill_keys(array_keys($args), -1);
85
86
    // this will keep track of how we've shrunk the format string
87
    $formatChange = 0;
88
89
    // transform the named parameters into offset parameters
90
    foreach ($matches[1] as $match) {
91
        // what is the named parameter?
92
        $paramName = $match[0];
93
94
        // make sure the named parameter exists in our original data
95
        if (!isset($paramKeys[$paramName])) {
96
            throw new InvalidArgumentException("vnsprintf: named format-string parameter " . $paramName . " is not provided in \$args array");
97
        }
98
        // have we already assigned it a place in $messageData?
99
        // in case a named parameter appears multiple times in the format string
100
        if ($paramKeys[$paramName] === -1) {
101
            // no - it needs a home
102
            $messageData[$nextData] =& $args[$paramName];
103
            $paramKeys[$paramName] = $nextData;
104
            $nextData++;
105
        }
106
        // convert the named parameter in the format string into a positional
107
        // parameter into $messageData
108
        $paramOffset = $paramKeys[$paramName] + 1;
109
        $format = substr_replace($format, $paramOffset, $formatChange + $match[1], strlen($paramName));
110
111
        // how much did we shrink / grow the format string by?
112
        $formatChange = $formatChange - strlen($paramName) + strlen($paramOffset);
113
    }
114
115
    // all done
116
    return vsprintf($format, $messageData);
117
}
118
119
}
120