Failed Conditions
Push — psr2 ( de3699...36dc94 )
by Andreas
06:50 queued 03:31
created

Acronym   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 64
c 0
b 0
f 0
rs 10
wmc 10
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A preConnect() 0 8 2
A connectTo() 0 8 3
A getSort() 0 4 1
A compare() 0 12 3
1
<?php
2
3
namespace dokuwiki\ParserMode;
4
5
use dokuwiki\Action\AbstractAclAction;
6
7
class Acronym extends AbstractMode
8
{
9
    // A list
10
    protected $acronyms = array();
11
    protected $pattern = '';
12
13
    /**
14
     * Acronym constructor.
15
     *
16
     * @param string[] $acronyms
17
     */
18
    public function __construct($acronyms)
19
    {
20
        usort($acronyms, array($this,'compare'));
21
        $this->acronyms = $acronyms;
22
    }
23
24
    /** @inheritdoc */
25
    public function preConnect()
26
    {
27
        if (!count($this->acronyms)) return;
28
29
        $bound = '[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]';
30
        $acronyms = array_map('Doku_Lexer_Escape', $this->acronyms);
31
        $this->pattern = '(?<=^|'.$bound.')(?:'.join('|', $acronyms).')(?='.$bound.')';
32
    }
33
34
    /** @inheritdoc */
35
    public function connectTo($mode)
36
    {
37
        if (!count($this->acronyms)) return;
38
39
        if (strlen($this->pattern) > 0) {
40
            $this->Lexer->addSpecialPattern($this->pattern, $mode, 'acronym');
41
        }
42
    }
43
44
    /** @inheritdoc */
45
    public function getSort()
46
    {
47
        return 240;
48
    }
49
50
    /**
51
     * sort callback to order by string length descending
52
     *
53
     * @param string $a
54
     * @param string $b
55
     *
56
     * @return int
57
     */
58
    protected function compare($a, $b)
59
    {
60
        $a_len = strlen($a);
61
        $b_len = strlen($b);
62
        if ($a_len > $b_len) {
63
            return -1;
64
        } elseif ($a_len < $b_len) {
65
            return 1;
66
        }
67
68
        return 0;
69
    }
70
}
71