Passed
Push — main ( e3d1bb...64bec1 )
by
unknown
31:25 queued 12s
created

JsonFormatter::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Formatter;
13
14
use BlitzPHP\Container\Services;
15
16
/**
17
 * Formateur de données JSON
18
 */
19
class JsonFormatter implements FormatterInterface
20
{
21
    /**
22
     *{@inheritDoc}
23
     *
24
     * @return false|string Représentation Json d'une valeur
25
     *                      false en cas d'erreur de formattage
26
     */
27
    public function format($data)
28
    {
29
        // Obtenir le paramètre de rappel (si défini)
30 4
        $callback = Services::request()->getQuery('callback');
31
32
        if (empty($callback)) {
33 2
            return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
34
        }
35
36
        // Nous n'honorons qu'un rappel jsonp qui sont des identifiants javascript valides
37
        if (preg_match('/^[a-z_\$][a-z0-9\$_]*(\.[a-z_\$][a-z0-9\$_]*)*$/i', $callback)) {
0 ignored issues
show
Bug introduced by
It seems like $callback can also be of type array; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        if (preg_match('/^[a-z_\$][a-z0-9\$_]*(\.[a-z_\$][a-z0-9\$_]*)*$/i', /** @scrutinizer ignore-type */ $callback)) {
Loading history...
38
            // Renvoie les données sous forme de json encodé avec un rappel
39 2
            return $callback . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
0 ignored issues
show
Bug introduced by
Are you sure $callback of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            return /** @scrutinizer ignore-type */ $callback . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
Loading history...
40
        }
41
42
        // Une fonction de rappel jsonp non valide a été fournie.
43
        // Bien que je ne pense pas que cela devrait être codé en dur ici
44 4
        $data['warning'] = 'INVALID JSONP CALLBACK: ' . $callback;
45
46 4
        return json_encode($data, JSON_UNESCAPED_UNICODE);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     *
52
     * @param string $data Chaine JSON
53
     */
54
    public function parse(string $data): array
55
    {
56 2
        return $data !== '' ? json_decode(trim($data), true) : [];
57
    }
58
}
59