GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

OutputFormatter   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 221
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A setDecorated() 0 4 1
A isDecorated() 0 4 1
A setStyle() 0 4 1
A hasStyle() 0 4 1
A getStyle() 0 8 2
D format() 0 41 10
C createStyleFromString() 0 29 7
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\Console\Formatter;
13
14
/**
15
 * Formatter class for console output.
16
 *
17
 * @author Konstantin Kudryashov <[email protected]>
18
 *
19
 * @api
20
 */
21
class OutputFormatter implements OutputFormatterInterface
22
{
23
    private $decorated;
24
    private $styles = array();
25
    private $styleStack;
26
27
    /**
28
     * Escapes "<" special char in given text.
29
     *
30
     * @param string $text Text to escape
31
     *
32
     * @return string Escaped text
33
     */
34
    public static function escape($text)
35
    {
36
        return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
37
    }
38
39
    /**
40
     * Initializes console output formatter.
41
     *
42
     * @param bool                            $decorated Whether this formatter should actually decorate strings
43
     * @param OutputFormatterStyleInterface[] $styles    Array of "name => FormatterStyle" instances
44
     *
45
     * @api
46
     */
47
    public function __construct($decorated = false, array $styles = array())
48
    {
49
        $this->decorated = (bool) $decorated;
50
51
        $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
52
        $this->setStyle('info', new OutputFormatterStyle('green'));
53
        $this->setStyle('comment', new OutputFormatterStyle('yellow'));
54
        $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
55
56
        foreach ($styles as $name => $style) {
57
            $this->setStyle($name, $style);
58
        }
59
60
        $this->styleStack = new OutputFormatterStyleStack();
61
    }
62
63
    /**
64
     * Sets the decorated flag.
65
     *
66
     * @param bool $decorated Whether to decorate the messages or not
67
     *
68
     * @api
69
     */
70
    public function setDecorated($decorated)
71
    {
72
        $this->decorated = (bool) $decorated;
73
    }
74
75
    /**
76
     * Gets the decorated flag.
77
     *
78
     * @return bool true if the output will decorate messages, false otherwise
79
     *
80
     * @api
81
     */
82
    public function isDecorated()
83
    {
84
        return $this->decorated;
85
    }
86
87
    /**
88
     * Sets a new style.
89
     *
90
     * @param string                        $name  The style name
91
     * @param OutputFormatterStyleInterface $style The style instance
92
     *
93
     * @api
94
     */
95
    public function setStyle($name, OutputFormatterStyleInterface $style)
96
    {
97
        $this->styles[strtolower($name)] = $style;
98
    }
99
100
    /**
101
     * Checks if output formatter has style with specified name.
102
     *
103
     * @param string $name
104
     *
105
     * @return bool
106
     *
107
     * @api
108
     */
109
    public function hasStyle($name)
110
    {
111
        return isset($this->styles[strtolower($name)]);
112
    }
113
114
    /**
115
     * Gets style options from style with specified name.
116
     *
117
     * @param string $name
118
     *
119
     * @return OutputFormatterStyleInterface
120
     *
121
     * @throws \InvalidArgumentException When style isn't defined
122
     *
123
     * @api
124
     */
125
    public function getStyle($name)
126
    {
127
        if (!$this->hasStyle($name)) {
128
            throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
129
        }
130
131
        return $this->styles[strtolower($name)];
132
    }
133
134
    /**
135
     * Formats a message according to the given styles.
136
     *
137
     * @param string $message The message to style
138
     *
139
     * @return string The styled message
140
     *
141
     * @api
142
     */
143
    public function format($message)
144
    {
145
        $offset = 0;
146
        $output = '';
147
        $tagRegex = '[a-z][a-z0-9_=;-]*';
148
        preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE);
149
        foreach ($matches[0] as $i => $match) {
150
            $pos = $match[1];
151
            $text = $match[0];
152
153
            if (0 != $pos && '\\' == $message[$pos - 1]) {
154
                continue;
155
            }
156
157
            // add the text up to the next tag
158
            $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
159
            $offset = $pos + strlen($text);
160
161
            // opening tag?
162
            if ($open = '/' != $text[1]) {
163
                $tag = $matches[1][$i][0];
164
            } else {
165
                $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
166
            }
167
168
            if (!$open && !$tag) {
169
                // </>
170
                $this->styleStack->pop();
171
            } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
172
                $output .= $this->applyCurrentStyle($text);
173
            } elseif ($open) {
174
                $this->styleStack->push($style);
0 ignored issues
show
Bug introduced by
It seems like $style defined by $this->createStyleFromString(strtolower($tag)) on line 171 can also be of type boolean; however, Symfony\Component\Consol...atterStyleStack::push() does only seem to accept object<Symfony\Component...ormatterStyleInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
175
            } else {
176
                $this->styleStack->pop($style);
0 ignored issues
show
Bug introduced by
It seems like $style defined by $this->createStyleFromString(strtolower($tag)) on line 171 can also be of type boolean; however, Symfony\Component\Consol...matterStyleStack::pop() does only seem to accept null|object<Symfony\Comp...ormatterStyleInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
177
            }
178
        }
179
180
        $output .= $this->applyCurrentStyle(substr($message, $offset));
181
182
        return str_replace('\\<', '<', $output);
183
    }
184
185
    /**
186
     * @return OutputFormatterStyleStack
187
     */
188
    public function getStyleStack()
189
    {
190
        return $this->styleStack;
191
    }
192
193
    /**
194
     * Tries to create new style instance from string.
195
     *
196
     * @param string $string
197
     *
198
     * @return OutputFormatterStyle|bool false if string is not format string
199
     */
200
    private function createStyleFromString($string)
201
    {
202
        if (isset($this->styles[$string])) {
203
            return $this->styles[$string];
204
        }
205
206
        if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
207
            return false;
208
        }
209
210
        $style = new OutputFormatterStyle();
211
        foreach ($matches as $match) {
212
            array_shift($match);
213
214
            if ('fg' == $match[0]) {
215
                $style->setForeground($match[1]);
216
            } elseif ('bg' == $match[0]) {
217
                $style->setBackground($match[1]);
218
            } else {
219
                try {
220
                    $style->setOption($match[1]);
221
                } catch (\InvalidArgumentException $e) {
222
                    return false;
223
                }
224
            }
225
        }
226
227
        return $style;
228
    }
229
230
    /**
231
     * Applies current style from stack to text, if must be applied.
232
     *
233
     * @param string $text Input text
234
     *
235
     * @return string Styled text
236
     */
237
    private function applyCurrentStyle($text)
238
    {
239
        return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
240
    }
241
}
242