Completed
Push — master ( dd8846...82fafa )
by Amine
03:04
created

Console::alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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));
0 ignored issues
show
Bug introduced by
The method transform does only exist in Tarsana\Command\Console\OutTransformer, but not in Tarsana\Command\Console\Console.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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));
0 ignored issues
show
Bug introduced by
The method transform does only exist in Tarsana\Command\Console\OutTransformer, but not in Tarsana\Command\Console\Console.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90
        return $this;
91
    }
92
93
    public function read() : string
94
    {
95
        return $this->in->read();
96
    }
97
98
    public function readLine() : string
99
    {
100
        return $this->in->readLine();
101
    }
102
103
    public function alias(string $name, string $value) : ConsoleInterface
104
    {
105
        $this->outTransformer->alias($name, $value);
106
        return $this;
107
    }
108
}
109