1 | <?php |
||
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()) |
||
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) |
||
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() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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 |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.