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

Acronym::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 12
c 0
b 0
f 0
cc 3
eloc 8
nop 2
rs 9.4285
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