Completed
Push — master ( 90ac5a...649bdf )
by Amine
11s
created

Console   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 16.28 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 6
dl 14
loc 86
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 2
A stdin() 0 7 2
A stdout() 7 7 2
A stderr() 7 7 2
A outTransformer() 0 8 2
A out() 0 5 1
A line() 0 4 1
A error() 0 6 1
A read() 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 namespace Tarsana\Command\Console;
2
3
use Tarsana\Command\Console\OutTransformer;
4
use Tarsana\Command\Interfaces\Console\ConsoleInterface;
5
use Tarsana\Command\Interfaces\Console\TransformerInterface;
6
use Tarsana\IO\Interfaces\Resource\Reader as ReaderInterface;
7
use Tarsana\IO\Interfaces\Resource\Writer as WriterInterface;
8
use Tarsana\IO\Resource\Reader;
9
use Tarsana\IO\Resource\Writer;
10
use Tarsana\Syntax\Syntax;
11
12
class Console implements ConsoleInterface {
13
14
    protected $out;
15
    protected $in;
16
    protected $err;
17
    protected $outTransformer;
18
19
    public function __construct()
20
    {
21
        $this->out = new Writer;
22
        $this->err = new Writer('php://stderr');
23
        $this->in  = new Reader;
24
        $this->outTransformer = new OutTransformer;
25
        $aliases = [
26
            '<info>'  => '<color:33>',
27
            '</info>' => '<reset>',
28
            '<warn>'  => '<color:220>',
29
            '</warn>' => '<reset>',
30
            '<success>'  => '<color:46>',
31
            '</success>' => '<reset>',
32
            '<error>' => '<bold><color:15><background:124>',
33
            '</error>' => '<reset>',
34
            '<tab>' => '    ',
35
            '<br>'  => PHP_EOL
36
        ];
37
        foreach ($aliases as $name => $value) {
38
            $this->outTransformer->alias($name, $value);
39
        }
40
    }
41
42
    public function stdin(ReaderInterface $in = null)
43
    {
44
        if (null === $in)
45
            return $this->in;
46
        $this->in = $in;
47
        return $this;
48
    }
49
50 View Code Duplication
    public function stdout(WriterInterface $out = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        if (null === $out)
53
            return $this->out;
54
        $this->out = $out;
55
        return $this;
56
    }
57
58 View Code Duplication
    public function stderr(WriterInterface $err = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if (null === $err)
61
            return $this->err;
62
        $this->err = $err;
63
        return $this;
64
    }
65
66
    public function outTransformer(TransformerInterface $value = null)
67
    {
68
        if (null === $value) {
69
            return $this->outTransformer;
70
        }
71
        $this->outTransformer = $value;
72
        return $this;
73
    }
74
75
    public function out(string $text) : ConsoleInterface
76
    {
77
        $this->out->write($this->outTransformer->transform($text));
78
        return $this;
79
    }
80
81
    public function line(string $text) : ConsoleInterface
82
    {
83
        return $this->out($text . '<br>');
84
    }
85
86
    public function error(string $text) : ConsoleInterface
87
    {
88
        $text = "<error>{$text}</error><br>";
89
        $this->err->write($this->outTransformer->transform($text));
90
        return $this;
91
    }
92
93
    public function read() : string
94
    {
95
        return $this->in->read();
96
    }
97
}
98