Completed
Push — master ( edcce7...6aac40 )
by Kacper
04:23
created

CliFormatter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 72.21%

Importance

Changes 0
Metric Value
dl 8
loc 44
rs 10
c 0
b 0
f 0
ccs 13
cts 18
cp 0.7221
wmc 8
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A format() 0 4 1
A token() 0 12 4
A formatLineStart() 0 4 1
A formatLineEnd() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Formatter;
17
18
use Kadet\Highlighter\Parser\Token\Token;
19
use Kadet\Highlighter\Parser\Tokens;
20
use Kadet\Highlighter\Utils\ArrayHelper;
21
use Kadet\Highlighter\Utils\Console;
22
23
/**
24
 * Class CliFormatter
25
 *
26
 * @package Kadet\Highlighter\Formatter
27
 */
28
class CliFormatter extends AbstractFormatter implements FormatterInterface
29
{
30
    private $_styles = [];
31
32
    /**
33
     * CliFormatter constructor.
34
     */
35 36 View Code Duplication
    public function __construct(array $options = [])
36
    {
37 36
        parent::__construct(array_replace_recursive([
38
            'styles' => include __DIR__.'/../Styles/Cli/Default.php'
39 36
        ], $options));
40
41 36
        $this->_styles = $this->_options['styles'];
42 36
    }
43
44 2
    public function format(Tokens $tokens)
45
    {
46 2
        return parent::format($tokens).Console::reset();
47
    }
48
49 2
    protected function token(Token $token)
50
    {
51 2
        $style = ArrayHelper::resolve($this->_styles, $token->name);
52
53 2
        if ($style === null) {
54
            return null;
55
        }
56
57 2
        return $token->isStart()
58 2
            ? Console::open(is_callable($style) ? $style($token) : $style)
59 2
            : Console::close();
60
    }
61
62
    protected function formatLineStart($line)
63
    {
64
        return str_pad($line, 5, ' ', STR_PAD_LEFT) . ' | '.Console::set(Console::current());
65
    }
66
67
    protected function formatLineEnd($line)
68
    {
69
        return Console::reset();
70
    }
71
}
72