Passed
Push — converter ( a025de )
by Arnaud
03:58
created

Converter::convertFrontmatter()   C

Complexity

Conditions 15
Paths 27

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 15
eloc 35
c 1
b 1
f 0
nc 27
nop 2
dl 0
loc 51
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Converter;
15
16
use Cecil\Builder;
17
use Cecil\Exception\RuntimeException;
18
use Symfony\Component\Yaml\Exception\ParseException;
19
use Symfony\Component\Yaml\Yaml;
20
use Yosymfony\Toml\Exception\ParseException as TomlParseException;
21
use Yosymfony\Toml\Toml;
22
23
/**
24
 * Class Converter.
25
 */
26
class Converter implements ConverterInterface
27
{
28
    /** @var Builder */
29
    protected $builder;
30
31
    public function __construct(Builder $builder)
32
    {
33
        $this->builder = $builder;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @throws RuntimeException
40
     */
41
    public function convertFrontmatter(string $string, string $format = 'yaml'): array
42
    {
43
        switch ($format) {
44
            // https://wikipedia.org/wiki/INI_file
45
            case 'ini':
46
                $result = parse_ini_string($string, true);
47
                if ($result === false) {
48
                    throw new RuntimeException('Can\'t parse INI front matter.');
49
                }
50
51
                return $result;
52
            // https://wikipedia.org/wiki/JSON
53
            case 'json':
54
                try {
55
                    $result = json_decode($string, true);
56
                    if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
57
                        throw new \Exception('JSON error.');
58
                    }
59
                } catch (\Exception $e) {
60
                    throw new RuntimeException('Can\'t parse JSON front matter.');
61
                }
62
63
                return $result;
64
            // https://wikipedia.org/wiki/TOML
65
            case 'toml':
66
                try {
67
                    $result = Toml::Parse((string) $string) ?? [];
68
                    if (!is_array($result)) {
69
                        throw new RuntimeException('Can\'t parse TOML front matter.');
70
                    }
71
72
                    return $result;
73
                } catch (TomlParseException $e) {
74
                    throw new RuntimeException($e->getMessage(), $e->getParsedFile(), $e->getParsedLine());
75
                } catch (\Exception $e) {
76
                    throw new RuntimeException($e->getMessage());
77
                }
78
            // https://wikipedia.org/wiki/YAML
79
            case 'yaml':
80
            default:
81
                try {
82
                    $result = Yaml::parse((string) $string) ?? [];
83
                    if (!is_array($result)) {
84
                        throw new RuntimeException('Can\'t parse YAML front matter.');
85
                    }
86
87
                    return $result;
88
                } catch (ParseException $e) {
89
                    throw new RuntimeException($e->getMessage(), $e->getParsedFile(), $e->getParsedLine());
90
                } catch (\Exception $e) {
91
                    throw new RuntimeException($e->getMessage());
92
                }
93
        }
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function convertBody(string $string): string
100
    {
101
        $parsedown = new Parsedown($this->builder);
102
103
        return $parsedown->text($string);
104
    }
105
}
106