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.

Embera::clean()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * Embera.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
 * The Main Class of this library
17
 */
18
class Embera
19
{
20
    /** @var int Class constant with the current Version of this library */
21
    const VERSION = '1.9.3';
22
23
    /** @var object Instance of \Embera\Oembed */
24
    protected $oembed;
25
26
    /** @var object Instance of \Embera\Providers */
27
    protected $providers;
28
29
    /** @var array Configuration Settings */
30
    protected $config = array();
31
32
    /** @var array Fetched errors */
33
    protected $errors = array();
34
35
    /** @var string The pattern used to extract urls from a text */
36
    protected $urlRegex = '~\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_))~i';
37
38
    /** @var string The pattern used to extract urls from a text when the embed:// prefix option is enabled */
39
    protected $urlEmbedRegex = '~\bembed://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_))~i';
40
41
    /**
42
     * Constructs the object and also instantiates the \Embera\Oembed Object
43
     * and stores it into the $oembed properoty
44
     *
45
     * @param array $config
46
     * @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...
47
     */
48 46
    public function __construct(array $config = array())
49
    {
50 46
        $this->config = array_replace_recursive(array(
51
            /**
52
             * The oembed setting represents the behaviour of the library.
53
             * The default value (since version 1.8.0) is null, which means that the
54
             * library will first try to get the data from the oembed endpoint and if that fails
55
             * it tries to use a fake response.
56
             *
57
             * When true is given it ONLY tries to get the info from the endpoint directly... It DOES NOT fallback
58
             * to fake responses.
59
             *
60
             * Finally, when false is given it ONLY uses fake responses.
61
             */
62 46
            'oembed' => null,
63 46
            'use_embed_prefix' => false,
64
            'params' => array(
65 46
                'width' => 0,
66 46
                'maxwidth' => 0,
67 46
                'height' => 0,
68 46
                'maxheight' => 0,
69 46
            ),
70 46
            'custom_params' => array(),
71 46
            'http' => array(),
72 46
            'fake' => array(),
73 46
            'ignore_tags' => array('pre', 'code', 'a', 'img'),
74 46
        ), $config);
75
76
77 46
        $this->config['params']['maxwidth'] = max(
78 46
            $this->config['params']['width'],
79 46
            $this->config['params']['maxwidth']
80 46
        );
81
82 46
        $this->config['params']['maxheight'] = max(
83 46
            $this->config['params']['height'],
84 46
            $this->config['params']['maxheight']
85 46
        );
86
87 46
        unset($this->config['params']['height'], $this->config['params']['width']);
88
89 46
        $this->oembed = new \Embera\Oembed(new \Embera\HttpRequest($this->config['http']));
90 46
        $this->providers = new \Embera\Providers($this->config, $this->oembed);
91 46
    }
92
93
    /**
94
     * Embeds known/available services into the
95
     * given text.
96
     *
97
     * @param string $body
98
     * @return string
99
     */
100 18
    public function autoEmbed($body = null)
101
    {
102 18
        if (!is_string($body)) {
103 6
            $this->errors[] = 'For auto-embedding purposes, the input must be a string';
104 18
        } elseif ($data = $this->getUrlInfo($body)) {
105 10
            $table = array();
106 10
            foreach ($data as $url => $service) {
107 10
                if (!empty($service['html'])) {
108 10
                    $table[$url] = $service['html'];
109 10
                }
110 10
            }
111
112
            // Determine wether the body looks like HTML or just plain text.
113 10
            if (strpos($body, '>') !== false) {
114 2
                $processor = new \Embera\HtmlProcessor($this->config['ignore_tags'], $table);
115 2
                return $processor->process($body);
116
            }
117
118 8
            return strtr($body, $table);
119
        }
120
121 8
        return $body;
122
    }
123
124
    /**
125
     * Finds all the information about a url (or a collection of urls)
126
     *
127
     * @param string|array $body An array or string with urls
128
     * @return array
129
     */
130 36
    public function getUrlInfo($body = null)
131
    {
132 36
        $results = array();
133 36
        if ($providers = $this->getProviders($body)) {
134 30
            foreach ($providers as $url => $service) {
135 30
                $results[$url] = $service->getInfo();
136 30
                $this->errors = array_merge($this->errors, $service->getErrors());
137 30
            }
138 30
        }
139
140 36
        return array_filter($results);
141
    }
142
143
    /**
144
     * Finds all the valid urls inside the given text,
145
     * compares which are allowed and returns an array
146
     * with the detected providers
147
     *
148
     * @param null|string|array $body An array or string with Urls
149
     * @return array
150
     */
151 40
    protected function getProviders($body = '')
152
    {
153 40
        $regex = ($this->config['use_embed_prefix'] === true ? $this->urlEmbedRegex : $this->urlRegex);
154 40
        if (is_array($body)) {
155
156
            $body = array_filter($body, function ($arr) use ($regex) {
157 22
                return preg_match($regex, $arr);
158 22
            });
159
160 22
            $services = $this->providers->getAll($body);
161 40
        } elseif (preg_match_all($regex, $body, $matches)) {
162 18
            $services = $this->providers->getAll($matches['0']);
163 18
        } else {
164 2
            return array();
165
        }
166
167 38
        return $this->clean($services);
168
    }
169
170
    /**
171
     * Adds a new Provider into the service map
172
     *
173
     * @param string $host The host for the map
174
     * @param string|object $class The class or object that should manage the provider
175
     * @param array $params Custom parameters that should be sent in the url for this Provider
176
     * @return void
177
     */
178 2
    public function addProvider($host, $class, array $params = array())
179
    {
180 2
        $this->providers->addProvider($host, $class, $params);
181 2
    }
182
183
    /**
184
     * Strips invalid providers from the list
185
     *
186
     * @param array $services
187
     * @return array
188
     */
189 38
    protected function clean(array $services = array())
190
    {
191 38
        if (empty($services)) {
192 4
            return array();
193
        }
194
195 34
        if (!empty($this->config['allow'])) {
196 2
            $allow = array_map('strtolower', (array) $this->config['allow']);
197
            $services = array_filter($services, function ($arr) use ($allow) {
198 2
                $serviceName = strtolower(basename(str_replace('\\', '/', get_class($arr))));
199 2
                return (in_array($serviceName, $allow));
200 2
            });
201 2
        }
202
203 34
        if (!empty($services) && !empty($this->config['deny'])) {
204 2
            $deny = array_map('strtolower', (array) $this->config['deny']);
205 2
            $services = array_filter($services, function ($arr) use ($deny) {
206 2
                $serviceName = strtolower(basename(str_replace('\\', '/', get_class($arr))));
207 2
                return (!in_array($serviceName, $deny));
208 2
            });
209 2
        }
210
211 34
        return (array) $services;
212
    }
213
214
    /**
215
     * Gets the last error found
216
     *
217
     * @return string
218
     */
219 6
    public function getLastError()
220
    {
221 6
        return end($this->errors);
222
    }
223
224
    /**
225
     * Returns an array with all the errors
226
     *
227
     * @return array
228
     */
229 8
    public function getErrors()
230
    {
231 8
        return $this->errors;
232
    }
233
234
    /**
235
     * Checks if there were errors
236
     *
237
     * @return bool
238
     */
239 6
    public function hasErrors()
240
    {
241 6
        return (!empty($this->errors));
242
    }
243
}
244
245
?>
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...
246