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.

Parser   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 18
c 10
b 1
f 1
lcom 1
cbo 2
dl 0
loc 118
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 12 2
A dump() 0 16 4
A isValid() 0 5 1
A parseHeader() 0 13 4
C dumpIni() 0 25 7
1
<?php
2
namespace VKBansal\FrontMatter;
3
4
use Symfony\Component\Yaml\Yaml;
5
6
/**
7
 * FrontMatter Parser
8
 * @package VKBansal\FrontMatter\Parser
9
 * @version 1.3.0
10
 * @author Vivek Kumar Bansal <[email protected]>
11
 * @license MIT
12
 */
13
class Parser
14
{
15
    /**
16
     * Regex for seperators
17
     * @var string
18
     */
19
    private static $matcherRegex = "/^-{3}\s?(\w*)\r?\n(.*)\r?\n-{3}\r?\n(.*)/s";
20
21
    /**
22
     * Constanst for Document::dump()
23
     * @see Document::dump()
24
     */
25
    const DUMP_YAML = 'yaml';
26
    const DUMP_JSON = 'json';
27
    const DUMP_INI  = 'ini';
28
29
    /**
30
     * Parse the given content
31
     * @param  string $input content to be parsed
32
     * @return Document
33
     */
34
    public static function parse($input)
35
    {
36
        if (preg_match(self::$matcherRegex, $input, $matches)) {
37
            $header = self::parseHeader($matches[2], strtolower($matches[1]));
38
            $content = $matches[3];
39
        } else {
40
            $content = $input;
41
            $header = [];
42
        }
43
44
        return new Document($content, $header);
0 ignored issues
show
Bug introduced by
It seems like $header defined by self::parseHeader($match...trtolower($matches[1])) on line 37 can also be of type string; however, VKBansal\FrontMatter\Document::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
    }
46
47
    /**
48
     * Convert a Document to string
49
     * @param  Document $document
50
     * @return string
51
     */
52
    public static function dump (Document $document, $mode = null)
53
    {
54
        switch ($mode) {
55
            case 'yaml':
56
                return "--- yaml\n".Yaml::dump($document->getConfig())."---\n".$document->getContent();
57
            
58
            case 'json':
59
                return "--- json\n".json_encode($document->getConfig(), JSON_PRETTY_PRINT)."\n---\n".$document->getContent();
60
            
61
            case 'ini':
62
                return "--- ini\n".self::dumpIni($document->getConfig())."---\n".$document->getContent();
63
64
            default:
65
                return "---\n".Yaml::dump($document->getConfig())."---\n".$document->getContent();
66
        }
67
    }
68
69
    /**
70
     * Determines if given content is valid 
71
     * @param  string  $input  Input to be validated
72
     * @return boolean         True if valid else false 
73
     */
74
    public static function isValid($input)
75
    {
76
        return preg_match(self::$matcherRegex, $input) === 1;
77
78
    }
79
80
    /**
81
     * Parses header
82
     * @param  string $header
83
     * @param  string $parser
84
     * @return array|string
85
     */
86
    private static function parseHeader($header, $parser)
87
    {
88
        switch($parser){
89
            case 'yaml':
90
                return Yaml::parse($header);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Symfony\Component\Yaml\Yaml::parse($header); of type array|string|stdClass adds the type stdClass to the return on line 90 which is incompatible with the return type documented by VKBansal\FrontMatter\Parser::parseHeader of type array|string.
Loading history...
91
            case 'json':
92
                return json_decode($header, true);
93
            case 'ini':
94
                return parse_ini_string($header, true);
95
            default:
96
                return Yaml::parse($header);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Symfony\Component\Yaml\Yaml::parse($header); of type array|string|stdClass adds the type stdClass to the return on line 96 which is incompatible with the return type documented by VKBansal\FrontMatter\Parser::parseHeader of type array|string.
Loading history...
97
        }
98
    }
99
100
    /**
101
     * Encodes PHP array to ini string
102
     * @param  array  $config
103
     * @return string
104
     */
105
    private static function dumpIni(array $config)
106
    {
107
        $sections = $globals = '';
108
        
109
        if (!empty($config)) {
110
            foreach ($config as $section => $item) {
111
                if (!is_array($item)) {
112
                    //To write globals at top
113
                    $globals .= "{$section} = {$item}\n";
114
                } else {
115
                    $sections = "\n[{$section}]\n";
116
                    foreach ($item as $key => $value) {
117
                        if (is_array($value)) {
118
                            foreach ($value as $arrKey => $arrValue) {
119
                                $sections .= "{$key}[{$arrKey}] = $arrValue\n";
120
                            }
121
                        } else {
122
                            $sections .= "{$key} = {$value}\n";
123
                        }
124
                    }
125
                }
126
            }
127
        }
128
        return $globals.$sections;
129
    }
130
}
131