Completed
Pull Request — master (#19)
by Flo
04:12
created

Highlighter   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 56
rs 10
c 1
b 0
f 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 1
1
<?php
2
/**
3
 * Class Highlighter | Highlighter.php
4
 * @package Faulancer\View\Helper
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\View\Helper;
8
9
use Faulancer\View\AbstractViewHelper;
10
use Faulancer\View\ViewController;
11
12
/**
13
 * Class Highlighter
14
 */
15
class Highlighter extends AbstractViewHelper
16
{
17
    
18
    private static $keys = [
19
        '\bif',
20
        'else',
21
        'new',
22
        'throw',
23
        'echo',
24
        'return',
25
        'null',
26
        'public',
27
        'private',
28
        'protected',
29
        'function',
30
        'class[?!class\=|?!\$class]',
31
        'extends',
32
        'implements',
33
        'use',
34
        'namespace',
35
        'unset',
36
        'include_once',
37
        'require_once',
38
        'include',
39
        'require',
40
        'foreach',
41
        'for',
42
        'try',
43
        'catch',
44
        '\bas\b',
45
        '&lt;\?php',
46
        '&lt;\?=',
47
        '\?&gt;\n'
48
    ];
49
50
    /**
51
     * @param ViewController $view
52
     * @param string         $data
53
     * @param string         $language
54
     * @return mixed|string
55
     * @codeCoverageIgnore
56
     */
57
    public function __invoke(ViewController $view, $data = '', $language = 'php')
58
    {
59
        $data = htmlentities($data, ENT_IGNORE, null, false);
60
        $data = preg_replace('/(' . implode('\b|\b', self::$keys) . ')/', '<span class="base_keys">$1</span>', $data);
61
        $data = preg_replace('/\$(\w+)/', '<span class="variable">$$1</span>', $data);
62
        $data = preg_replace('/\'(.*)\'/', '<span class="string_text">\'$1\'</span>', $data);
63
        $data = preg_replace('/-&gt;(\w+)/', '-><span class="methods">$1</span>', $data);
64
        $data = preg_replace('/\/\*\*(.*)\*\//s', '<span class="annotation">/**$1*/</span>', $data);
65
        $data = preg_replace('/(_*)([A-Z]{2,})(_*)/', '<span class="constant">$1$2$3</span>', $data);
66
67
        return $data;
68
    }
69
70
}