Passed
Pull Request — master (#1013)
by lee
07:38
created

Converter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 53
ccs 15
cts 21
cp 0.7143
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertBody() 0 9 2
A __construct() 0 3 1
A convertFrontmatter() 0 21 6
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Converter;
12
13
use Cecil\Builder;
14
use Cecil\Exception\Exception;
15
use Symfony\Component\Yaml\Exception\ParseException;
16
use Symfony\Component\Yaml\Yaml;
17
18
/**
19
 * Class Converter.
20
 */
21
class Converter implements ConverterInterface
22
{
23
    /** @var Builder */
24
    protected $builder;
25
26
    /**
27
     * @param Builder $builder
28
     */
29 1
    public function __construct(Builder $builder)
30
    {
31 1
        $this->builder = $builder;
32 1
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function convertFrontmatter(string $string, string $type = 'yaml'): array
38
    {
39
        switch ($type) {
40 1
            case 'ini':
41
                $result = parse_ini_string($string);
42
                if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
                    throw new Exception('Can\'t parse INI front matter');
44
                }
45
46
                return $result;
47 1
            case 'yaml':
48
            default:
49
                try {
50 1
                    $result = Yaml::parse((string) $string);
51 1
                    if (!is_array($result)) {
52 1
                        throw new Exception('Parse result of YAML front matter is not an array');
53
                    }
54
55 1
                    return $result;
56 1
                } catch (ParseException $e) {
57 1
                    throw new Exception($e->getMessage());
58
                }
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function convertBody(string $string): string
66
    {
67
        try {
68 1
            $parsedown = new Parsedown($this->builder);
69
        } catch (\Exception $e) {
70
            throw new Exception('Can\'t convert Markdown.');
71
        }
72
73 1
        return $parsedown->text($string);
74
    }
75
}
76