Passed
Push — develop ( e162a8...209be3 )
by Sebastian
14:13 queued 09:03
created

removeConsecutivePunctuation()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 4
rs 10
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 147
    public function removeConsecutivePunctuation($punctuationSign, $subject): string
41
    {
42 147
        if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) {
43 55
            return $subject;
44
        }
45 125
        $pattern = '/'.preg_quote(trim($punctuationSign), '/').'{2,}/';
46 125
        if (preg_match($pattern, $subject)) {
47 1
            return preg_replace($pattern, $punctuationSign, $subject);
48
        }
49 125
        return $subject;
50
    }
51
52
    /**
53
     * @param $child
54
     */
55 170
    protected function getChildrenAffixesAndDelimiter($child)
56
    {
57 170
        if (method_exists($child, "renderPrefix")) {
58 166
            if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) {
59 48
                $this->childrenPrefixes[] = $child->renderPrefix();
60
            }
61
        }
62 170
        if (method_exists($child, "renderSuffix")) {
63 166
            if (!empty($child->renderSuffix()) && !in_array($child->renderSuffix(), $this->childrenSuffixes)) {
64 46
                $this->childrenSuffixes[] = $child->renderSuffix();
65
            }
66
        }
67 170
        if (method_exists($child, "getDelimiter")) {
68 136
            if (!empty($child->getDelimiter()) && !in_array($child->getDelimiter(), $this->childrenDelimiter)) {
69 130
                $this->childrenDelimiter[] = $child->getDelimiter();
70
            }
71
        }
72 170
    }
73
74
    /**
75
     * @param string $string
76
     * @return string
77
     */
78 175
    protected function removeConsecutiveChars($string)
79
    {
80 175
        foreach ($this->childrenPrefixes as $prefix) {
81 48
            $string = $this->removeConsecutivePunctuation($prefix, $string);
82
        }
83 175
        foreach ($this->childrenSuffixes as $suffix) {
84 46
            $string = $this->removeConsecutivePunctuation($suffix, $string);
85
        }
86 175
        foreach ($this->childrenDelimiter as $delimiter) {
87 126
            $string = $this->removeConsecutivePunctuation($delimiter, $string);
88
        }
89
90 175
        $string = preg_replace("/\s{2,}/", " ", $string);
91
92 175
        return $string;
93
    }
94
}
95