Passed
Push — master ( 661b73...ccb1dd )
by Sebastian
08:54 queued 05:17
created

ConsecutivePunctuationCharacterTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 77
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A removeConsecutiveChars() 0 15 4
A removeConsecutivePunctuation() 0 11 4
B getChildsAffixesAndDelimiter() 0 15 10
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
15
 * @author Sebastian Böttger <[email protected]>
1 ignored issue
show
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 1
Loading history...
16
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
17
trait ConsecutivePunctuationCharacterTrait
18
{
19
20
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
     * @var array
22
     */
23
    private $childrenPrefixes = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "childrenPrefixes" must be prefixed with an underscore
Loading history...
24
25
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @var array
27
     */
28
    private $childrenSuffixes = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "childrenSuffixes" must be prefixed with an underscore
Loading history...
29
30
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @var array
32
     */
33
    private $childrenDelimiter = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "childrenDelimiter" must be prefixed with an underscore
Loading history...
34
35
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $punctuationSign should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $subject should have a doc-comment as per coding-style.
Loading history...
36
     * @param $punctuationSign
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
37
     * @param $subject
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
38
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
39
     */
40 92
    public function removeConsecutivePunctuation($punctuationSign, $subject)
41
    {
42 92
        if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) {
43 14
            return $subject;
44
        }
45 91
        $pattern = '/' . preg_quote(trim($punctuationSign)) . '{2,}/';
46 91
        if (preg_match($pattern, $subject)) {
47 4
            $res = preg_replace($pattern, $punctuationSign, $subject);
48 4
            return $res;
49
        }
50 89
        return $subject;
51
    }
52
53
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $child should have a doc-comment as per coding-style.
Loading history...
54
     * @param $child
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56 132
    protected function getChildsAffixesAndDelimiter($child)
57
    {
58 132
        if (method_exists($child, "renderPrefix")) {
59 131
            if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) {
60 36
                $this->childrenPrefixes[] = $child->renderPrefix();
61
            }
62
        }
63 132
        if (method_exists($child, "renderSuffix")) {
64 131
            if (!empty($child->renderSuffix()) && !in_array($child->renderSuffix(), $this->childrenSuffixes)) {
65 31
                $this->childrenSuffixes[] = $child->renderSuffix();
66
            }
67
        }
68 132
        if (method_exists($child, "getDelimiter")) {
69 79
            if (!empty($child->getDelimiter()) && !in_array($child->getDelimiter(), $this->childrenDelimiter)) {
70 79
                $this->childrenDelimiter[] = $child->getDelimiter();
71
            }
72
        }
73 132
    }
74
75
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
76
     * @param string $string
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
77
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
78
     */
79 136
    protected function removeConsecutiveChars($string)
80
    {
81 136
        foreach ($this->childrenPrefixes as $prefix) {
82 36
            $string = $this->removeConsecutivePunctuation($prefix, $string);
83
        }
84 136
        foreach ($this->childrenSuffixes as $suffix) {
85 31
            $string = $this->removeConsecutivePunctuation($suffix, $string);
86
        }
87 136
        foreach ($this->childrenDelimiter as $delimiter) {
88 79
            $string = $this->removeConsecutivePunctuation($delimiter, $string);
89
        }
90
91 136
        $string = preg_replace("/\s{2,}/", " ", $string);
92
93 136
        return $string;
94
    }
95
}