Completed
Push — add/relase-scripts-package ( 9d8a04...097cd4 )
by Yaroslav
68:29 queued 59:27
created

Logger   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 11 2
A info() 0 3 1
A __construct() 0 27 1
A get_colored_string() 0 17 3
A get_foreground_colors() 0 3 1
A get_background_colors() 0 3 1
1
<?php
2
/**
3
 * Set of scripts for Jetpack.
4
 *
5
 * @package automattic/jetpack-scripts
6
 */
7
8
namespace Automattic\Jetpack\Scripts;
9
10
/**
11
 * Fancy logger
12
 */
13
class Logger {
14
	/**
15
	 * Array of foreground colors
16
	 *
17
	 * @var Array
18
	 */
19
	private $foreground_colors = array();
20
	/**
21
	 * Array of background colors
22
	 *
23
	 * @var Array
24
	 */
25
	private $background_colors = array();
26
27
	/**
28
	 * A generic log command
29
	 *
30
	 * @param String $string string to log.
31
	 * @param String $foreground_color foreground color.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $foreground_color not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
32
	 * @param String $background_color background color.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $background_color not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
33
	 */
34
	public static function log( $string, $foreground_color = null, $background_color = null ) {
35
		$logger = new Logger();
36
		$out    = $string;
37
38
		if ( ! is_string( $out ) ) {
39
			$out = print_r( $string, 1 ); //phpcs:ignore
40
		}
41
42
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
43
		error_log( $logger->get_colored_string( $out, $foreground_color, $background_color ) );
44
	}
45
46
	/**
47
	 * Info log command in yellow
48
	 *
49
	 * @param String $string string to log.
50
	 */
51
	public static function info( $string ) {
52
		self::log( $string, 'yellow' );
53
	}
54
55
	/**
56
	 * Set up shell colors
57
	 */
58
	public function __construct() {
59
		$this->foreground_colors['black']        = '0;30';
60
		$this->foreground_colors['dark_gray']    = '1;30';
61
		$this->foreground_colors['blue']         = '0;34';
62
		$this->foreground_colors['light_blue']   = '1;34';
63
		$this->foreground_colors['green']        = '0;32';
64
		$this->foreground_colors['light_green']  = '1;32';
65
		$this->foreground_colors['cyan']         = '0;36';
66
		$this->foreground_colors['light_cyan']   = '1;36';
67
		$this->foreground_colors['red']          = '0;31';
68
		$this->foreground_colors['light_red']    = '1;31';
69
		$this->foreground_colors['purple']       = '0;35';
70
		$this->foreground_colors['light_purple'] = '1;35';
71
		$this->foreground_colors['brown']        = '0;33';
72
		$this->foreground_colors['yellow']       = '1;33';
73
		$this->foreground_colors['light_gray']   = '0;37';
74
		$this->foreground_colors['white']        = '1;37';
75
76
		$this->background_colors['black']      = '40';
77
		$this->background_colors['red']        = '41';
78
		$this->background_colors['green']      = '42';
79
		$this->background_colors['yellow']     = '43';
80
		$this->background_colors['blue']       = '44';
81
		$this->background_colors['magenta']    = '45';
82
		$this->background_colors['cyan']       = '46';
83
		$this->background_colors['light_gray'] = '47';
84
	}
85
86
	/**
87
	 * Returns colored string
88
	 *
89
	 * @param String $string string to log.
90
	 * @param String $foreground_color foreground color.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $foreground_color not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
91
	 * @param String $background_color background color.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $background_color not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
92
	 */
93
	public function get_colored_string( $string, $foreground_color = null, $background_color = null ) {
94
		$colored_string = '';
95
96
		// Check if given foreground color found.
97
		if ( isset( $this->foreground_colors[ $foreground_color ] ) ) {
98
			$colored_string .= "\033[" . $this->foreground_colors[ $foreground_color ] . 'm';
99
		}
100
		// Check if given background color found.
101
		if ( isset( $this->background_colors[ $background_color ] ) ) {
102
			$colored_string .= "\033[" . $this->background_colors[ $background_color ] . 'm';
103
		}
104
105
		// Add string and end coloring.
106
		$colored_string .= $string . "\033[0m";
107
108
		return $colored_string;
109
	}
110
111
	/**
112
	 * Returns all foreground color names
113
	 */
114
	public function get_foreground_colors() {
115
		return array_keys( $this->foreground_colors );
116
	}
117
118
	/**
119
	 * Returns all background color names
120
	 */
121
	public function get_background_colors() {
122
		return array_keys( $this->background_colors );
123
	}
124
}
125