GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a4de62...189a0f )
by Michael
04:10
created

Formatter::replace()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.2
cc 4
eloc 7
nc 3
nop 3
crap 4
1
<?php
2
/**
3
 * Formatter.php
4
 *
5
 * @package Embera
6
 * @author  Michael Pratt <[email protected]>
7
 * @link    http://www.michael-pratt.com/
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 *
12
 */
13
namespace Embera;
14
15
/**
16
 * A formatter that acts as a Decorator for the main library.
17
 * It outputs the oembed data as a custom string.
18
 */
19
class Formatter
20
{
21
    /** @var object Instance of \Embera\Embera */
22
    protected $embera;
23
24
    /** @var bool Wether or not to allow offline responses */
25
    protected $allowOffline;
26
27
    /** @var array Fetched errors */
28
    protected $errors = array();
29
30
    /** @var string The template with placeholders to be replaced with the data from an ombed response */
31
    protected $template;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param object $embera Instance of \Embera\Embera
37
     * @param bool $allowOffline Wether or not to allow offline embera
38
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
39
     */
40 16
    public function __construct(\Embera\Embera $embera, $allowOffline = false)
41
    {
42 16
        $this->embera = $embera;
43 16
        $this->allowOffline = $allowOffline;
44 16
    }
45
46
    /**
47
     * Sets a template with placeholders, that should be
48
     * replaced by the data from an oembed response.
49
     *
50
     *
51
     * @param string $template
52
     * @param string|array $body An array or string with Urls
53
     * @return string
54
     */
55 12
    public function setTemplate($template, $body = null)
56
    {
57 12
        $this->template = $template;
58
59 12
        if (!is_null($body)) {
60 2
            return $this->transform($body);
61
        }
62
63 10
        return '';
64
    }
65
66
    /**
67
     * This method transforms an array or a string with urls
68
     * into another string using a specified template.
69
     *
70
     * @param string|array $body An array or string with Urls
71
     * @return string
72
     */
73 12
    public function transform($body = null)
74
    {
75 12
        $providers = array();
76 12
        if ($urls = $this->embera->getUrlInfo($body)) {
77 10
78 10
            foreach ($urls as $url => $data) {
79 2
                if (!$this->allowOffline && (int) $data['embera_using_fake'] === 1) {
80 2
                    $this->errors[] = 'Using fake oembed response on ' . $url;
81
                    continue;
82
                }
83 8
84 8
                $providers[$url] = $this->replace($data, $this->template);
85 8
            }
86 10
        }
87 10
88
        if (is_array($body)) {
89 12
            $return = implode('', $providers);
90 10
        } else {
91 10
            $return = str_replace(array_keys($providers), array_values($providers), $body);
92 4
        }
93
94
        // Remove unchanged placeholders
95
        return preg_replace('~{([\w\d\-_]+)}~i', '', $return);
96 12
    }
97
98
    /**
99
     * Replaces the given $data inside the $template
100
     *
101
     * @param array $data
102
     * @param string $template
103
     * @param string $prefix
104 4
     * @return string
105
     */
106 4
    protected function replace(array $data, $template, $prefix = null)
107 4
    {
108
        foreach ($data as $k => $d) {
109
            if (is_array($d)) {
110
                return $this->replace($d, $template, $k . '.');
111
            } else {
112
                $template = str_replace('{' . (!empty($prefix) ? $prefix . $k : $k) . '}', $d, $template);
113
            }
114
        }
115 4
116
        return $template;
117 4
    }
118
119
    /**
120
     * Gets the last error found
121
     *
122
     * @return string
123
     */
124
    public function getLastError()
125 2
    {
126
        $errors = $this->getErrors();
127 2
        return end($errors);
128
    }
129
130
    /**
131
     * Returns an array with all the errors
132
     *
133
     * @return array
134
     */
135
    public function getErrors()
136
    {
137
        return array_merge($this->embera->getErrors(), $this->errors);
138
    }
139
140
    /**
141 4
     * Checks if there were errors
142
     *
143 4
     * @return bool
144 2
     */
145
    public function hasErrors()
146
    {
147 2
        return ($this->embera->hasErrors() || !empty($this->errors));
148
    }
149
150
    /**
151
     * Truly decorate the embera object. With this
152
     * method Im preserving compatability with the
153
     * API of the decorated object.
154
     *
155
     * @param string $method
156
     * @param array  $args
157
     * @return mixed
158
     *
159
     * @throws InvalidArgumentException when a method was not found
160
     */
161
    public function __call($method, $args)
162
    {
163
        if (is_callable(array($this->embera, $method))) {
164
            return call_user_func_array(array($this->embera, $method), $args);
165
        }
166
167
        throw new \InvalidArgumentException('No method ' . $method . ' was found');
168
    }
169
}
170
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
171