Passed
Push — latest ( 0eb2c6...9ae518 )
by Mark
03:09
created

Converter::loadLocalePreset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.9332
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji;
6
7
use UnicornFail\Emoji\Token\AbstractEmojiToken;
8
9
final class Converter
10
{
11
    /** @var Parser|ParserInterface */
12
    private $parser;
13
14
    /**
15
     * @param mixed[]|\Traversable $configuration
16
     */
17 606
    public function __construct(?iterable $configuration = null, ?Dataset $dataset = null, ?ParserInterface $parser = null)
18
    {
19 606
        $this->parser = $parser ?? new Parser($configuration, $dataset);
20 192
    }
21
22
    /**
23
     * @param mixed[]|\Traversable $configuration
24
     */
25 63
    public static function create(?iterable $configuration = null): self
26
    {
27 63
        return new self($configuration);
28
    }
29
30 81
    public function convert(string $input, ?int $type = null): string
31
    {
32 81
        $stringableType = (int) $this->parser->getConfiguration()->get('stringableType');
33
34
        // Parse.
35 81
        $tokens = $this->getParser()->parse($input);
36
37
        // Ensure tokens are set to the correct stringable type.
38 81
        if ($type !== null && $type !== $stringableType) {
39 39
            foreach (AbstractEmojiToken::filter($tokens) as $token) {
40 39
                $token->setStringableType($type);
0 ignored issues
show
Bug introduced by
The method setStringableType() does not exist on UnicornFail\Emoji\Token\AbstractToken. It seems like you code against a sub-type of UnicornFail\Emoji\Token\AbstractToken such as UnicornFail\Emoji\Token\AbstractEmojiToken. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
                $token->/** @scrutinizer ignore-call */ 
41
                        setStringableType($type);
Loading history...
41
            }
42
        }
43
44 81
        return \implode($tokens);
45
    }
46
47 3
    public function convertToEmoticon(string $input): string
48
    {
49 3
        return $this->convert($input, Lexer::T_EMOTICON);
50
    }
51
52 6
    public function convertToHtml(string $input): string
53
    {
54 6
        return $this->convert($input, Lexer::T_HTML_ENTITY);
55
    }
56
57 6
    public function convertToShortcode(string $input): string
58
    {
59 6
        return $this->convert($input, Lexer::T_SHORTCODE);
60
    }
61
62 15
    public function convertToUnicode(string $input): string
63
    {
64 15
        return $this->convert($input, Lexer::T_UNICODE);
65
    }
66
67 81
    public function getParser(): ParserInterface
68
    {
69 81
        return $this->parser;
70
    }
71
}
72