Completed
Push — develop ( b39487...b8a687 )
by Yitzchok
01:27
created

EmojiPrinter::shortcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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