ConsoleFormatter   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 101
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeHeader() 0 14 3
D writeColor() 0 43 17
A write() 0 4 1
A writeArray() 0 4 1
1
<?php
2
/**
3
 * Scabbia2 Formatters Component
4
 * https://github.com/eserozvataf/scabbia2
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @link        https://github.com/eserozvataf/scabbia2-formatters for the canonical source repository
10
 * @copyright   2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/)
11
 * @license     http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0
12
 */
13
14
namespace Scabbia\Formatters;
15
16
use Scabbia\Formatters\FormatterInterface;
17
18
/**
19
 * Implementation for generation of console-compatible output
20
 *
21
 * @package     Scabbia\Formatters
22
 * @author      Eser Ozvataf <[email protected]>
23
 * @since       2.0.0
24
 *
25
 * @todo add stdin features
26
 */
27
class ConsoleFormatter implements FormatterInterface
28
{
29
    /**
30
     * Writes given message in header format
31
     *
32
     * @param int    $uHeading size
33
     * @param string $uMessage message
34
     *
35
     * @return void
36
     */
37
    public function writeHeader($uHeading, $uMessage)
38
    {
39
        if ($uHeading === 1) {
40
            $tChar = "=";
41
        } else {
42
            $tChar = "-";
43
        }
44
45
        echo $uMessage, PHP_EOL, str_repeat($tChar, strlen($uMessage)), PHP_EOL;
46
47
        if ($uHeading === 1) {
48
            echo PHP_EOL;
49
        }
50
    }
51
52
    /**
53
     * Writes given message in specified color
54
     *
55
     * @param string $uColor   color
56
     * @param string $uMessage message
57
     *
58
     * @return void
59
     */
60
    public function writeColor($uColor, $uMessage)
61
    {
62
        if (strncasecmp(PHP_OS, "WIN", 3) === 0) {
63
            echo $uMessage, PHP_EOL;
64
            return;
65
        }
66
67
        if ($uColor === "black") {
68
            $tColor = "[0;30m";
69
        } elseif ($uColor === "darkgray") {
70
            $tColor = "[1;30m";
71
        } elseif ($uColor === "blue") {
72
            $tColor = "[0;34m";
73
        } elseif ($uColor === "lightblue") {
74
            $tColor = "[1;34m";
75
        } elseif ($uColor === "green") {
76
            $tColor = "[0;32m";
77
        } elseif ($uColor === "lightgreen") {
78
            $tColor = "[1;32m";
79
        } elseif ($uColor === "cyan") {
80
            $tColor = "[0;36m";
81
        } elseif ($uColor === "lightcyan") {
82
            $tColor = "[1;36m";
83
        } elseif ($uColor === "red") {
84
            $tColor = "[0;31m";
85
        } elseif ($uColor === "lightred") {
86
            $tColor = "[1;31m";
87
        } elseif ($uColor === "purple") {
88
            $tColor = "[0;35m";
89
        } elseif ($uColor === "lightpurple") {
90
            $tColor = "[1;35m";
91
        } elseif ($uColor === "brown") {
92
            $tColor = "[0;33m";
93
        } elseif ($uColor === "yellow") {
94
            $tColor = "[1;33m";
95
        } elseif ($uColor === "white") {
96
            $tColor = "[1;37m";
97
        } else /* if ($uColor === "lightgray") */ {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
            $tColor = "[0;37m";
99
        }
100
101
        echo "\033", $tColor, $uMessage, "\033[0m", PHP_EOL;
102
    }
103
104
    /**
105
     * Writes given message
106
     *
107
     * @param string $uMessage message
108
     *
109
     * @return void
110
     */
111
    public function write($uMessage)
112
    {
113
        echo $uMessage, PHP_EOL;
114
    }
115
116
    /**
117
     * Outputs the array to console
118
     *
119
     * @param array $uArray Target array will be printed
120
     *
121
     * @return void
122
     */
123
    public function writeArray(array $uArray)
124
    {
125
        print_r($uArray);
126
    }
127
}
128