Passed
Push — new-api ( 34a0a9...30b18d )
by Sebastian
04:06
created

QuotesRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 64
ccs 35
cts 35
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 19 6
A __construct() 0 5 1
A replaceOuterQuotes() 0 19 1
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        https://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2020 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Styles;
12
13
use Seboettg\CiteProc\Locale\Locale;
14
use Seboettg\CiteProc\Util\StringHelper;
15
16
final class QuotesRenderer implements StylesRendererInterface
17
{
18
    /** @var bool */
19
    private $quotes;
20
21
    /** @var Locale|null */
22
    private $locale;
23
24
    /** @var string|null */
25
    private $suffix;
26
27 139
    public function __construct(bool $quotes, ?Locale $locale, ?string $suffix)
28
    {
29 139
        $this->quotes = $quotes;
30 139
        $this->locale = $locale;
31 139
        $this->suffix = $suffix;
32 139
    }
33
34 101
    public function render(string $text): string
35
    {
36 101
        if ($this->quotes) {
37 12
            $openQuote = $this->locale->filter("terms", "open-quote")->single;
0 ignored issues
show
Bug introduced by
The method filter() does not exist on null. ( Ignorable by Annotation )

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

37
            $openQuote = $this->locale->/** @scrutinizer ignore-call */ filter("terms", "open-quote")->single;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38 12
            $closeQuote = $this->locale->filter("terms", "close-quote")->single;
39 12
            $punctuationInQuotes =$this->locale->filter("options", "punctuation-in-quote");
40 12
            $text = $this->replaceOuterQuotes($text, $openQuote, $closeQuote);
41 12
            if (null !== $punctuationInQuotes || $punctuationInQuotes === false) {
0 ignored issues
show
introduced by
The condition $punctuationInQuotes === false is always true.
Loading history...
42 12
                if (preg_match("/([^\.,;]+)([\.,;]{1,})$/", $text, $match)) {
43 4
                    $punctuation = substr($match[2], -1);
44 4
                    if ($this->suffix !== $punctuation) {
45 1
                        $text = $match[1] . substr($match[2], 0, strlen($match[2]) - 1);
46 1
                        return $openQuote . $text . $closeQuote . $punctuation;
47
                    }
48
                }
49
            }
50 12
            return $openQuote . $text . $closeQuote;
51
        }
52 101
        return $text;
53
    }
54
55
    /**
56
     * @param $text
57
     * @param $outerOpenQuote
58
     * @param $outerCloseQuote
59
     * @return string
60
     */
61 12
    private function replaceOuterQuotes($text, $outerOpenQuote, $outerCloseQuote)
62
    {
63 12
        $innerOpenQuote = $this->locale->filter("terms", "open-inner-quote")->single;
64 12
        $innerCloseQuote = $this->locale->filter("terms", "close-inner-quote")->single;
65 12
        $text = StringHelper::replaceOuterQuotes(
66 12
            $text,
67 12
            "\"",
68 12
            "\"",
69 12
            $innerOpenQuote,
70 12
            $innerCloseQuote
71
        );
72 12
        $text = StringHelper::replaceOuterQuotes(
73 12
            $text,
74 12
            $outerOpenQuote,
75 12
            $outerCloseQuote,
76 12
            $innerOpenQuote,
77 12
            $innerCloseQuote
78
        );
79 12
        return $text;
80
    }
81
}
82