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.

BSON::parse()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 30

Duplication

Lines 19
Ratio 63.33 %

Code Coverage

Tests 3
CRAP Score 39.042

Importance

Changes 0
Metric Value
cc 8
nc 9
nop 1
dl 19
loc 30
ccs 3
cts 14
cp 0.2143
crap 39.042
rs 8.1954
c 0
b 0
f 0
1
<?php
2
3
namespace Nathanmac\Utilities\Parser\Formats;
4
5
use Nathanmac\Utilities\Parser\Exceptions\ParserException;
6
7
/**
8
 * BSON Formatter
9
 *
10
 * @package    Nathanmac\Utilities\Parser\Formats
11
 * @author     Nathan Macnamara <[email protected]>
12
 * @license    https://github.com/nathanmac/Parser/blob/master/LICENSE.md  MIT
13
 */
14
class BSON implements FormatInterface
15
{
16
    /**
17
     * Parse Payload Data
18
     *
19
     * @param string $payload
20
     *
21
     * @throws ParserException
22
     *
23
     * @return array
24
     */
25 3
    public function parse($payload)
26
    {
27 3
        if (function_exists('MongoDB\BSON\toPHP') && ! function_exists('bson_decode')) {
28
            require_once(__DIR__ . '/BSONPolyfill.php');  //@codeCoverageIgnore
29 3
        } elseif ( ! (function_exists('bson_decode') || function_exists('MongoDB\BSON\toPHP'))) {
30
            throw new ParserException('Failed To Parse BSON - Supporting Library Not Available');  // @codeCoverageIgnore
31
        }
32
33 View Code Duplication
        if ($payload) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
            $prevHandler = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
0 ignored issues
show
Unused Code introduced by
The parameter $errfile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errline is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errcontext is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
                throw new \Exception($errstr);  // @codeCoverageIgnore
36
            });
37
38
            try {
39
                $bson = bson_decode(trim($payload, " \t\n\r\x0b"));  // Don't trim \0, as it has valid meaning in BSON
40
                if ( ! $bson) {
41
                    throw new \Exception('Unknown error');  // @codeCoverageIgnore
42
                }
43
            } catch (\Exception $e) {
44
                set_error_handler($prevHandler);
45
                throw new ParserException('Failed To Parse BSON - ' . $e->getMessage());
46
            }
47
48
            set_error_handler($prevHandler);
49
50
            return $bson;
51
        }
52
53
        return [];
54
    }
55
}
56