Completed
Push — 57-formatter-per-extension ( 73b3c8 )
by Nicolas
40:17 queued 36:16
created

OutputAware   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 49
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 52
rs 8.5455

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setOutput() 0 6 1
A error() 0 4 1
A warning() 0 4 1
A info() 0 4 1
A debug() 0 4 1
A write() 0 19 4

How to fix   Complexity   

Complex Class

Complex classes like OutputAware often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OutputAware, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Karma\Logging;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
trait OutputAware
8
{
9
    protected
10
        $output = null;
11
12
    public function setOutput(OutputInterface $output)
13
    {
14
        $this->output = $output;
15
        
16
        return $this;
17
    }
18
19
    protected function error($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
20
    {
21
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'red');
22
    }
23
    
24
    protected function warning($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
25
    {
26
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'yellow');
27
    }
28
    
29
    protected function info($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
30
    {
31
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_NORMAL, 'white');
32
    }
33
    
34
    protected function debug($messages, $newline = false, $type = OutputInterface::OUTPUT_NORMAL)
35
    {
36
        return $this->write($messages, $newline, $type, OutputInterface::VERBOSITY_VERBOSE, 'white');
37
    }
38
    
39
    private function write($messages, $newline, $type, $verbosity, $textColor)
40
    {
41
        if($this->output instanceof OutputInterface)
42
        {
43
            if($verbosity <= $this->output->getVerbosity())
44
            {
45
                if(! is_array($messages))
46
                {
47
                    $messages = array($messages);
48
                }
49
    
50
                array_walk($messages, function(& $message) use($textColor) {
51
                    $message = "<fg=$textColor>$message</fg=$textColor>";
52
                });
53
    
54
                $this->output->write($messages, $newline, $type);
55
            }
56
        }
57
    }
58
}