EmojiPrinter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 82
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A startTestSuite() 0 15 2
A writeProgress() 0 4 1
A writeProgressWithColor() 0 4 1
A projectroot() 0 6 1
A emojifile() 0 7 1
A phpunitxml() 0 4 1
A parse() 0 4 2
A emojiset() 0 4 2
A emojis() 0 4 1
A emojify() 0 4 1
A shortcode() 0 6 1
1
<?php declare(strict_types = 1);
2
3
namespace Coderabbi\Phpumoji;
4
5
use DOMElement;
6
use LitEmoji\LitEmoji;
7
use PHPUnit\Framework\TestSuite;
8
use PHPUnit\TextUI\ResultPrinter;
9
use PHPUnit\Util\Xml;
10
11
final class EmojiPrinter extends ResultPrinter
12
{
13
    const CONFIG = __DIR__ . '/../config';
14
    const EMOJIFILE = '/.emojifile';
15
    const EMOJISET = 'phpumoji';
16
    const SPACER = ' ';
17
18
    private $emojis;
19
20
    public function startTestSuite(TestSuite $suite)
21
    {
22
        $this->emojis = $this->emojis(
23
            $this->emojifile($this->projectroot()),
24
            $this->emojiset($this->phpunitxml())
25
        );
26
27
        if ($this->numTests == -1) {
28
            $this->numTests = count($suite);
29
            $this->numTestsWidth = strlen((string) $this->numTests);
30
            $this->maxColumn = 40 - (2 * $this->numTestsWidth);
31
        }
32
33
        parent::startTestSuite($suite);
34
    }
35
36
    protected function writeProgress($progress)
37
    {
38
        return parent::writeProgress($this->emojify($progress) . self::SPACER);
39
    }
40
41
    protected function writeProgressWithColor($color, $progress)
42
    {
43
        return $this->writeProgress($progress);
44
    }
45
46
    private function projectroot(): string
47
    {
48
        return substr($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], 0, strrpos(
49
            $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], '/')
50
        );
51
    }
52
53
    private function emojifile(string $projectroot): array
54
    {
55
        return array_merge(
56
            $this->parse(self::CONFIG . self::EMOJIFILE),
57
            $this->parse($projectroot . self::EMOJIFILE)
58
        );
59
    }
60
61
    private function phpunitxml(): DOMElement
62
    {
63
        return Xml::loadFile($GLOBALS['__PHPUNIT_CONFIGURATION_FILE'], false, true, true)->documentElement;
64
    }
65
66
    private function parse(string $filepath): array
67
    {
68
        return file_exists($filepath) ? parse_ini_file($filepath, true) : [];
69
    }
70
71
    private function emojiset(DOMElement $phpunitxml): string
72
    {
73
        return $phpunitxml->hasAttribute('emojiset') ? $phpunitxml->getAttribute('emojiset') : self::EMOJISET;
74
    }
75
76
    private function emojis(array $emojifile, string $emojiset): array
77
    {
78
        return $emojifile[$emojiset] ?? $emojifile[self::EMOJISET];
79
    }
80
81
    private function emojify(string $result): string
82
    {
83
        return LitEmoji::encodeUnicode(":{$this->shortcode($result)}:");
84
    }
85
86
    private function shortcode(string $character): string
87
    {
88
        return array_values(array_filter($this->emojis, function($emojikey) use ($character) {
89
            return strrpos(strtoupper($emojikey), $character) === 0;
90
        }, ARRAY_FILTER_USE_KEY))[0] ?? $this->emojis['pass'];
91
    }
92
}
93