• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 7168486434

11 Dec 2023 01:57PM UTC coverage: 83.362% (+0.3%) from 83.077%
7168486434

push

github

ArnaudLigny
refactor: PHP 8 Exception

4 of 17 new or added lines in 8 files covered. (23.53%)

105 existing lines in 2 files now uncovered.

2866 of 3438 relevant lines covered (83.36%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/src/Converter/Converter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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;
1✔
34
    }
35

36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @throws RuntimeException
40
     */
41
    public function convertFrontmatter(string $string, string $format = 'yaml'): array
42
    {
43
        if (!\in_array($format, ['yaml', 'ini', 'toml', 'json'])) {
1✔
44
            throw new RuntimeException(sprintf('The front matter format "%s" is not supported ("yaml", "ini", "toml" or "json").', $format));
×
45
        }
46
        $method = sprintf('convert%sToArray', ucfirst($format));
1✔
47

48
        return self::$method($string);
1✔
49
    }
50

51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function convertBody(string $string): string
55
    {
56
        $parsedown = new Parsedown($this->builder);
1✔
57

58
        return $parsedown->text($string);
1✔
59
    }
60

61
    /**
62
     * Converts YAML string to array.
63
     *
64
     * @see https://wikipedia.org/wiki/YAML
65
     */
66
    private static function convertYamlToArray(string $string): array
67
    {
68
        try {
69
            $result = Yaml::parse((string) $string) ?? [];
1✔
70
            if (!\is_array($result)) {
1✔
71
                throw new RuntimeException('Can\'t parse YAML front matter.');
×
72
            }
73

74
            return $result;
1✔
75
        } catch (ParseException $e) {
1✔
76
            throw new RuntimeException($e->getMessage(), null, $e->getParsedLine());
1✔
77
        } catch (\Exception $e) {
×
78
            throw new RuntimeException($e->getMessage());
×
79
        }
80
    }
81

82
    /**
83
     * Converts INI string to array.
84
     *
85
     * @see https://wikipedia.org/wiki/INI_file
86
     */
87
    private static function convertIniToArray(string $string): array
88
    {
89
        $result = parse_ini_string($string, true);
×
90
        if ($result === false) {
×
91
            throw new RuntimeException('Can\'t parse INI front matter.');
×
92
        }
93

94
        return $result;
×
95
    }
96

97
    /**
98
     * Converts TOML string to array.
99
     *
100
     * @see https://wikipedia.org/wiki/TOML
101
     */
102
    private static function convertTomlToArray(string $string): array
103
    {
104
        try {
105
            $result = Toml::Parse((string) $string) ?? [];
×
106
            if (!\is_array($result)) {
×
107
                throw new RuntimeException('Can\'t parse TOML front matter.');
×
108
            }
109

110
            return $result;
×
111
        } catch (TomlParseException $e) {
×
112
            throw new RuntimeException($e->getMessage(), $e->getParsedFile(), $e->getParsedLine());
×
113
        } catch (\Exception $e) {
×
114
            throw new RuntimeException($e->getMessage());
×
115
        }
116
    }
117

118
    /**
119
     * Converts JSON string to array.
120
     *
121
     * @see https://wikipedia.org/wiki/JSON
122
     */
123
    private static function convertJsonToArray(string $string): array
124
    {
125
        try {
126
            $result = json_decode($string, true);
×
127
            if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
×
128
                throw new \Exception('JSON error.');
×
129
            }
130

131
            return $result;
×
NEW
132
        } catch (\Exception) {
×
133
            throw new RuntimeException('Can\'t parse JSON front matter.');
×
134
        }
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc