Passed
Push — new-api ( 4bfe18...7ec1cc )
by Sebastian
05:06
created

removeConsecutiveChars()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 1
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Styles;
11
12
/**
13
 * Trait ConsecutivePunctuationCharacterTrait
14
 * @package Seboettg\CiteProc\Styles
15
 * @author Sebastian Böttger <[email protected]>
16
 */
17
trait ConsecutivePunctuationCharacterTrait
18
{
19
20
    /**
21
     * @var array
22
     */
23
    private $childrenPrefixes = [];
24
25
    /**
26
     * @var array
27
     */
28
    private $childrenSuffixes = [];
29
30
    /**
31
     * @var array
32
     */
33
    private $childrenDelimiter = [];
34
35
    /**
36
     * @param $punctuationSign
37
     * @param $subject
38
     * @return string
39
     */
40 66
    public function removeConsecutivePunctuation($punctuationSign, $subject)
41
    {
42 66
        if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) {
43 39
            return $subject;
44
        }
45 34
        $pattern = '/' . preg_quote(trim($punctuationSign), '/') . '{2,}/';
46 34
        if (preg_match($pattern, $subject)) {
47
            return preg_replace($pattern, $punctuationSign, $subject);
48
        }
49 34
        return $subject;
50
    }
51
52
    /**
53
     * @param $child
54
     */
55 169
    protected function getChildrenAffixesAndDelimiter($child)
56
    {
57 169
        if (method_exists($child, "renderPrefix")) {
58
            if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) {
59
                $this->childrenPrefixes[] = $child->renderPrefix();
60
            }
61
        }
62 169
        if (method_exists($child, "renderSuffix")) {
63
            if (!empty($child->renderSuffix()) && !in_array($child->renderSuffix(), $this->childrenSuffixes)) {
64
                $this->childrenSuffixes[] = $child->renderSuffix();
65
            }
66
        }
67 169
        if (method_exists($child, "getDelimiter")) {
68 82
            if (!empty($child->getDelimiter()) && !in_array($child->getDelimiter(), $this->childrenDelimiter)) {
69 69
                $this->childrenDelimiter[] = $child->getDelimiter();
70
            }
71
        }
72 169
    }
73
74
    /**
75
     * @param string $string
76
     * @return string
77
     */
78 173
    protected function removeConsecutiveChars($string)
79
    {
80 173
        foreach ($this->childrenPrefixes as $prefix) {
81
            $string = $this->removeConsecutivePunctuation($prefix, $string);
82
        }
83 173
        foreach ($this->childrenSuffixes as $suffix) {
84
            $string = $this->removeConsecutivePunctuation($suffix, $string);
85
        }
86 173
        foreach ($this->childrenDelimiter as $delimiter) {
87 66
            $string = $this->removeConsecutivePunctuation($delimiter, $string);
88
        }
89
90 173
        $string = preg_replace("/\s{2,}/", " ", $string);
91
92 173
        return $string;
93
    }
94
}
95