@@ -26,39 +26,39 @@ |
||
26 | 26 | private $cloner; |
27 | 27 | private $handler; |
28 | 28 | |
29 | - public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null) |
|
29 | + public function __construct( OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null ) |
|
30 | 30 | { |
31 | 31 | $this->output = $output; |
32 | 32 | $this->dumper = $dumper; |
33 | 33 | $this->cloner = $cloner; |
34 | 34 | |
35 | - if (class_exists(CliDumper::class)) { |
|
36 | - $this->handler = function ($var): string { |
|
37 | - $dumper = $this->dumper ?? $this->dumper = new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR); |
|
38 | - $dumper->setColors($this->output->isDecorated()); |
|
35 | + if ( class_exists( CliDumper::class ) ) { |
|
36 | + $this->handler = function( $var ): string { |
|
37 | + $dumper = $this->dumper ?? $this->dumper = new CliDumper( null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR ); |
|
38 | + $dumper->setColors( $this->output->isDecorated() ); |
|
39 | 39 | |
40 | - return rtrim($dumper->dump(($this->cloner ?? $this->cloner = new VarCloner())->cloneVar($var)->withRefHandles(false), true)); |
|
40 | + return rtrim( $dumper->dump( ( $this->cloner ?? $this->cloner = new VarCloner() )->cloneVar( $var )->withRefHandles( false ), true ) ); |
|
41 | 41 | }; |
42 | 42 | } else { |
43 | - $this->handler = function ($var): string { |
|
44 | - switch (true) { |
|
43 | + $this->handler = function( $var ): string { |
|
44 | + switch ( true ) { |
|
45 | 45 | case null === $var: |
46 | 46 | return 'null'; |
47 | 47 | case true === $var: |
48 | 48 | return 'true'; |
49 | 49 | case false === $var: |
50 | 50 | return 'false'; |
51 | - case \is_string($var): |
|
52 | - return '"'.$var.'"'; |
|
51 | + case \is_string( $var ): |
|
52 | + return '"' . $var . '"'; |
|
53 | 53 | default: |
54 | - return rtrim(print_r($var, true)); |
|
54 | + return rtrim( print_r( $var, true ) ); |
|
55 | 55 | } |
56 | 56 | }; |
57 | 57 | } |
58 | 58 | } |
59 | 59 | |
60 | - public function __invoke($var): string |
|
60 | + public function __invoke( $var ): string |
|
61 | 61 | { |
62 | - return ($this->handler)($var); |
|
62 | + return ( $this->handler )( $var ); |
|
63 | 63 | } |
64 | 64 | } |
@@ -45,34 +45,34 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @throws RuntimeException If there is no data to read in the input stream |
47 | 47 | */ |
48 | - public function ask(InputInterface $input, OutputInterface $output, Question $question) |
|
48 | + public function ask( InputInterface $input, OutputInterface $output, Question $question ) |
|
49 | 49 | { |
50 | - if ($output instanceof ConsoleOutputInterface) { |
|
50 | + if ( $output instanceof ConsoleOutputInterface ) { |
|
51 | 51 | $output = $output->getErrorOutput(); |
52 | 52 | } |
53 | 53 | |
54 | - if (!$input->isInteractive()) { |
|
55 | - return $this->getDefaultAnswer($question); |
|
54 | + if ( ! $input->isInteractive() ) { |
|
55 | + return $this->getDefaultAnswer( $question ); |
|
56 | 56 | } |
57 | 57 | |
58 | - if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) { |
|
58 | + if ( $input instanceof StreamableInputInterface && $stream = $input->getStream() ) { |
|
59 | 59 | $this->inputStream = $stream; |
60 | 60 | } |
61 | 61 | |
62 | 62 | try { |
63 | - if (!$question->getValidator()) { |
|
64 | - return $this->doAsk($output, $question); |
|
63 | + if ( ! $question->getValidator() ) { |
|
64 | + return $this->doAsk( $output, $question ); |
|
65 | 65 | } |
66 | 66 | |
67 | - $interviewer = function () use ($output, $question) { |
|
68 | - return $this->doAsk($output, $question); |
|
67 | + $interviewer = function() use ( $output, $question ) { |
|
68 | + return $this->doAsk( $output, $question ); |
|
69 | 69 | }; |
70 | 70 | |
71 | - return $this->validateAttempts($interviewer, $output, $question); |
|
72 | - } catch (MissingInputException $exception) { |
|
73 | - $input->setInteractive(false); |
|
71 | + return $this->validateAttempts( $interviewer, $output, $question ); |
|
72 | + } catch ( MissingInputException $exception ) { |
|
73 | + $input->setInteractive( false ); |
|
74 | 74 | |
75 | - if (null === $fallbackOutput = $this->getDefaultAnswer($question)) { |
|
75 | + if ( null === $fallbackOutput = $this->getDefaultAnswer( $question ) ) { |
|
76 | 76 | throw $exception; |
77 | 77 | } |
78 | 78 | |
@@ -103,48 +103,48 @@ discard block |
||
103 | 103 | * |
104 | 104 | * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden |
105 | 105 | */ |
106 | - private function doAsk(OutputInterface $output, Question $question) |
|
106 | + private function doAsk( OutputInterface $output, Question $question ) |
|
107 | 107 | { |
108 | - $this->writePrompt($output, $question); |
|
108 | + $this->writePrompt( $output, $question ); |
|
109 | 109 | |
110 | 110 | $inputStream = $this->inputStream ?: \STDIN; |
111 | 111 | $autocomplete = $question->getAutocompleterCallback(); |
112 | 112 | |
113 | - if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) { |
|
113 | + if ( null === $autocomplete || ! self::$stty || ! Terminal::hasSttyAvailable() ) { |
|
114 | 114 | $ret = false; |
115 | - if ($question->isHidden()) { |
|
115 | + if ( $question->isHidden() ) { |
|
116 | 116 | try { |
117 | - $hiddenResponse = $this->getHiddenResponse($output, $inputStream, $question->isTrimmable()); |
|
118 | - $ret = $question->isTrimmable() ? trim($hiddenResponse) : $hiddenResponse; |
|
119 | - } catch (RuntimeException $e) { |
|
120 | - if (!$question->isHiddenFallback()) { |
|
117 | + $hiddenResponse = $this->getHiddenResponse( $output, $inputStream, $question->isTrimmable() ); |
|
118 | + $ret = $question->isTrimmable() ? trim( $hiddenResponse ) : $hiddenResponse; |
|
119 | + } catch ( RuntimeException $e ) { |
|
120 | + if ( ! $question->isHiddenFallback() ) { |
|
121 | 121 | throw $e; |
122 | 122 | } |
123 | 123 | } |
124 | 124 | } |
125 | 125 | |
126 | - if (false === $ret) { |
|
127 | - $ret = $this->readInput($inputStream, $question); |
|
128 | - if (false === $ret) { |
|
129 | - throw new MissingInputException('Aborted.'); |
|
126 | + if ( false === $ret ) { |
|
127 | + $ret = $this->readInput( $inputStream, $question ); |
|
128 | + if ( false === $ret ) { |
|
129 | + throw new MissingInputException( 'Aborted.' ); |
|
130 | 130 | } |
131 | - if ($question->isTrimmable()) { |
|
132 | - $ret = trim($ret); |
|
131 | + if ( $question->isTrimmable() ) { |
|
132 | + $ret = trim( $ret ); |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | } else { |
136 | - $autocomplete = $this->autocomplete($output, $question, $inputStream, $autocomplete); |
|
137 | - $ret = $question->isTrimmable() ? trim($autocomplete) : $autocomplete; |
|
136 | + $autocomplete = $this->autocomplete( $output, $question, $inputStream, $autocomplete ); |
|
137 | + $ret = $question->isTrimmable() ? trim( $autocomplete ) : $autocomplete; |
|
138 | 138 | } |
139 | 139 | |
140 | - if ($output instanceof ConsoleSectionOutput) { |
|
141 | - $output->addContent($ret); |
|
140 | + if ( $output instanceof ConsoleSectionOutput ) { |
|
141 | + $output->addContent( $ret ); |
|
142 | 142 | } |
143 | 143 | |
144 | - $ret = \strlen($ret) > 0 ? $ret : $question->getDefault(); |
|
144 | + $ret = \strlen( $ret ) > 0 ? $ret : $question->getDefault(); |
|
145 | 145 | |
146 | - if ($normalizer = $question->getNormalizer()) { |
|
147 | - return $normalizer($ret); |
|
146 | + if ( $normalizer = $question->getNormalizer() ) { |
|
147 | + return $normalizer( $ret ); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | return $ret; |
@@ -153,27 +153,27 @@ discard block |
||
153 | 153 | /** |
154 | 154 | * @return mixed |
155 | 155 | */ |
156 | - private function getDefaultAnswer(Question $question) |
|
156 | + private function getDefaultAnswer( Question $question ) |
|
157 | 157 | { |
158 | 158 | $default = $question->getDefault(); |
159 | 159 | |
160 | - if (null === $default) { |
|
160 | + if ( null === $default ) { |
|
161 | 161 | return $default; |
162 | 162 | } |
163 | 163 | |
164 | - if ($validator = $question->getValidator()) { |
|
165 | - return \call_user_func($question->getValidator(), $default); |
|
166 | - } elseif ($question instanceof ChoiceQuestion) { |
|
164 | + if ( $validator = $question->getValidator() ) { |
|
165 | + return \call_user_func( $question->getValidator(), $default ); |
|
166 | + } elseif ( $question instanceof ChoiceQuestion ) { |
|
167 | 167 | $choices = $question->getChoices(); |
168 | 168 | |
169 | - if (!$question->isMultiselect()) { |
|
170 | - return $choices[$default] ?? $default; |
|
169 | + if ( ! $question->isMultiselect() ) { |
|
170 | + return $choices[ $default ] ?? $default; |
|
171 | 171 | } |
172 | 172 | |
173 | - $default = explode(',', $default); |
|
174 | - foreach ($default as $k => $v) { |
|
175 | - $v = $question->isTrimmable() ? trim($v) : $v; |
|
176 | - $default[$k] = $choices[$v] ?? $v; |
|
173 | + $default = explode( ',', $default ); |
|
174 | + foreach ( $default as $k => $v ) { |
|
175 | + $v = $question->isTrimmable() ? trim( $v ) : $v; |
|
176 | + $default[ $k ] = $choices[ $v ] ?? $v; |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | |
@@ -183,34 +183,34 @@ discard block |
||
183 | 183 | /** |
184 | 184 | * Outputs the question prompt. |
185 | 185 | */ |
186 | - protected function writePrompt(OutputInterface $output, Question $question) |
|
186 | + protected function writePrompt( OutputInterface $output, Question $question ) |
|
187 | 187 | { |
188 | 188 | $message = $question->getQuestion(); |
189 | 189 | |
190 | - if ($question instanceof ChoiceQuestion) { |
|
191 | - $output->writeln(array_merge([ |
|
190 | + if ( $question instanceof ChoiceQuestion ) { |
|
191 | + $output->writeln( array_merge( [ |
|
192 | 192 | $question->getQuestion(), |
193 | - ], $this->formatChoiceQuestionChoices($question, 'info'))); |
|
193 | + ], $this->formatChoiceQuestionChoices( $question, 'info' ) ) ); |
|
194 | 194 | |
195 | 195 | $message = $question->getPrompt(); |
196 | 196 | } |
197 | 197 | |
198 | - $output->write($message); |
|
198 | + $output->write( $message ); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
202 | 202 | * @return string[] |
203 | 203 | */ |
204 | - protected function formatChoiceQuestionChoices(ChoiceQuestion $question, string $tag) |
|
204 | + protected function formatChoiceQuestionChoices( ChoiceQuestion $question, string $tag ) |
|
205 | 205 | { |
206 | - $messages = []; |
|
206 | + $messages = [ ]; |
|
207 | 207 | |
208 | - $maxWidth = max(array_map('self::width', array_keys($choices = $question->getChoices()))); |
|
208 | + $maxWidth = max( array_map( 'self::width', array_keys( $choices = $question->getChoices() ) ) ); |
|
209 | 209 | |
210 | - foreach ($choices as $key => $value) { |
|
211 | - $padding = str_repeat(' ', $maxWidth - self::width($key)); |
|
210 | + foreach ( $choices as $key => $value ) { |
|
211 | + $padding = str_repeat( ' ', $maxWidth - self::width( $key ) ); |
|
212 | 212 | |
213 | - $messages[] = sprintf(" [<$tag>%s$padding</$tag>] %s", $key, $value); |
|
213 | + $messages[ ] = sprintf( " [<$tag>%s$padding</$tag>] %s", $key, $value ); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | return $messages; |
@@ -219,15 +219,15 @@ discard block |
||
219 | 219 | /** |
220 | 220 | * Outputs an error message. |
221 | 221 | */ |
222 | - protected function writeError(OutputInterface $output, \Exception $error) |
|
222 | + protected function writeError( OutputInterface $output, \Exception $error ) |
|
223 | 223 | { |
224 | - if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) { |
|
225 | - $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'); |
|
224 | + if ( null !== $this->getHelperSet() && $this->getHelperSet()->has( 'formatter' ) ) { |
|
225 | + $message = $this->getHelperSet()->get( 'formatter' )->formatBlock( $error->getMessage(), 'error' ); |
|
226 | 226 | } else { |
227 | - $message = '<error>'.$error->getMessage().'</error>'; |
|
227 | + $message = '<error>' . $error->getMessage() . '</error>'; |
|
228 | 228 | } |
229 | 229 | |
230 | - $output->writeln($message); |
|
230 | + $output->writeln( $message ); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
@@ -235,91 +235,91 @@ discard block |
||
235 | 235 | * |
236 | 236 | * @param resource $inputStream |
237 | 237 | */ |
238 | - private function autocomplete(OutputInterface $output, Question $question, $inputStream, callable $autocomplete): string |
|
238 | + private function autocomplete( OutputInterface $output, Question $question, $inputStream, callable $autocomplete ): string |
|
239 | 239 | { |
240 | - $cursor = new Cursor($output, $inputStream); |
|
240 | + $cursor = new Cursor( $output, $inputStream ); |
|
241 | 241 | |
242 | 242 | $fullChoice = ''; |
243 | 243 | $ret = ''; |
244 | 244 | |
245 | 245 | $i = 0; |
246 | 246 | $ofs = -1; |
247 | - $matches = $autocomplete($ret); |
|
248 | - $numMatches = \count($matches); |
|
247 | + $matches = $autocomplete( $ret ); |
|
248 | + $numMatches = \count( $matches ); |
|
249 | 249 | |
250 | - $sttyMode = shell_exec('stty -g'); |
|
250 | + $sttyMode = shell_exec( 'stty -g' ); |
|
251 | 251 | |
252 | 252 | // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) |
253 | - shell_exec('stty -icanon -echo'); |
|
253 | + shell_exec( 'stty -icanon -echo' ); |
|
254 | 254 | |
255 | 255 | // Add highlighted text style |
256 | - $output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white')); |
|
256 | + $output->getFormatter()->setStyle( 'hl', new OutputFormatterStyle( 'black', 'white' ) ); |
|
257 | 257 | |
258 | 258 | // Read a keypress |
259 | - while (!feof($inputStream)) { |
|
260 | - $c = fread($inputStream, 1); |
|
259 | + while ( ! feof( $inputStream ) ) { |
|
260 | + $c = fread( $inputStream, 1 ); |
|
261 | 261 | |
262 | 262 | // as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false. |
263 | - if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) { |
|
264 | - shell_exec(sprintf('stty %s', $sttyMode)); |
|
265 | - throw new MissingInputException('Aborted.'); |
|
266 | - } elseif ("\177" === $c) { // Backspace Character |
|
267 | - if (0 === $numMatches && 0 !== $i) { |
|
263 | + if ( false === $c || ( '' === $ret && '' === $c && null === $question->getDefault() ) ) { |
|
264 | + shell_exec( sprintf( 'stty %s', $sttyMode ) ); |
|
265 | + throw new MissingInputException( 'Aborted.' ); |
|
266 | + } elseif ( "\177" === $c ) { // Backspace Character |
|
267 | + if ( 0 === $numMatches && 0 !== $i ) { |
|
268 | 268 | --$i; |
269 | - $cursor->moveLeft(s($fullChoice)->slice(-1)->width(false)); |
|
269 | + $cursor->moveLeft( s( $fullChoice )->slice(-1)->width( false ) ); |
|
270 | 270 | |
271 | - $fullChoice = self::substr($fullChoice, 0, $i); |
|
271 | + $fullChoice = self::substr( $fullChoice, 0, $i ); |
|
272 | 272 | } |
273 | 273 | |
274 | - if (0 === $i) { |
|
274 | + if ( 0 === $i ) { |
|
275 | 275 | $ofs = -1; |
276 | - $matches = $autocomplete($ret); |
|
277 | - $numMatches = \count($matches); |
|
276 | + $matches = $autocomplete( $ret ); |
|
277 | + $numMatches = \count( $matches ); |
|
278 | 278 | } else { |
279 | 279 | $numMatches = 0; |
280 | 280 | } |
281 | 281 | |
282 | 282 | // Pop the last character off the end of our string |
283 | - $ret = self::substr($ret, 0, $i); |
|
284 | - } elseif ("\033" === $c) { |
|
283 | + $ret = self::substr( $ret, 0, $i ); |
|
284 | + } elseif ( "\033" === $c ) { |
|
285 | 285 | // Did we read an escape sequence? |
286 | - $c .= fread($inputStream, 2); |
|
286 | + $c .= fread( $inputStream, 2 ); |
|
287 | 287 | |
288 | 288 | // A = Up Arrow. B = Down Arrow |
289 | - if (isset($c[2]) && ('A' === $c[2] || 'B' === $c[2])) { |
|
290 | - if ('A' === $c[2] && -1 === $ofs) { |
|
289 | + if ( isset( $c[ 2 ] ) && ( 'A' === $c[ 2 ] || 'B' === $c[ 2 ] ) ) { |
|
290 | + if ( 'A' === $c[ 2 ] && -1 === $ofs ) { |
|
291 | 291 | $ofs = 0; |
292 | 292 | } |
293 | 293 | |
294 | - if (0 === $numMatches) { |
|
294 | + if ( 0 === $numMatches ) { |
|
295 | 295 | continue; |
296 | 296 | } |
297 | 297 | |
298 | - $ofs += ('A' === $c[2]) ? -1 : 1; |
|
299 | - $ofs = ($numMatches + $ofs) % $numMatches; |
|
298 | + $ofs += ( 'A' === $c[ 2 ] ) ? -1 : 1; |
|
299 | + $ofs = ( $numMatches + $ofs ) % $numMatches; |
|
300 | 300 | } |
301 | - } elseif (\ord($c) < 32) { |
|
302 | - if ("\t" === $c || "\n" === $c) { |
|
303 | - if ($numMatches > 0 && -1 !== $ofs) { |
|
304 | - $ret = (string) $matches[$ofs]; |
|
301 | + } elseif ( \ord( $c ) < 32 ) { |
|
302 | + if ( "\t" === $c || "\n" === $c ) { |
|
303 | + if ( $numMatches > 0 && -1 !== $ofs ) { |
|
304 | + $ret = (string)$matches[ $ofs ]; |
|
305 | 305 | // Echo out remaining chars for current match |
306 | - $remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)))); |
|
307 | - $output->write($remainingCharacters); |
|
306 | + $remainingCharacters = substr( $ret, \strlen( trim( $this->mostRecentlyEnteredValue( $fullChoice ) ) ) ); |
|
307 | + $output->write( $remainingCharacters ); |
|
308 | 308 | $fullChoice .= $remainingCharacters; |
309 | - $i = (false === $encoding = mb_detect_encoding($fullChoice, null, true)) ? \strlen($fullChoice) : mb_strlen($fullChoice, $encoding); |
|
309 | + $i = ( false === $encoding = mb_detect_encoding( $fullChoice, null, true ) ) ? \strlen( $fullChoice ) : mb_strlen( $fullChoice, $encoding ); |
|
310 | 310 | |
311 | 311 | $matches = array_filter( |
312 | - $autocomplete($ret), |
|
313 | - function ($match) use ($ret) { |
|
314 | - return '' === $ret || str_starts_with($match, $ret); |
|
312 | + $autocomplete( $ret ), |
|
313 | + function( $match ) use ( $ret ) { |
|
314 | + return '' === $ret || str_starts_with( $match, $ret ); |
|
315 | 315 | } |
316 | 316 | ); |
317 | - $numMatches = \count($matches); |
|
317 | + $numMatches = \count( $matches ); |
|
318 | 318 | $ofs = -1; |
319 | 319 | } |
320 | 320 | |
321 | - if ("\n" === $c) { |
|
322 | - $output->write($c); |
|
321 | + if ( "\n" === $c ) { |
|
322 | + $output->write( $c ); |
|
323 | 323 | break; |
324 | 324 | } |
325 | 325 | |
@@ -328,58 +328,58 @@ discard block |
||
328 | 328 | |
329 | 329 | continue; |
330 | 330 | } else { |
331 | - if ("\x80" <= $c) { |
|
332 | - $c .= fread($inputStream, ["\xC0" => 1, "\xD0" => 1, "\xE0" => 2, "\xF0" => 3][$c & "\xF0"]); |
|
331 | + if ( "\x80" <= $c ) { |
|
332 | + $c .= fread( $inputStream, [ "\xC0" => 1, "\xD0" => 1, "\xE0" => 2, "\xF0" => 3 ][ $c & "\xF0" ] ); |
|
333 | 333 | } |
334 | 334 | |
335 | - $output->write($c); |
|
335 | + $output->write( $c ); |
|
336 | 336 | $ret .= $c; |
337 | 337 | $fullChoice .= $c; |
338 | 338 | ++$i; |
339 | 339 | |
340 | 340 | $tempRet = $ret; |
341 | 341 | |
342 | - if ($question instanceof ChoiceQuestion && $question->isMultiselect()) { |
|
343 | - $tempRet = $this->mostRecentlyEnteredValue($fullChoice); |
|
342 | + if ( $question instanceof ChoiceQuestion && $question->isMultiselect() ) { |
|
343 | + $tempRet = $this->mostRecentlyEnteredValue( $fullChoice ); |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | $numMatches = 0; |
347 | 347 | $ofs = 0; |
348 | 348 | |
349 | - foreach ($autocomplete($ret) as $value) { |
|
349 | + foreach ( $autocomplete( $ret ) as $value ) { |
|
350 | 350 | // If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle) |
351 | - if (str_starts_with($value, $tempRet)) { |
|
352 | - $matches[$numMatches++] = $value; |
|
351 | + if ( str_starts_with( $value, $tempRet ) ) { |
|
352 | + $matches[ $numMatches++ ] = $value; |
|
353 | 353 | } |
354 | 354 | } |
355 | 355 | } |
356 | 356 | |
357 | 357 | $cursor->clearLineAfter(); |
358 | 358 | |
359 | - if ($numMatches > 0 && -1 !== $ofs) { |
|
359 | + if ( $numMatches > 0 && -1 !== $ofs ) { |
|
360 | 360 | $cursor->savePosition(); |
361 | 361 | // Write highlighted text, complete the partially entered response |
362 | - $charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))); |
|
363 | - $output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).'</hl>'); |
|
362 | + $charactersEntered = \strlen( trim( $this->mostRecentlyEnteredValue( $fullChoice ) ) ); |
|
363 | + $output->write( '<hl>' . OutputFormatter::escapeTrailingBackslash( substr( $matches[ $ofs ], $charactersEntered ) ) . '</hl>' ); |
|
364 | 364 | $cursor->restorePosition(); |
365 | 365 | } |
366 | 366 | } |
367 | 367 | |
368 | 368 | // Reset stty so it behaves normally again |
369 | - shell_exec(sprintf('stty %s', $sttyMode)); |
|
369 | + shell_exec( sprintf( 'stty %s', $sttyMode ) ); |
|
370 | 370 | |
371 | 371 | return $fullChoice; |
372 | 372 | } |
373 | 373 | |
374 | - private function mostRecentlyEnteredValue(string $entered): string |
|
374 | + private function mostRecentlyEnteredValue( string $entered ): string |
|
375 | 375 | { |
376 | 376 | // Determine the most recent value that the user entered |
377 | - if (!str_contains($entered, ',')) { |
|
377 | + if ( ! str_contains( $entered, ',' ) ) { |
|
378 | 378 | return $entered; |
379 | 379 | } |
380 | 380 | |
381 | - $choices = explode(',', $entered); |
|
382 | - if ('' !== $lastChoice = trim($choices[\count($choices) - 1])) { |
|
381 | + $choices = explode( ',', $entered ); |
|
382 | + if ( '' !== $lastChoice = trim( $choices[ \count( $choices ) - 1 ] ) ) { |
|
383 | 383 | return $lastChoice; |
384 | 384 | } |
385 | 385 | |
@@ -394,49 +394,49 @@ discard block |
||
394 | 394 | * |
395 | 395 | * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden |
396 | 396 | */ |
397 | - private function getHiddenResponse(OutputInterface $output, $inputStream, bool $trimmable = true): string |
|
397 | + private function getHiddenResponse( OutputInterface $output, $inputStream, bool $trimmable = true ): string |
|
398 | 398 | { |
399 | - if ('\\' === \DIRECTORY_SEPARATOR) { |
|
400 | - $exe = __DIR__.'/../Resources/bin/hiddeninput.exe'; |
|
399 | + if ( '\\' === \DIRECTORY_SEPARATOR ) { |
|
400 | + $exe = __DIR__ . '/../Resources/bin/hiddeninput.exe'; |
|
401 | 401 | |
402 | 402 | // handle code running from a phar |
403 | - if ('phar:' === substr(__FILE__, 0, 5)) { |
|
404 | - $tmpExe = sys_get_temp_dir().'/hiddeninput.exe'; |
|
405 | - copy($exe, $tmpExe); |
|
403 | + if ( 'phar:' === substr( __FILE__, 0, 5 ) ) { |
|
404 | + $tmpExe = sys_get_temp_dir() . '/hiddeninput.exe'; |
|
405 | + copy( $exe, $tmpExe ); |
|
406 | 406 | $exe = $tmpExe; |
407 | 407 | } |
408 | 408 | |
409 | - $sExec = shell_exec('"'.$exe.'"'); |
|
410 | - $value = $trimmable ? rtrim($sExec) : $sExec; |
|
411 | - $output->writeln(''); |
|
409 | + $sExec = shell_exec( '"' . $exe . '"' ); |
|
410 | + $value = $trimmable ? rtrim( $sExec ) : $sExec; |
|
411 | + $output->writeln( '' ); |
|
412 | 412 | |
413 | - if (isset($tmpExe)) { |
|
414 | - unlink($tmpExe); |
|
413 | + if ( isset( $tmpExe ) ) { |
|
414 | + unlink( $tmpExe ); |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | return $value; |
418 | 418 | } |
419 | 419 | |
420 | - if (self::$stty && Terminal::hasSttyAvailable()) { |
|
421 | - $sttyMode = shell_exec('stty -g'); |
|
422 | - shell_exec('stty -echo'); |
|
423 | - } elseif ($this->isInteractiveInput($inputStream)) { |
|
424 | - throw new RuntimeException('Unable to hide the response.'); |
|
420 | + if ( self::$stty && Terminal::hasSttyAvailable() ) { |
|
421 | + $sttyMode = shell_exec( 'stty -g' ); |
|
422 | + shell_exec( 'stty -echo' ); |
|
423 | + } elseif ( $this->isInteractiveInput( $inputStream ) ) { |
|
424 | + throw new RuntimeException( 'Unable to hide the response.' ); |
|
425 | 425 | } |
426 | 426 | |
427 | - $value = fgets($inputStream, 4096); |
|
427 | + $value = fgets( $inputStream, 4096 ); |
|
428 | 428 | |
429 | - if (self::$stty && Terminal::hasSttyAvailable()) { |
|
430 | - shell_exec(sprintf('stty %s', $sttyMode)); |
|
429 | + if ( self::$stty && Terminal::hasSttyAvailable() ) { |
|
430 | + shell_exec( sprintf( 'stty %s', $sttyMode ) ); |
|
431 | 431 | } |
432 | 432 | |
433 | - if (false === $value) { |
|
434 | - throw new MissingInputException('Aborted.'); |
|
433 | + if ( false === $value ) { |
|
434 | + throw new MissingInputException( 'Aborted.' ); |
|
435 | 435 | } |
436 | - if ($trimmable) { |
|
437 | - $value = trim($value); |
|
436 | + if ( $trimmable ) { |
|
437 | + $value = trim( $value ); |
|
438 | 438 | } |
439 | - $output->writeln(''); |
|
439 | + $output->writeln( '' ); |
|
440 | 440 | |
441 | 441 | return $value; |
442 | 442 | } |
@@ -450,50 +450,50 @@ discard block |
||
450 | 450 | * |
451 | 451 | * @throws \Exception In case the max number of attempts has been reached and no valid response has been given |
452 | 452 | */ |
453 | - private function validateAttempts(callable $interviewer, OutputInterface $output, Question $question) |
|
453 | + private function validateAttempts( callable $interviewer, OutputInterface $output, Question $question ) |
|
454 | 454 | { |
455 | 455 | $error = null; |
456 | 456 | $attempts = $question->getMaxAttempts(); |
457 | 457 | |
458 | - while (null === $attempts || $attempts--) { |
|
459 | - if (null !== $error) { |
|
460 | - $this->writeError($output, $error); |
|
458 | + while ( null === $attempts || $attempts-- ) { |
|
459 | + if ( null !== $error ) { |
|
460 | + $this->writeError( $output, $error ); |
|
461 | 461 | } |
462 | 462 | |
463 | 463 | try { |
464 | - return $question->getValidator()($interviewer()); |
|
465 | - } catch (RuntimeException $e) { |
|
464 | + return $question->getValidator()( $interviewer() ); |
|
465 | + } catch ( RuntimeException $e ) { |
|
466 | 466 | throw $e; |
467 | - } catch (\Exception $error) { |
|
467 | + } catch ( \Exception $error ) { |
|
468 | 468 | } |
469 | 469 | } |
470 | 470 | |
471 | 471 | throw $error; |
472 | 472 | } |
473 | 473 | |
474 | - private function isInteractiveInput($inputStream): bool |
|
474 | + private function isInteractiveInput( $inputStream ): bool |
|
475 | 475 | { |
476 | - if ('php://stdin' !== (stream_get_meta_data($inputStream)['uri'] ?? null)) { |
|
476 | + if ( 'php://stdin' !== ( stream_get_meta_data( $inputStream )[ 'uri' ] ?? null ) ) { |
|
477 | 477 | return false; |
478 | 478 | } |
479 | 479 | |
480 | - if (null !== self::$stdinIsInteractive) { |
|
480 | + if ( null !== self::$stdinIsInteractive ) { |
|
481 | 481 | return self::$stdinIsInteractive; |
482 | 482 | } |
483 | 483 | |
484 | - if (\function_exists('stream_isatty')) { |
|
485 | - return self::$stdinIsInteractive = stream_isatty(fopen('php://stdin', 'r')); |
|
484 | + if ( \function_exists( 'stream_isatty' ) ) { |
|
485 | + return self::$stdinIsInteractive = stream_isatty( fopen( 'php://stdin', 'r' ) ); |
|
486 | 486 | } |
487 | 487 | |
488 | - if (\function_exists('posix_isatty')) { |
|
489 | - return self::$stdinIsInteractive = posix_isatty(fopen('php://stdin', 'r')); |
|
488 | + if ( \function_exists( 'posix_isatty' ) ) { |
|
489 | + return self::$stdinIsInteractive = posix_isatty( fopen( 'php://stdin', 'r' ) ); |
|
490 | 490 | } |
491 | 491 | |
492 | - if (!\function_exists('exec')) { |
|
492 | + if ( ! \function_exists( 'exec' ) ) { |
|
493 | 493 | return self::$stdinIsInteractive = true; |
494 | 494 | } |
495 | 495 | |
496 | - exec('stty 2> /dev/null', $output, $status); |
|
496 | + exec( 'stty 2> /dev/null', $output, $status ); |
|
497 | 497 | |
498 | 498 | return self::$stdinIsInteractive = 1 !== $status; |
499 | 499 | } |
@@ -506,30 +506,30 @@ discard block |
||
506 | 506 | * |
507 | 507 | * @return string|false The input received, false in case input could not be read |
508 | 508 | */ |
509 | - private function readInput($inputStream, Question $question) |
|
509 | + private function readInput( $inputStream, Question $question ) |
|
510 | 510 | { |
511 | - if (!$question->isMultiline()) { |
|
511 | + if ( ! $question->isMultiline() ) { |
|
512 | 512 | $cp = $this->setIOCodepage(); |
513 | - $ret = fgets($inputStream, 4096); |
|
513 | + $ret = fgets( $inputStream, 4096 ); |
|
514 | 514 | |
515 | - return $this->resetIOCodepage($cp, $ret); |
|
515 | + return $this->resetIOCodepage( $cp, $ret ); |
|
516 | 516 | } |
517 | 517 | |
518 | - $multiLineStreamReader = $this->cloneInputStream($inputStream); |
|
519 | - if (null === $multiLineStreamReader) { |
|
518 | + $multiLineStreamReader = $this->cloneInputStream( $inputStream ); |
|
519 | + if ( null === $multiLineStreamReader ) { |
|
520 | 520 | return false; |
521 | 521 | } |
522 | 522 | |
523 | 523 | $ret = ''; |
524 | 524 | $cp = $this->setIOCodepage(); |
525 | - while (false !== ($char = fgetc($multiLineStreamReader))) { |
|
526 | - if (\PHP_EOL === "{$ret}{$char}") { |
|
525 | + while ( false !== ( $char = fgetc( $multiLineStreamReader ) ) ) { |
|
526 | + if ( \PHP_EOL === "{$ret}{$char}" ) { |
|
527 | 527 | break; |
528 | 528 | } |
529 | 529 | $ret .= $char; |
530 | 530 | } |
531 | 531 | |
532 | - return $this->resetIOCodepage($cp, $ret); |
|
532 | + return $this->resetIOCodepage( $cp, $ret ); |
|
533 | 533 | } |
534 | 534 | |
535 | 535 | /** |
@@ -539,9 +539,9 @@ discard block |
||
539 | 539 | */ |
540 | 540 | private function setIOCodepage(): int |
541 | 541 | { |
542 | - if (\function_exists('sapi_windows_cp_set')) { |
|
542 | + if ( \function_exists( 'sapi_windows_cp_set' ) ) { |
|
543 | 543 | $cp = sapi_windows_cp_get(); |
544 | - sapi_windows_cp_set(sapi_windows_cp_get('oem')); |
|
544 | + sapi_windows_cp_set( sapi_windows_cp_get( 'oem' ) ); |
|
545 | 545 | |
546 | 546 | return $cp; |
547 | 547 | } |
@@ -556,13 +556,13 @@ discard block |
||
556 | 556 | * |
557 | 557 | * @return string|false |
558 | 558 | */ |
559 | - private function resetIOCodepage(int $cp, $input) |
|
559 | + private function resetIOCodepage( int $cp, $input ) |
|
560 | 560 | { |
561 | - if (0 !== $cp) { |
|
562 | - sapi_windows_cp_set($cp); |
|
561 | + if ( 0 !== $cp ) { |
|
562 | + sapi_windows_cp_set( $cp ); |
|
563 | 563 | |
564 | - if (false !== $input && '' !== $input) { |
|
565 | - $input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input); |
|
564 | + if ( false !== $input && '' !== $input ) { |
|
565 | + $input = sapi_windows_cp_conv( sapi_windows_cp_get( 'oem' ), $cp, $input ); |
|
566 | 566 | } |
567 | 567 | } |
568 | 568 | |
@@ -577,27 +577,27 @@ discard block |
||
577 | 577 | * |
578 | 578 | * @return resource|null The cloned resource, null in case it could not be cloned |
579 | 579 | */ |
580 | - private function cloneInputStream($inputStream) |
|
580 | + private function cloneInputStream( $inputStream ) |
|
581 | 581 | { |
582 | - $streamMetaData = stream_get_meta_data($inputStream); |
|
583 | - $seekable = $streamMetaData['seekable'] ?? false; |
|
584 | - $mode = $streamMetaData['mode'] ?? 'rb'; |
|
585 | - $uri = $streamMetaData['uri'] ?? null; |
|
582 | + $streamMetaData = stream_get_meta_data( $inputStream ); |
|
583 | + $seekable = $streamMetaData[ 'seekable' ] ?? false; |
|
584 | + $mode = $streamMetaData[ 'mode' ] ?? 'rb'; |
|
585 | + $uri = $streamMetaData[ 'uri' ] ?? null; |
|
586 | 586 | |
587 | - if (null === $uri) { |
|
587 | + if ( null === $uri ) { |
|
588 | 588 | return null; |
589 | 589 | } |
590 | 590 | |
591 | - $cloneStream = fopen($uri, $mode); |
|
591 | + $cloneStream = fopen( $uri, $mode ); |
|
592 | 592 | |
593 | 593 | // For seekable and writable streams, add all the same data to the |
594 | 594 | // cloned stream and then seek to the same offset. |
595 | - if (true === $seekable && !\in_array($mode, ['r', 'rb', 'rt'])) { |
|
596 | - $offset = ftell($inputStream); |
|
597 | - rewind($inputStream); |
|
598 | - stream_copy_to_stream($inputStream, $cloneStream); |
|
599 | - fseek($inputStream, $offset); |
|
600 | - fseek($cloneStream, $offset); |
|
595 | + if ( true === $seekable && ! \in_array( $mode, [ 'r', 'rb', 'rt' ] ) ) { |
|
596 | + $offset = ftell( $inputStream ); |
|
597 | + rewind( $inputStream ); |
|
598 | + stream_copy_to_stream( $inputStream, $cloneStream ); |
|
599 | + fseek( $inputStream, $offset ); |
|
600 | + fseek( $cloneStream, $offset ); |
|
601 | 601 | } |
602 | 602 | |
603 | 603 | return $cloneStream; |
@@ -42,18 +42,18 @@ discard block |
||
42 | 42 | /** |
43 | 43 | * Table headers. |
44 | 44 | */ |
45 | - private $headers = []; |
|
45 | + private $headers = [ ]; |
|
46 | 46 | |
47 | 47 | /** |
48 | 48 | * Table rows. |
49 | 49 | */ |
50 | - private $rows = []; |
|
50 | + private $rows = [ ]; |
|
51 | 51 | private $horizontal = false; |
52 | 52 | |
53 | 53 | /** |
54 | 54 | * Column widths cache. |
55 | 55 | */ |
56 | - private $effectiveColumnWidths = []; |
|
56 | + private $effectiveColumnWidths = [ ]; |
|
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Number of columns cache. |
@@ -75,41 +75,41 @@ discard block |
||
75 | 75 | /** |
76 | 76 | * @var array |
77 | 77 | */ |
78 | - private $columnStyles = []; |
|
78 | + private $columnStyles = [ ]; |
|
79 | 79 | |
80 | 80 | /** |
81 | 81 | * User set column widths. |
82 | 82 | * |
83 | 83 | * @var array |
84 | 84 | */ |
85 | - private $columnWidths = []; |
|
86 | - private $columnMaxWidths = []; |
|
85 | + private $columnWidths = [ ]; |
|
86 | + private $columnMaxWidths = [ ]; |
|
87 | 87 | |
88 | 88 | private static $styles; |
89 | 89 | |
90 | 90 | private $rendered = false; |
91 | 91 | |
92 | - public function __construct(OutputInterface $output) |
|
92 | + public function __construct( OutputInterface $output ) |
|
93 | 93 | { |
94 | 94 | $this->output = $output; |
95 | 95 | |
96 | - if (!self::$styles) { |
|
96 | + if ( ! self::$styles ) { |
|
97 | 97 | self::$styles = self::initStyles(); |
98 | 98 | } |
99 | 99 | |
100 | - $this->setStyle('default'); |
|
100 | + $this->setStyle( 'default' ); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
104 | 104 | * Sets a style definition. |
105 | 105 | */ |
106 | - public static function setStyleDefinition(string $name, TableStyle $style) |
|
106 | + public static function setStyleDefinition( string $name, TableStyle $style ) |
|
107 | 107 | { |
108 | - if (!self::$styles) { |
|
108 | + if ( ! self::$styles ) { |
|
109 | 109 | self::$styles = self::initStyles(); |
110 | 110 | } |
111 | 111 | |
112 | - self::$styles[$name] = $style; |
|
112 | + self::$styles[ $name ] = $style; |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -117,17 +117,17 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return TableStyle |
119 | 119 | */ |
120 | - public static function getStyleDefinition(string $name) |
|
120 | + public static function getStyleDefinition( string $name ) |
|
121 | 121 | { |
122 | - if (!self::$styles) { |
|
122 | + if ( ! self::$styles ) { |
|
123 | 123 | self::$styles = self::initStyles(); |
124 | 124 | } |
125 | 125 | |
126 | - if (isset(self::$styles[$name])) { |
|
127 | - return self::$styles[$name]; |
|
126 | + if ( isset( self::$styles[ $name ] ) ) { |
|
127 | + return self::$styles[ $name ]; |
|
128 | 128 | } |
129 | 129 | |
130 | - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); |
|
130 | + throw new InvalidArgumentException( sprintf( 'Style "%s" is not defined.', $name ) ); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | /** |
@@ -137,9 +137,9 @@ discard block |
||
137 | 137 | * |
138 | 138 | * @return $this |
139 | 139 | */ |
140 | - public function setStyle($name) |
|
140 | + public function setStyle( $name ) |
|
141 | 141 | { |
142 | - $this->style = $this->resolveStyle($name); |
|
142 | + $this->style = $this->resolveStyle( $name ); |
|
143 | 143 | |
144 | 144 | return $this; |
145 | 145 | } |
@@ -161,9 +161,9 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @return $this |
163 | 163 | */ |
164 | - public function setColumnStyle(int $columnIndex, $name) |
|
164 | + public function setColumnStyle( int $columnIndex, $name ) |
|
165 | 165 | { |
166 | - $this->columnStyles[$columnIndex] = $this->resolveStyle($name); |
|
166 | + $this->columnStyles[ $columnIndex ] = $this->resolveStyle( $name ); |
|
167 | 167 | |
168 | 168 | return $this; |
169 | 169 | } |
@@ -175,9 +175,9 @@ discard block |
||
175 | 175 | * |
176 | 176 | * @return TableStyle |
177 | 177 | */ |
178 | - public function getColumnStyle(int $columnIndex) |
|
178 | + public function getColumnStyle( int $columnIndex ) |
|
179 | 179 | { |
180 | - return $this->columnStyles[$columnIndex] ?? $this->getStyle(); |
|
180 | + return $this->columnStyles[ $columnIndex ] ?? $this->getStyle(); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
@@ -185,9 +185,9 @@ discard block |
||
185 | 185 | * |
186 | 186 | * @return $this |
187 | 187 | */ |
188 | - public function setColumnWidth(int $columnIndex, int $width) |
|
188 | + public function setColumnWidth( int $columnIndex, int $width ) |
|
189 | 189 | { |
190 | - $this->columnWidths[$columnIndex] = $width; |
|
190 | + $this->columnWidths[ $columnIndex ] = $width; |
|
191 | 191 | |
192 | 192 | return $this; |
193 | 193 | } |
@@ -197,11 +197,11 @@ discard block |
||
197 | 197 | * |
198 | 198 | * @return $this |
199 | 199 | */ |
200 | - public function setColumnWidths(array $widths) |
|
200 | + public function setColumnWidths( array $widths ) |
|
201 | 201 | { |
202 | - $this->columnWidths = []; |
|
203 | - foreach ($widths as $index => $width) { |
|
204 | - $this->setColumnWidth($index, $width); |
|
202 | + $this->columnWidths = [ ]; |
|
203 | + foreach ( $widths as $index => $width ) { |
|
204 | + $this->setColumnWidth( $index, $width ); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | return $this; |
@@ -215,22 +215,22 @@ discard block |
||
215 | 215 | * |
216 | 216 | * @return $this |
217 | 217 | */ |
218 | - public function setColumnMaxWidth(int $columnIndex, int $width): self |
|
218 | + public function setColumnMaxWidth( int $columnIndex, int $width ): self |
|
219 | 219 | { |
220 | - if (!$this->output->getFormatter() instanceof WrappableOutputFormatterInterface) { |
|
221 | - throw new \LogicException(sprintf('Setting a maximum column width is only supported when using a "%s" formatter, got "%s".', WrappableOutputFormatterInterface::class, get_debug_type($this->output->getFormatter()))); |
|
220 | + if ( ! $this->output->getFormatter() instanceof WrappableOutputFormatterInterface ) { |
|
221 | + throw new \LogicException( sprintf( 'Setting a maximum column width is only supported when using a "%s" formatter, got "%s".', WrappableOutputFormatterInterface::class, get_debug_type( $this->output->getFormatter() ) ) ); |
|
222 | 222 | } |
223 | 223 | |
224 | - $this->columnMaxWidths[$columnIndex] = $width; |
|
224 | + $this->columnMaxWidths[ $columnIndex ] = $width; |
|
225 | 225 | |
226 | 226 | return $this; |
227 | 227 | } |
228 | 228 | |
229 | - public function setHeaders(array $headers) |
|
229 | + public function setHeaders( array $headers ) |
|
230 | 230 | { |
231 | - $headers = array_values($headers); |
|
232 | - if (!empty($headers) && !\is_array($headers[0])) { |
|
233 | - $headers = [$headers]; |
|
231 | + $headers = array_values( $headers ); |
|
232 | + if ( ! empty( $headers ) && ! \is_array( $headers[ 0 ] ) ) { |
|
233 | + $headers = [ $headers ]; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | $this->headers = $headers; |
@@ -238,35 +238,35 @@ discard block |
||
238 | 238 | return $this; |
239 | 239 | } |
240 | 240 | |
241 | - public function setRows(array $rows) |
|
241 | + public function setRows( array $rows ) |
|
242 | 242 | { |
243 | - $this->rows = []; |
|
243 | + $this->rows = [ ]; |
|
244 | 244 | |
245 | - return $this->addRows($rows); |
|
245 | + return $this->addRows( $rows ); |
|
246 | 246 | } |
247 | 247 | |
248 | - public function addRows(array $rows) |
|
248 | + public function addRows( array $rows ) |
|
249 | 249 | { |
250 | - foreach ($rows as $row) { |
|
251 | - $this->addRow($row); |
|
250 | + foreach ( $rows as $row ) { |
|
251 | + $this->addRow( $row ); |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | return $this; |
255 | 255 | } |
256 | 256 | |
257 | - public function addRow($row) |
|
257 | + public function addRow( $row ) |
|
258 | 258 | { |
259 | - if ($row instanceof TableSeparator) { |
|
260 | - $this->rows[] = $row; |
|
259 | + if ( $row instanceof TableSeparator ) { |
|
260 | + $this->rows[ ] = $row; |
|
261 | 261 | |
262 | 262 | return $this; |
263 | 263 | } |
264 | 264 | |
265 | - if (!\is_array($row)) { |
|
266 | - throw new InvalidArgumentException('A row must be an array or a TableSeparator instance.'); |
|
265 | + if ( ! \is_array( $row ) ) { |
|
266 | + throw new InvalidArgumentException( 'A row must be an array or a TableSeparator instance.' ); |
|
267 | 267 | } |
268 | 268 | |
269 | - $this->rows[] = array_values($row); |
|
269 | + $this->rows[ ] = array_values( $row ); |
|
270 | 270 | |
271 | 271 | return $this; |
272 | 272 | } |
@@ -274,44 +274,44 @@ discard block |
||
274 | 274 | /** |
275 | 275 | * Adds a row to the table, and re-renders the table. |
276 | 276 | */ |
277 | - public function appendRow($row): self |
|
277 | + public function appendRow( $row ): self |
|
278 | 278 | { |
279 | - if (!$this->output instanceof ConsoleSectionOutput) { |
|
280 | - throw new RuntimeException(sprintf('Output should be an instance of "%s" when calling "%s".', ConsoleSectionOutput::class, __METHOD__)); |
|
279 | + if ( ! $this->output instanceof ConsoleSectionOutput ) { |
|
280 | + throw new RuntimeException( sprintf( 'Output should be an instance of "%s" when calling "%s".', ConsoleSectionOutput::class, __METHOD__ ) ); |
|
281 | 281 | } |
282 | 282 | |
283 | - if ($this->rendered) { |
|
284 | - $this->output->clear($this->calculateRowCount()); |
|
283 | + if ( $this->rendered ) { |
|
284 | + $this->output->clear( $this->calculateRowCount() ); |
|
285 | 285 | } |
286 | 286 | |
287 | - $this->addRow($row); |
|
287 | + $this->addRow( $row ); |
|
288 | 288 | $this->render(); |
289 | 289 | |
290 | 290 | return $this; |
291 | 291 | } |
292 | 292 | |
293 | - public function setRow($column, array $row) |
|
293 | + public function setRow( $column, array $row ) |
|
294 | 294 | { |
295 | - $this->rows[$column] = $row; |
|
295 | + $this->rows[ $column ] = $row; |
|
296 | 296 | |
297 | 297 | return $this; |
298 | 298 | } |
299 | 299 | |
300 | - public function setHeaderTitle(?string $title): self |
|
300 | + public function setHeaderTitle( ?string $title ): self |
|
301 | 301 | { |
302 | 302 | $this->headerTitle = $title; |
303 | 303 | |
304 | 304 | return $this; |
305 | 305 | } |
306 | 306 | |
307 | - public function setFooterTitle(?string $title): self |
|
307 | + public function setFooterTitle( ?string $title ): self |
|
308 | 308 | { |
309 | 309 | $this->footerTitle = $title; |
310 | 310 | |
311 | 311 | return $this; |
312 | 312 | } |
313 | 313 | |
314 | - public function setHorizontal(bool $horizontal = true): self |
|
314 | + public function setHorizontal( bool $horizontal = true ): self |
|
315 | 315 | { |
316 | 316 | $this->horizontal = $horizontal; |
317 | 317 | |
@@ -334,52 +334,52 @@ discard block |
||
334 | 334 | public function render() |
335 | 335 | { |
336 | 336 | $divider = new TableSeparator(); |
337 | - if ($this->horizontal) { |
|
338 | - $rows = []; |
|
339 | - foreach ($this->headers[0] ?? [] as $i => $header) { |
|
340 | - $rows[$i] = [$header]; |
|
341 | - foreach ($this->rows as $row) { |
|
342 | - if ($row instanceof TableSeparator) { |
|
337 | + if ( $this->horizontal ) { |
|
338 | + $rows = [ ]; |
|
339 | + foreach ( $this->headers[ 0 ] ?? [ ] as $i => $header ) { |
|
340 | + $rows[ $i ] = [ $header ]; |
|
341 | + foreach ( $this->rows as $row ) { |
|
342 | + if ( $row instanceof TableSeparator ) { |
|
343 | 343 | continue; |
344 | 344 | } |
345 | - if (isset($row[$i])) { |
|
346 | - $rows[$i][] = $row[$i]; |
|
347 | - } elseif ($rows[$i][0] instanceof TableCell && $rows[$i][0]->getColspan() >= 2) { |
|
345 | + if ( isset( $row[ $i ] ) ) { |
|
346 | + $rows[ $i ][ ] = $row[ $i ]; |
|
347 | + } elseif ( $rows[ $i ][ 0 ] instanceof TableCell && $rows[ $i ][ 0 ]->getColspan() >= 2 ) { |
|
348 | 348 | // Noop, there is a "title" |
349 | 349 | } else { |
350 | - $rows[$i][] = null; |
|
350 | + $rows[ $i ][ ] = null; |
|
351 | 351 | } |
352 | 352 | } |
353 | 353 | } |
354 | 354 | } else { |
355 | - $rows = array_merge($this->headers, [$divider], $this->rows); |
|
355 | + $rows = array_merge( $this->headers, [ $divider ], $this->rows ); |
|
356 | 356 | } |
357 | 357 | |
358 | - $this->calculateNumberOfColumns($rows); |
|
358 | + $this->calculateNumberOfColumns( $rows ); |
|
359 | 359 | |
360 | - $rows = $this->buildTableRows($rows); |
|
361 | - $this->calculateColumnsWidth($rows); |
|
360 | + $rows = $this->buildTableRows( $rows ); |
|
361 | + $this->calculateColumnsWidth( $rows ); |
|
362 | 362 | |
363 | - $isHeader = !$this->horizontal; |
|
363 | + $isHeader = ! $this->horizontal; |
|
364 | 364 | $isFirstRow = $this->horizontal; |
365 | - $hasTitle = (bool) $this->headerTitle; |
|
366 | - foreach ($rows as $row) { |
|
367 | - if ($divider === $row) { |
|
365 | + $hasTitle = (bool)$this->headerTitle; |
|
366 | + foreach ( $rows as $row ) { |
|
367 | + if ( $divider === $row ) { |
|
368 | 368 | $isHeader = false; |
369 | 369 | $isFirstRow = true; |
370 | 370 | |
371 | 371 | continue; |
372 | 372 | } |
373 | - if ($row instanceof TableSeparator) { |
|
373 | + if ( $row instanceof TableSeparator ) { |
|
374 | 374 | $this->renderRowSeparator(); |
375 | 375 | |
376 | 376 | continue; |
377 | 377 | } |
378 | - if (!$row) { |
|
378 | + if ( ! $row ) { |
|
379 | 379 | continue; |
380 | 380 | } |
381 | 381 | |
382 | - if ($isHeader || $isFirstRow) { |
|
382 | + if ( $isHeader || $isFirstRow ) { |
|
383 | 383 | $this->renderRowSeparator( |
384 | 384 | $isHeader ? self::SEPARATOR_TOP : self::SEPARATOR_TOP_BOTTOM, |
385 | 385 | $hasTitle ? $this->headerTitle : null, |
@@ -388,13 +388,13 @@ discard block |
||
388 | 388 | $isFirstRow = false; |
389 | 389 | $hasTitle = false; |
390 | 390 | } |
391 | - if ($this->horizontal) { |
|
392 | - $this->renderRow($row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat()); |
|
391 | + if ( $this->horizontal ) { |
|
392 | + $this->renderRow( $row, $this->style->getCellRowFormat(), $this->style->getCellHeaderFormat() ); |
|
393 | 393 | } else { |
394 | - $this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat()); |
|
394 | + $this->renderRow( $row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat() ); |
|
395 | 395 | } |
396 | 396 | } |
397 | - $this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat()); |
|
397 | + $this->renderRowSeparator( self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat() ); |
|
398 | 398 | |
399 | 399 | $this->cleanup(); |
400 | 400 | $this->rendered = true; |
@@ -407,62 +407,62 @@ discard block |
||
407 | 407 | * |
408 | 408 | * +-----+-----------+-------+ |
409 | 409 | */ |
410 | - private function renderRowSeparator(int $type = self::SEPARATOR_MID, string $title = null, string $titleFormat = null) |
|
410 | + private function renderRowSeparator( int $type = self::SEPARATOR_MID, string $title = null, string $titleFormat = null ) |
|
411 | 411 | { |
412 | - if (0 === $count = $this->numberOfColumns) { |
|
412 | + if ( 0 === $count = $this->numberOfColumns ) { |
|
413 | 413 | return; |
414 | 414 | } |
415 | 415 | |
416 | 416 | $borders = $this->style->getBorderChars(); |
417 | - if (!$borders[0] && !$borders[2] && !$this->style->getCrossingChar()) { |
|
417 | + if ( ! $borders[ 0 ] && ! $borders[ 2 ] && ! $this->style->getCrossingChar() ) { |
|
418 | 418 | return; |
419 | 419 | } |
420 | 420 | |
421 | 421 | $crossings = $this->style->getCrossingChars(); |
422 | - if (self::SEPARATOR_MID === $type) { |
|
423 | - [$horizontal, $leftChar, $midChar, $rightChar] = [$borders[2], $crossings[8], $crossings[0], $crossings[4]]; |
|
424 | - } elseif (self::SEPARATOR_TOP === $type) { |
|
425 | - [$horizontal, $leftChar, $midChar, $rightChar] = [$borders[0], $crossings[1], $crossings[2], $crossings[3]]; |
|
426 | - } elseif (self::SEPARATOR_TOP_BOTTOM === $type) { |
|
427 | - [$horizontal, $leftChar, $midChar, $rightChar] = [$borders[0], $crossings[9], $crossings[10], $crossings[11]]; |
|
422 | + if ( self::SEPARATOR_MID === $type ) { |
|
423 | + [ $horizontal, $leftChar, $midChar, $rightChar ] = [ $borders[ 2 ], $crossings[ 8 ], $crossings[ 0 ], $crossings[ 4 ] ]; |
|
424 | + } elseif ( self::SEPARATOR_TOP === $type ) { |
|
425 | + [ $horizontal, $leftChar, $midChar, $rightChar ] = [ $borders[ 0 ], $crossings[ 1 ], $crossings[ 2 ], $crossings[ 3 ] ]; |
|
426 | + } elseif ( self::SEPARATOR_TOP_BOTTOM === $type ) { |
|
427 | + [ $horizontal, $leftChar, $midChar, $rightChar ] = [ $borders[ 0 ], $crossings[ 9 ], $crossings[ 10 ], $crossings[ 11 ] ]; |
|
428 | 428 | } else { |
429 | - [$horizontal, $leftChar, $midChar, $rightChar] = [$borders[0], $crossings[7], $crossings[6], $crossings[5]]; |
|
429 | + [ $horizontal, $leftChar, $midChar, $rightChar ] = [ $borders[ 0 ], $crossings[ 7 ], $crossings[ 6 ], $crossings[ 5 ] ]; |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | $markup = $leftChar; |
433 | - for ($column = 0; $column < $count; ++$column) { |
|
434 | - $markup .= str_repeat($horizontal, $this->effectiveColumnWidths[$column]); |
|
433 | + for ( $column = 0; $column < $count; ++$column ) { |
|
434 | + $markup .= str_repeat( $horizontal, $this->effectiveColumnWidths[ $column ] ); |
|
435 | 435 | $markup .= $column === $count - 1 ? $rightChar : $midChar; |
436 | 436 | } |
437 | 437 | |
438 | - if (null !== $title) { |
|
439 | - $titleLength = Helper::width(Helper::removeDecoration($formatter = $this->output->getFormatter(), $formattedTitle = sprintf($titleFormat, $title))); |
|
440 | - $markupLength = Helper::width($markup); |
|
441 | - if ($titleLength > $limit = $markupLength - 4) { |
|
438 | + if ( null !== $title ) { |
|
439 | + $titleLength = Helper::width( Helper::removeDecoration( $formatter = $this->output->getFormatter(), $formattedTitle = sprintf( $titleFormat, $title ) ) ); |
|
440 | + $markupLength = Helper::width( $markup ); |
|
441 | + if ( $titleLength > $limit = $markupLength - 4 ) { |
|
442 | 442 | $titleLength = $limit; |
443 | - $formatLength = Helper::width(Helper::removeDecoration($formatter, sprintf($titleFormat, ''))); |
|
444 | - $formattedTitle = sprintf($titleFormat, Helper::substr($title, 0, $limit - $formatLength - 3).'...'); |
|
443 | + $formatLength = Helper::width( Helper::removeDecoration( $formatter, sprintf( $titleFormat, '' ) ) ); |
|
444 | + $formattedTitle = sprintf( $titleFormat, Helper::substr( $title, 0, $limit - $formatLength - 3 ) . '...' ); |
|
445 | 445 | } |
446 | 446 | |
447 | - $titleStart = intdiv($markupLength - $titleLength, 2); |
|
448 | - if (false === mb_detect_encoding($markup, null, true)) { |
|
449 | - $markup = substr_replace($markup, $formattedTitle, $titleStart, $titleLength); |
|
447 | + $titleStart = intdiv( $markupLength - $titleLength, 2 ); |
|
448 | + if ( false === mb_detect_encoding( $markup, null, true ) ) { |
|
449 | + $markup = substr_replace( $markup, $formattedTitle, $titleStart, $titleLength ); |
|
450 | 450 | } else { |
451 | - $markup = mb_substr($markup, 0, $titleStart).$formattedTitle.mb_substr($markup, $titleStart + $titleLength); |
|
451 | + $markup = mb_substr( $markup, 0, $titleStart ) . $formattedTitle . mb_substr( $markup, $titleStart + $titleLength ); |
|
452 | 452 | } |
453 | 453 | } |
454 | 454 | |
455 | - $this->output->writeln(sprintf($this->style->getBorderFormat(), $markup)); |
|
455 | + $this->output->writeln( sprintf( $this->style->getBorderFormat(), $markup ) ); |
|
456 | 456 | } |
457 | 457 | |
458 | 458 | /** |
459 | 459 | * Renders vertical column separator. |
460 | 460 | */ |
461 | - private function renderColumnSeparator(int $type = self::BORDER_OUTSIDE): string |
|
461 | + private function renderColumnSeparator( int $type = self::BORDER_OUTSIDE ): string |
|
462 | 462 | { |
463 | 463 | $borders = $this->style->getBorderChars(); |
464 | 464 | |
465 | - return sprintf($this->style->getBorderFormat(), self::BORDER_OUTSIDE === $type ? $borders[1] : $borders[3]); |
|
465 | + return sprintf( $this->style->getBorderFormat(), self::BORDER_OUTSIDE === $type ? $borders[ 1 ] : $borders[ 3 ] ); |
|
466 | 466 | } |
467 | 467 | |
468 | 468 | /** |
@@ -472,137 +472,137 @@ discard block |
||
472 | 472 | * |
473 | 473 | * | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | |
474 | 474 | */ |
475 | - private function renderRow(array $row, string $cellFormat, string $firstCellFormat = null) |
|
476 | - { |
|
477 | - $rowContent = $this->renderColumnSeparator(self::BORDER_OUTSIDE); |
|
478 | - $columns = $this->getRowColumns($row); |
|
479 | - $last = \count($columns) - 1; |
|
480 | - foreach ($columns as $i => $column) { |
|
481 | - if ($firstCellFormat && 0 === $i) { |
|
482 | - $rowContent .= $this->renderCell($row, $column, $firstCellFormat); |
|
475 | + private function renderRow( array $row, string $cellFormat, string $firstCellFormat = null ) |
|
476 | + { |
|
477 | + $rowContent = $this->renderColumnSeparator( self::BORDER_OUTSIDE ); |
|
478 | + $columns = $this->getRowColumns( $row ); |
|
479 | + $last = \count( $columns ) - 1; |
|
480 | + foreach ( $columns as $i => $column ) { |
|
481 | + if ( $firstCellFormat && 0 === $i ) { |
|
482 | + $rowContent .= $this->renderCell( $row, $column, $firstCellFormat ); |
|
483 | 483 | } else { |
484 | - $rowContent .= $this->renderCell($row, $column, $cellFormat); |
|
484 | + $rowContent .= $this->renderCell( $row, $column, $cellFormat ); |
|
485 | 485 | } |
486 | - $rowContent .= $this->renderColumnSeparator($last === $i ? self::BORDER_OUTSIDE : self::BORDER_INSIDE); |
|
486 | + $rowContent .= $this->renderColumnSeparator( $last === $i ? self::BORDER_OUTSIDE : self::BORDER_INSIDE ); |
|
487 | 487 | } |
488 | - $this->output->writeln($rowContent); |
|
488 | + $this->output->writeln( $rowContent ); |
|
489 | 489 | } |
490 | 490 | |
491 | 491 | /** |
492 | 492 | * Renders table cell with padding. |
493 | 493 | */ |
494 | - private function renderCell(array $row, int $column, string $cellFormat): string |
|
494 | + private function renderCell( array $row, int $column, string $cellFormat ): string |
|
495 | 495 | { |
496 | - $cell = $row[$column] ?? ''; |
|
497 | - $width = $this->effectiveColumnWidths[$column]; |
|
498 | - if ($cell instanceof TableCell && $cell->getColspan() > 1) { |
|
496 | + $cell = $row[ $column ] ?? ''; |
|
497 | + $width = $this->effectiveColumnWidths[ $column ]; |
|
498 | + if ( $cell instanceof TableCell && $cell->getColspan() > 1 ) { |
|
499 | 499 | // add the width of the following columns(numbers of colspan). |
500 | - foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) { |
|
501 | - $width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[$nextColumn]; |
|
500 | + foreach ( range( $column + 1, $column + $cell->getColspan() - 1 ) as $nextColumn ) { |
|
501 | + $width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[ $nextColumn ]; |
|
502 | 502 | } |
503 | 503 | } |
504 | 504 | |
505 | 505 | // str_pad won't work properly with multi-byte strings, we need to fix the padding |
506 | - if (false !== $encoding = mb_detect_encoding($cell, null, true)) { |
|
507 | - $width += \strlen($cell) - mb_strwidth($cell, $encoding); |
|
506 | + if ( false !== $encoding = mb_detect_encoding( $cell, null, true ) ) { |
|
507 | + $width += \strlen( $cell ) - mb_strwidth( $cell, $encoding ); |
|
508 | 508 | } |
509 | 509 | |
510 | - $style = $this->getColumnStyle($column); |
|
510 | + $style = $this->getColumnStyle( $column ); |
|
511 | 511 | |
512 | - if ($cell instanceof TableSeparator) { |
|
513 | - return sprintf($style->getBorderFormat(), str_repeat($style->getBorderChars()[2], $width)); |
|
512 | + if ( $cell instanceof TableSeparator ) { |
|
513 | + return sprintf( $style->getBorderFormat(), str_repeat( $style->getBorderChars()[ 2 ], $width ) ); |
|
514 | 514 | } |
515 | 515 | |
516 | - $width += Helper::length($cell) - Helper::length(Helper::removeDecoration($this->output->getFormatter(), $cell)); |
|
517 | - $content = sprintf($style->getCellRowContentFormat(), $cell); |
|
516 | + $width += Helper::length( $cell ) - Helper::length( Helper::removeDecoration( $this->output->getFormatter(), $cell ) ); |
|
517 | + $content = sprintf( $style->getCellRowContentFormat(), $cell ); |
|
518 | 518 | |
519 | 519 | $padType = $style->getPadType(); |
520 | - if ($cell instanceof TableCell && $cell->getStyle() instanceof TableCellStyle) { |
|
521 | - $isNotStyledByTag = !preg_match('/^<(\w+|(\w+=[\w,]+;?)*)>.+<\/(\w+|(\w+=\w+;?)*)?>$/', $cell); |
|
522 | - if ($isNotStyledByTag) { |
|
520 | + if ( $cell instanceof TableCell && $cell->getStyle() instanceof TableCellStyle ) { |
|
521 | + $isNotStyledByTag = ! preg_match( '/^<(\w+|(\w+=[\w,]+;?)*)>.+<\/(\w+|(\w+=\w+;?)*)?>$/', $cell ); |
|
522 | + if ( $isNotStyledByTag ) { |
|
523 | 523 | $cellFormat = $cell->getStyle()->getCellFormat(); |
524 | - if (!\is_string($cellFormat)) { |
|
525 | - $tag = http_build_query($cell->getStyle()->getTagOptions(), '', ';'); |
|
526 | - $cellFormat = '<'.$tag.'>%s</>'; |
|
524 | + if ( ! \is_string( $cellFormat ) ) { |
|
525 | + $tag = http_build_query( $cell->getStyle()->getTagOptions(), '', ';' ); |
|
526 | + $cellFormat = '<' . $tag . '>%s</>'; |
|
527 | 527 | } |
528 | 528 | |
529 | - if (strstr($content, '</>')) { |
|
530 | - $content = str_replace('</>', '', $content); |
|
529 | + if ( strstr( $content, '</>' ) ) { |
|
530 | + $content = str_replace( '</>', '', $content ); |
|
531 | 531 | $width -= 3; |
532 | 532 | } |
533 | - if (strstr($content, '<fg=default;bg=default>')) { |
|
534 | - $content = str_replace('<fg=default;bg=default>', '', $content); |
|
535 | - $width -= \strlen('<fg=default;bg=default>'); |
|
533 | + if ( strstr( $content, '<fg=default;bg=default>' ) ) { |
|
534 | + $content = str_replace( '<fg=default;bg=default>', '', $content ); |
|
535 | + $width -= \strlen( '<fg=default;bg=default>' ); |
|
536 | 536 | } |
537 | 537 | } |
538 | 538 | |
539 | 539 | $padType = $cell->getStyle()->getPadByAlign(); |
540 | 540 | } |
541 | 541 | |
542 | - return sprintf($cellFormat, str_pad($content, $width, $style->getPaddingChar(), $padType)); |
|
542 | + return sprintf( $cellFormat, str_pad( $content, $width, $style->getPaddingChar(), $padType ) ); |
|
543 | 543 | } |
544 | 544 | |
545 | 545 | /** |
546 | 546 | * Calculate number of columns for this table. |
547 | 547 | */ |
548 | - private function calculateNumberOfColumns(array $rows) |
|
548 | + private function calculateNumberOfColumns( array $rows ) |
|
549 | 549 | { |
550 | - $columns = [0]; |
|
551 | - foreach ($rows as $row) { |
|
552 | - if ($row instanceof TableSeparator) { |
|
550 | + $columns = [ 0 ]; |
|
551 | + foreach ( $rows as $row ) { |
|
552 | + if ( $row instanceof TableSeparator ) { |
|
553 | 553 | continue; |
554 | 554 | } |
555 | 555 | |
556 | - $columns[] = $this->getNumberOfColumns($row); |
|
556 | + $columns[ ] = $this->getNumberOfColumns( $row ); |
|
557 | 557 | } |
558 | 558 | |
559 | - $this->numberOfColumns = max($columns); |
|
559 | + $this->numberOfColumns = max( $columns ); |
|
560 | 560 | } |
561 | 561 | |
562 | - private function buildTableRows(array $rows): TableRows |
|
562 | + private function buildTableRows( array $rows ): TableRows |
|
563 | 563 | { |
564 | 564 | /** @var WrappableOutputFormatterInterface $formatter */ |
565 | 565 | $formatter = $this->output->getFormatter(); |
566 | - $unmergedRows = []; |
|
567 | - for ($rowKey = 0; $rowKey < \count($rows); ++$rowKey) { |
|
568 | - $rows = $this->fillNextRows($rows, $rowKey); |
|
566 | + $unmergedRows = [ ]; |
|
567 | + for ( $rowKey = 0; $rowKey < \count( $rows ); ++$rowKey ) { |
|
568 | + $rows = $this->fillNextRows( $rows, $rowKey ); |
|
569 | 569 | |
570 | 570 | // Remove any new line breaks and replace it with a new line |
571 | - foreach ($rows[$rowKey] as $column => $cell) { |
|
571 | + foreach ( $rows[ $rowKey ] as $column => $cell ) { |
|
572 | 572 | $colspan = $cell instanceof TableCell ? $cell->getColspan() : 1; |
573 | 573 | |
574 | - if (isset($this->columnMaxWidths[$column]) && Helper::width(Helper::removeDecoration($formatter, $cell)) > $this->columnMaxWidths[$column]) { |
|
575 | - $cell = $formatter->formatAndWrap($cell, $this->columnMaxWidths[$column] * $colspan); |
|
574 | + if ( isset( $this->columnMaxWidths[ $column ] ) && Helper::width( Helper::removeDecoration( $formatter, $cell ) ) > $this->columnMaxWidths[ $column ] ) { |
|
575 | + $cell = $formatter->formatAndWrap( $cell, $this->columnMaxWidths[ $column ] * $colspan ); |
|
576 | 576 | } |
577 | - if (!strstr($cell ?? '', "\n")) { |
|
577 | + if ( ! strstr( $cell ?? '', "\n" ) ) { |
|
578 | 578 | continue; |
579 | 579 | } |
580 | - $escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell))); |
|
581 | - $cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped; |
|
582 | - $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell)); |
|
583 | - foreach ($lines as $lineKey => $line) { |
|
584 | - if ($colspan > 1) { |
|
585 | - $line = new TableCell($line, ['colspan' => $colspan]); |
|
580 | + $escaped = implode( "\n", array_map( [ OutputFormatter::class, 'escapeTrailingBackslash' ], explode( "\n", $cell ) ) ); |
|
581 | + $cell = $cell instanceof TableCell ? new TableCell( $escaped, [ 'colspan' => $cell->getColspan() ] ) : $escaped; |
|
582 | + $lines = explode( "\n", str_replace( "\n", "<fg=default;bg=default>\n</>", $cell ) ); |
|
583 | + foreach ( $lines as $lineKey => $line ) { |
|
584 | + if ( $colspan > 1 ) { |
|
585 | + $line = new TableCell( $line, [ 'colspan' => $colspan ] ); |
|
586 | 586 | } |
587 | - if (0 === $lineKey) { |
|
588 | - $rows[$rowKey][$column] = $line; |
|
587 | + if ( 0 === $lineKey ) { |
|
588 | + $rows[ $rowKey ][ $column ] = $line; |
|
589 | 589 | } else { |
590 | - if (!\array_key_exists($rowKey, $unmergedRows) || !\array_key_exists($lineKey, $unmergedRows[$rowKey])) { |
|
591 | - $unmergedRows[$rowKey][$lineKey] = $this->copyRow($rows, $rowKey); |
|
590 | + if ( ! \array_key_exists( $rowKey, $unmergedRows ) || ! \array_key_exists( $lineKey, $unmergedRows[ $rowKey ] ) ) { |
|
591 | + $unmergedRows[ $rowKey ][ $lineKey ] = $this->copyRow( $rows, $rowKey ); |
|
592 | 592 | } |
593 | - $unmergedRows[$rowKey][$lineKey][$column] = $line; |
|
593 | + $unmergedRows[ $rowKey ][ $lineKey ][ $column ] = $line; |
|
594 | 594 | } |
595 | 595 | } |
596 | 596 | } |
597 | 597 | } |
598 | 598 | |
599 | - return new TableRows(function () use ($rows, $unmergedRows): \Traversable { |
|
600 | - foreach ($rows as $rowKey => $row) { |
|
601 | - yield $row instanceof TableSeparator ? $row : $this->fillCells($row); |
|
599 | + return new TableRows( function() use ( $rows, $unmergedRows ): \Traversable { |
|
600 | + foreach ( $rows as $rowKey => $row ) { |
|
601 | + yield $row instanceof TableSeparator ? $row : $this->fillCells( $row ); |
|
602 | 602 | |
603 | - if (isset($unmergedRows[$rowKey])) { |
|
604 | - foreach ($unmergedRows[$rowKey] as $row) { |
|
605 | - yield $row instanceof TableSeparator ? $row : $this->fillCells($row); |
|
603 | + if ( isset( $unmergedRows[ $rowKey ] ) ) { |
|
604 | + foreach ( $unmergedRows[ $rowKey ] as $row ) { |
|
605 | + yield $row instanceof TableSeparator ? $row : $this->fillCells( $row ); |
|
606 | 606 | } |
607 | 607 | } |
608 | 608 | } |
@@ -611,13 +611,13 @@ discard block |
||
611 | 611 | |
612 | 612 | private function calculateRowCount(): int |
613 | 613 | { |
614 | - $numberOfRows = \count(iterator_to_array($this->buildTableRows(array_merge($this->headers, [new TableSeparator()], $this->rows)))); |
|
614 | + $numberOfRows = \count( iterator_to_array( $this->buildTableRows( array_merge( $this->headers, [ new TableSeparator() ], $this->rows ) ) ) ); |
|
615 | 615 | |
616 | - if ($this->headers) { |
|
616 | + if ( $this->headers ) { |
|
617 | 617 | ++$numberOfRows; // Add row for header separator |
618 | 618 | } |
619 | 619 | |
620 | - if (\count($this->rows) > 0) { |
|
620 | + if ( \count( $this->rows ) > 0 ) { |
|
621 | 621 | ++$numberOfRows; // Add row for footer separator |
622 | 622 | } |
623 | 623 | |
@@ -629,51 +629,51 @@ discard block |
||
629 | 629 | * |
630 | 630 | * @throws InvalidArgumentException |
631 | 631 | */ |
632 | - private function fillNextRows(array $rows, int $line): array |
|
632 | + private function fillNextRows( array $rows, int $line ): array |
|
633 | 633 | { |
634 | - $unmergedRows = []; |
|
635 | - foreach ($rows[$line] as $column => $cell) { |
|
636 | - if (null !== $cell && !$cell instanceof TableCell && !is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) { |
|
637 | - throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing "__toString()", "%s" given.', get_debug_type($cell))); |
|
634 | + $unmergedRows = [ ]; |
|
635 | + foreach ( $rows[ $line ] as $column => $cell ) { |
|
636 | + if ( null !== $cell && ! $cell instanceof TableCell && ! is_scalar( $cell ) && ! ( \is_object( $cell ) && method_exists( $cell, '__toString' ) ) ) { |
|
637 | + throw new InvalidArgumentException( sprintf( 'A cell must be a TableCell, a scalar or an object implementing "__toString()", "%s" given.', get_debug_type( $cell ) ) ); |
|
638 | 638 | } |
639 | - if ($cell instanceof TableCell && $cell->getRowspan() > 1) { |
|
639 | + if ( $cell instanceof TableCell && $cell->getRowspan() > 1 ) { |
|
640 | 640 | $nbLines = $cell->getRowspan() - 1; |
641 | - $lines = [$cell]; |
|
642 | - if (strstr($cell, "\n")) { |
|
643 | - $lines = explode("\n", str_replace("\n", "<fg=default;bg=default>\n</>", $cell)); |
|
644 | - $nbLines = \count($lines) > $nbLines ? substr_count($cell, "\n") : $nbLines; |
|
641 | + $lines = [ $cell ]; |
|
642 | + if ( strstr( $cell, "\n" ) ) { |
|
643 | + $lines = explode( "\n", str_replace( "\n", "<fg=default;bg=default>\n</>", $cell ) ); |
|
644 | + $nbLines = \count( $lines ) > $nbLines ? substr_count( $cell, "\n" ) : $nbLines; |
|
645 | 645 | |
646 | - $rows[$line][$column] = new TableCell($lines[0], ['colspan' => $cell->getColspan(), 'style' => $cell->getStyle()]); |
|
647 | - unset($lines[0]); |
|
646 | + $rows[ $line ][ $column ] = new TableCell( $lines[ 0 ], [ 'colspan' => $cell->getColspan(), 'style' => $cell->getStyle() ] ); |
|
647 | + unset( $lines[ 0 ] ); |
|
648 | 648 | } |
649 | 649 | |
650 | 650 | // create a two dimensional array (rowspan x colspan) |
651 | - $unmergedRows = array_replace_recursive(array_fill($line + 1, $nbLines, []), $unmergedRows); |
|
652 | - foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) { |
|
653 | - $value = $lines[$unmergedRowKey - $line] ?? ''; |
|
654 | - $unmergedRows[$unmergedRowKey][$column] = new TableCell($value, ['colspan' => $cell->getColspan(), 'style' => $cell->getStyle()]); |
|
655 | - if ($nbLines === $unmergedRowKey - $line) { |
|
651 | + $unmergedRows = array_replace_recursive( array_fill( $line + 1, $nbLines, [ ] ), $unmergedRows ); |
|
652 | + foreach ( $unmergedRows as $unmergedRowKey => $unmergedRow ) { |
|
653 | + $value = $lines[ $unmergedRowKey - $line ] ?? ''; |
|
654 | + $unmergedRows[ $unmergedRowKey ][ $column ] = new TableCell( $value, [ 'colspan' => $cell->getColspan(), 'style' => $cell->getStyle() ] ); |
|
655 | + if ( $nbLines === $unmergedRowKey - $line ) { |
|
656 | 656 | break; |
657 | 657 | } |
658 | 658 | } |
659 | 659 | } |
660 | 660 | } |
661 | 661 | |
662 | - foreach ($unmergedRows as $unmergedRowKey => $unmergedRow) { |
|
662 | + foreach ( $unmergedRows as $unmergedRowKey => $unmergedRow ) { |
|
663 | 663 | // we need to know if $unmergedRow will be merged or inserted into $rows |
664 | - if (isset($rows[$unmergedRowKey]) && \is_array($rows[$unmergedRowKey]) && ($this->getNumberOfColumns($rows[$unmergedRowKey]) + $this->getNumberOfColumns($unmergedRows[$unmergedRowKey]) <= $this->numberOfColumns)) { |
|
665 | - foreach ($unmergedRow as $cellKey => $cell) { |
|
664 | + if ( isset( $rows[ $unmergedRowKey ] ) && \is_array( $rows[ $unmergedRowKey ] ) && ( $this->getNumberOfColumns( $rows[ $unmergedRowKey ] ) + $this->getNumberOfColumns( $unmergedRows[ $unmergedRowKey ] ) <= $this->numberOfColumns ) ) { |
|
665 | + foreach ( $unmergedRow as $cellKey => $cell ) { |
|
666 | 666 | // insert cell into row at cellKey position |
667 | - array_splice($rows[$unmergedRowKey], $cellKey, 0, [$cell]); |
|
667 | + array_splice( $rows[ $unmergedRowKey ], $cellKey, 0, [ $cell ] ); |
|
668 | 668 | } |
669 | 669 | } else { |
670 | - $row = $this->copyRow($rows, $unmergedRowKey - 1); |
|
671 | - foreach ($unmergedRow as $column => $cell) { |
|
672 | - if (!empty($cell)) { |
|
673 | - $row[$column] = $unmergedRow[$column]; |
|
670 | + $row = $this->copyRow( $rows, $unmergedRowKey - 1 ); |
|
671 | + foreach ( $unmergedRow as $column => $cell ) { |
|
672 | + if ( ! empty( $cell ) ) { |
|
673 | + $row[ $column ] = $unmergedRow[ $column ]; |
|
674 | 674 | } |
675 | 675 | } |
676 | - array_splice($rows, $unmergedRowKey, 0, [$row]); |
|
676 | + array_splice( $rows, $unmergedRowKey, 0, [ $row ] ); |
|
677 | 677 | } |
678 | 678 | } |
679 | 679 | |
@@ -683,16 +683,16 @@ discard block |
||
683 | 683 | /** |
684 | 684 | * fill cells for a row that contains colspan > 1. |
685 | 685 | */ |
686 | - private function fillCells(iterable $row) |
|
686 | + private function fillCells( iterable $row ) |
|
687 | 687 | { |
688 | - $newRow = []; |
|
688 | + $newRow = [ ]; |
|
689 | 689 | |
690 | - foreach ($row as $column => $cell) { |
|
691 | - $newRow[] = $cell; |
|
692 | - if ($cell instanceof TableCell && $cell->getColspan() > 1) { |
|
693 | - foreach (range($column + 1, $column + $cell->getColspan() - 1) as $position) { |
|
690 | + foreach ( $row as $column => $cell ) { |
|
691 | + $newRow[ ] = $cell; |
|
692 | + if ( $cell instanceof TableCell && $cell->getColspan() > 1 ) { |
|
693 | + foreach ( range( $column + 1, $column + $cell->getColspan() - 1 ) as $position ) { |
|
694 | 694 | // insert empty value at column position |
695 | - $newRow[] = ''; |
|
695 | + $newRow[ ] = ''; |
|
696 | 696 | } |
697 | 697 | } |
698 | 698 | } |
@@ -700,13 +700,13 @@ discard block |
||
700 | 700 | return $newRow ?: $row; |
701 | 701 | } |
702 | 702 | |
703 | - private function copyRow(array $rows, int $line): array |
|
703 | + private function copyRow( array $rows, int $line ): array |
|
704 | 704 | { |
705 | - $row = $rows[$line]; |
|
706 | - foreach ($row as $cellKey => $cellValue) { |
|
707 | - $row[$cellKey] = ''; |
|
708 | - if ($cellValue instanceof TableCell) { |
|
709 | - $row[$cellKey] = new TableCell('', ['colspan' => $cellValue->getColspan()]); |
|
705 | + $row = $rows[ $line ]; |
|
706 | + foreach ( $row as $cellKey => $cellValue ) { |
|
707 | + $row[ $cellKey ] = ''; |
|
708 | + if ( $cellValue instanceof TableCell ) { |
|
709 | + $row[ $cellKey ] = new TableCell( '', [ 'colspan' => $cellValue->getColspan() ] ); |
|
710 | 710 | } |
711 | 711 | } |
712 | 712 | |
@@ -716,11 +716,11 @@ discard block |
||
716 | 716 | /** |
717 | 717 | * Gets number of columns by row. |
718 | 718 | */ |
719 | - private function getNumberOfColumns(array $row): int |
|
719 | + private function getNumberOfColumns( array $row ): int |
|
720 | 720 | { |
721 | - $columns = \count($row); |
|
722 | - foreach ($row as $column) { |
|
723 | - $columns += $column instanceof TableCell ? ($column->getColspan() - 1) : 0; |
|
721 | + $columns = \count( $row ); |
|
722 | + foreach ( $row as $column ) { |
|
723 | + $columns += $column instanceof TableCell ? ( $column->getColspan() - 1 ) : 0; |
|
724 | 724 | } |
725 | 725 | |
726 | 726 | return $columns; |
@@ -729,13 +729,13 @@ discard block |
||
729 | 729 | /** |
730 | 730 | * Gets list of columns for the given row. |
731 | 731 | */ |
732 | - private function getRowColumns(array $row): array |
|
732 | + private function getRowColumns( array $row ): array |
|
733 | 733 | { |
734 | - $columns = range(0, $this->numberOfColumns - 1); |
|
735 | - foreach ($row as $cellKey => $cell) { |
|
736 | - if ($cell instanceof TableCell && $cell->getColspan() > 1) { |
|
734 | + $columns = range( 0, $this->numberOfColumns - 1 ); |
|
735 | + foreach ( $row as $cellKey => $cell ) { |
|
736 | + if ( $cell instanceof TableCell && $cell->getColspan() > 1 ) { |
|
737 | 737 | // exclude grouped columns. |
738 | - $columns = array_diff($columns, range($cellKey + 1, $cellKey + $cell->getColspan() - 1)); |
|
738 | + $columns = array_diff( $columns, range( $cellKey + 1, $cellKey + $cell->getColspan() - 1 ) ); |
|
739 | 739 | } |
740 | 740 | } |
741 | 741 | |
@@ -745,53 +745,53 @@ discard block |
||
745 | 745 | /** |
746 | 746 | * Calculates columns widths. |
747 | 747 | */ |
748 | - private function calculateColumnsWidth(iterable $rows) |
|
748 | + private function calculateColumnsWidth( iterable $rows ) |
|
749 | 749 | { |
750 | - for ($column = 0; $column < $this->numberOfColumns; ++$column) { |
|
751 | - $lengths = []; |
|
752 | - foreach ($rows as $row) { |
|
753 | - if ($row instanceof TableSeparator) { |
|
750 | + for ( $column = 0; $column < $this->numberOfColumns; ++$column ) { |
|
751 | + $lengths = [ ]; |
|
752 | + foreach ( $rows as $row ) { |
|
753 | + if ( $row instanceof TableSeparator ) { |
|
754 | 754 | continue; |
755 | 755 | } |
756 | 756 | |
757 | - foreach ($row as $i => $cell) { |
|
758 | - if ($cell instanceof TableCell) { |
|
759 | - $textContent = Helper::removeDecoration($this->output->getFormatter(), $cell); |
|
760 | - $textLength = Helper::width($textContent); |
|
761 | - if ($textLength > 0) { |
|
762 | - $contentColumns = str_split($textContent, ceil($textLength / $cell->getColspan())); |
|
763 | - foreach ($contentColumns as $position => $content) { |
|
764 | - $row[$i + $position] = $content; |
|
757 | + foreach ( $row as $i => $cell ) { |
|
758 | + if ( $cell instanceof TableCell ) { |
|
759 | + $textContent = Helper::removeDecoration( $this->output->getFormatter(), $cell ); |
|
760 | + $textLength = Helper::width( $textContent ); |
|
761 | + if ( $textLength > 0 ) { |
|
762 | + $contentColumns = str_split( $textContent, ceil( $textLength / $cell->getColspan() ) ); |
|
763 | + foreach ( $contentColumns as $position => $content ) { |
|
764 | + $row[ $i + $position ] = $content; |
|
765 | 765 | } |
766 | 766 | } |
767 | 767 | } |
768 | 768 | } |
769 | 769 | |
770 | - $lengths[] = $this->getCellWidth($row, $column); |
|
770 | + $lengths[ ] = $this->getCellWidth( $row, $column ); |
|
771 | 771 | } |
772 | 772 | |
773 | - $this->effectiveColumnWidths[$column] = max($lengths) + Helper::width($this->style->getCellRowContentFormat()) - 2; |
|
773 | + $this->effectiveColumnWidths[ $column ] = max( $lengths ) + Helper::width( $this->style->getCellRowContentFormat() ) - 2; |
|
774 | 774 | } |
775 | 775 | } |
776 | 776 | |
777 | 777 | private function getColumnSeparatorWidth(): int |
778 | 778 | { |
779 | - return Helper::width(sprintf($this->style->getBorderFormat(), $this->style->getBorderChars()[3])); |
|
779 | + return Helper::width( sprintf( $this->style->getBorderFormat(), $this->style->getBorderChars()[ 3 ] ) ); |
|
780 | 780 | } |
781 | 781 | |
782 | - private function getCellWidth(array $row, int $column): int |
|
782 | + private function getCellWidth( array $row, int $column ): int |
|
783 | 783 | { |
784 | 784 | $cellWidth = 0; |
785 | 785 | |
786 | - if (isset($row[$column])) { |
|
787 | - $cell = $row[$column]; |
|
788 | - $cellWidth = Helper::width(Helper::removeDecoration($this->output->getFormatter(), $cell)); |
|
786 | + if ( isset( $row[ $column ] ) ) { |
|
787 | + $cell = $row[ $column ]; |
|
788 | + $cellWidth = Helper::width( Helper::removeDecoration( $this->output->getFormatter(), $cell ) ); |
|
789 | 789 | } |
790 | 790 | |
791 | - $columnWidth = $this->columnWidths[$column] ?? 0; |
|
792 | - $cellWidth = max($cellWidth, $columnWidth); |
|
791 | + $columnWidth = $this->columnWidths[ $column ] ?? 0; |
|
792 | + $cellWidth = max( $cellWidth, $columnWidth ); |
|
793 | 793 | |
794 | - return isset($this->columnMaxWidths[$column]) ? min($this->columnMaxWidths[$column], $cellWidth) : $cellWidth; |
|
794 | + return isset( $this->columnMaxWidths[ $column ] ) ? min( $this->columnMaxWidths[ $column ], $cellWidth ) : $cellWidth; |
|
795 | 795 | } |
796 | 796 | |
797 | 797 | /** |
@@ -799,7 +799,7 @@ discard block |
||
799 | 799 | */ |
800 | 800 | private function cleanup() |
801 | 801 | { |
802 | - $this->effectiveColumnWidths = []; |
|
802 | + $this->effectiveColumnWidths = [ ]; |
|
803 | 803 | $this->numberOfColumns = null; |
804 | 804 | } |
805 | 805 | |
@@ -807,37 +807,37 @@ discard block |
||
807 | 807 | { |
808 | 808 | $borderless = new TableStyle(); |
809 | 809 | $borderless |
810 | - ->setHorizontalBorderChars('=') |
|
811 | - ->setVerticalBorderChars(' ') |
|
812 | - ->setDefaultCrossingChar(' ') |
|
810 | + ->setHorizontalBorderChars( '=' ) |
|
811 | + ->setVerticalBorderChars( ' ' ) |
|
812 | + ->setDefaultCrossingChar( ' ' ) |
|
813 | 813 | ; |
814 | 814 | |
815 | 815 | $compact = new TableStyle(); |
816 | 816 | $compact |
817 | - ->setHorizontalBorderChars('') |
|
818 | - ->setVerticalBorderChars(' ') |
|
819 | - ->setDefaultCrossingChar('') |
|
820 | - ->setCellRowContentFormat('%s') |
|
817 | + ->setHorizontalBorderChars( '' ) |
|
818 | + ->setVerticalBorderChars( ' ' ) |
|
819 | + ->setDefaultCrossingChar( '' ) |
|
820 | + ->setCellRowContentFormat( '%s' ) |
|
821 | 821 | ; |
822 | 822 | |
823 | 823 | $styleGuide = new TableStyle(); |
824 | 824 | $styleGuide |
825 | - ->setHorizontalBorderChars('-') |
|
826 | - ->setVerticalBorderChars(' ') |
|
827 | - ->setDefaultCrossingChar(' ') |
|
828 | - ->setCellHeaderFormat('%s') |
|
825 | + ->setHorizontalBorderChars( '-' ) |
|
826 | + ->setVerticalBorderChars( ' ' ) |
|
827 | + ->setDefaultCrossingChar( ' ' ) |
|
828 | + ->setCellHeaderFormat( '%s' ) |
|
829 | 829 | ; |
830 | 830 | |
831 | - $box = (new TableStyle()) |
|
832 | - ->setHorizontalBorderChars('─') |
|
833 | - ->setVerticalBorderChars('│') |
|
834 | - ->setCrossingChars('┼', '┌', '┬', '┐', '┤', '┘', '┴', '└', '├') |
|
831 | + $box = ( new TableStyle() ) |
|
832 | + ->setHorizontalBorderChars( '─' ) |
|
833 | + ->setVerticalBorderChars( '│' ) |
|
834 | + ->setCrossingChars( '┼', '┌', '┬', '┐', '┤', '┘', '┴', '└', '├' ) |
|
835 | 835 | ; |
836 | 836 | |
837 | - $boxDouble = (new TableStyle()) |
|
838 | - ->setHorizontalBorderChars('═', '─') |
|
839 | - ->setVerticalBorderChars('║', '│') |
|
840 | - ->setCrossingChars('┼', '╔', '╤', '╗', '╢', '╝', '╧', '╚', '╟', '╠', '╪', '╣') |
|
837 | + $boxDouble = ( new TableStyle() ) |
|
838 | + ->setHorizontalBorderChars( '═', '─' ) |
|
839 | + ->setVerticalBorderChars( '║', '│' ) |
|
840 | + ->setCrossingChars( '┼', '╔', '╤', '╗', '╢', '╝', '╧', '╚', '╟', '╠', '╪', '╣' ) |
|
841 | 841 | ; |
842 | 842 | |
843 | 843 | return [ |
@@ -850,16 +850,16 @@ discard block |
||
850 | 850 | ]; |
851 | 851 | } |
852 | 852 | |
853 | - private function resolveStyle($name): TableStyle |
|
853 | + private function resolveStyle( $name ): TableStyle |
|
854 | 854 | { |
855 | - if ($name instanceof TableStyle) { |
|
855 | + if ( $name instanceof TableStyle ) { |
|
856 | 856 | return $name; |
857 | 857 | } |
858 | 858 | |
859 | - if (isset(self::$styles[$name])) { |
|
860 | - return self::$styles[$name]; |
|
859 | + if ( isset( self::$styles[ $name ] ) ) { |
|
860 | + return self::$styles[ $name ]; |
|
861 | 861 | } |
862 | 862 | |
863 | - throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); |
|
863 | + throw new InvalidArgumentException( sprintf( 'Style "%s" is not defined.', $name ) ); |
|
864 | 864 | } |
865 | 865 | } |
@@ -18,8 +18,8 @@ |
||
18 | 18 | */ |
19 | 19 | class TableSeparator extends TableCell |
20 | 20 | { |
21 | - public function __construct(array $options = []) |
|
21 | + public function __construct( array $options = [ ] ) |
|
22 | 22 | { |
23 | - parent::__construct('', $options); |
|
23 | + parent::__construct( '', $options ); |
|
24 | 24 | } |
25 | 25 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | /** |
27 | 27 | * {@inheritdoc} |
28 | 28 | */ |
29 | - public function setInput(InputInterface $input) |
|
29 | + public function setInput( InputInterface $input ) |
|
30 | 30 | { |
31 | 31 | $this->input = $input; |
32 | 32 | } |
@@ -25,20 +25,20 @@ discard block |
||
25 | 25 | 'style' => null, |
26 | 26 | ]; |
27 | 27 | |
28 | - public function __construct(string $value = '', array $options = []) |
|
28 | + public function __construct( string $value = '', array $options = [ ] ) |
|
29 | 29 | { |
30 | 30 | $this->value = $value; |
31 | 31 | |
32 | 32 | // check option names |
33 | - if ($diff = array_diff(array_keys($options), array_keys($this->options))) { |
|
34 | - throw new InvalidArgumentException(sprintf('The TableCell does not support the following options: \'%s\'.', implode('\', \'', $diff))); |
|
33 | + if ( $diff = array_diff( array_keys( $options ), array_keys( $this->options ) ) ) { |
|
34 | + throw new InvalidArgumentException( sprintf( 'The TableCell does not support the following options: \'%s\'.', implode( '\', \'', $diff ) ) ); |
|
35 | 35 | } |
36 | 36 | |
37 | - if (isset($options['style']) && !$options['style'] instanceof TableCellStyle) { |
|
38 | - throw new InvalidArgumentException('The style option must be an instance of "TableCellStyle".'); |
|
37 | + if ( isset( $options[ 'style' ] ) && ! $options[ 'style' ] instanceof TableCellStyle ) { |
|
38 | + throw new InvalidArgumentException( 'The style option must be an instance of "TableCellStyle".' ); |
|
39 | 39 | } |
40 | 40 | |
41 | - $this->options = array_merge($this->options, $options); |
|
41 | + $this->options = array_merge( $this->options, $options ); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | */ |
59 | 59 | public function getColspan() |
60 | 60 | { |
61 | - return (int) $this->options['colspan']; |
|
61 | + return (int)$this->options[ 'colspan' ]; |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -68,11 +68,11 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function getRowspan() |
70 | 70 | { |
71 | - return (int) $this->options['rowspan']; |
|
71 | + return (int)$this->options[ 'rowspan' ]; |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | public function getStyle(): ?TableCellStyle |
75 | 75 | { |
76 | - return $this->options['style']; |
|
76 | + return $this->options[ 'style' ]; |
|
77 | 77 | } |
78 | 78 | } |
@@ -24,27 +24,27 @@ discard block |
||
24 | 24 | /** |
25 | 25 | * @var Helper[] |
26 | 26 | */ |
27 | - private $helpers = []; |
|
27 | + private $helpers = [ ]; |
|
28 | 28 | private $command; |
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @param Helper[] $helpers An array of helper |
32 | 32 | */ |
33 | - public function __construct(array $helpers = []) |
|
33 | + public function __construct( array $helpers = [ ] ) |
|
34 | 34 | { |
35 | - foreach ($helpers as $alias => $helper) { |
|
36 | - $this->set($helper, \is_int($alias) ? null : $alias); |
|
35 | + foreach ( $helpers as $alias => $helper ) { |
|
36 | + $this->set( $helper, \is_int( $alias ) ? null : $alias ); |
|
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | - public function set(HelperInterface $helper, string $alias = null) |
|
40 | + public function set( HelperInterface $helper, string $alias = null ) |
|
41 | 41 | { |
42 | - $this->helpers[$helper->getName()] = $helper; |
|
43 | - if (null !== $alias) { |
|
44 | - $this->helpers[$alias] = $helper; |
|
42 | + $this->helpers[ $helper->getName() ] = $helper; |
|
43 | + if ( null !== $alias ) { |
|
44 | + $this->helpers[ $alias ] = $helper; |
|
45 | 45 | } |
46 | 46 | |
47 | - $helper->setHelperSet($this); |
|
47 | + $helper->setHelperSet( $this ); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
@@ -52,9 +52,9 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return bool true if the helper is defined, false otherwise |
54 | 54 | */ |
55 | - public function has(string $name) |
|
55 | + public function has( string $name ) |
|
56 | 56 | { |
57 | - return isset($this->helpers[$name]); |
|
57 | + return isset( $this->helpers[ $name ] ); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
@@ -64,16 +64,16 @@ discard block |
||
64 | 64 | * |
65 | 65 | * @throws InvalidArgumentException if the helper is not defined |
66 | 66 | */ |
67 | - public function get(string $name) |
|
67 | + public function get( string $name ) |
|
68 | 68 | { |
69 | - if (!$this->has($name)) { |
|
70 | - throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); |
|
69 | + if ( ! $this->has( $name ) ) { |
|
70 | + throw new InvalidArgumentException( sprintf( 'The helper "%s" is not defined.', $name ) ); |
|
71 | 71 | } |
72 | 72 | |
73 | - return $this->helpers[$name]; |
|
73 | + return $this->helpers[ $name ]; |
|
74 | 74 | } |
75 | 75 | |
76 | - public function setCommand(Command $command = null) |
|
76 | + public function setCommand( Command $command = null ) |
|
77 | 77 | { |
78 | 78 | $this->command = $command; |
79 | 79 | } |
@@ -94,6 +94,6 @@ discard block |
||
94 | 94 | #[\ReturnTypeWillChange] |
95 | 95 | public function getIterator() |
96 | 96 | { |
97 | - return new \ArrayIterator($this->helpers); |
|
97 | + return new \ArrayIterator( $this->helpers ); |
|
98 | 98 | } |
99 | 99 | } |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class DebugFormatterHelper extends Helper |
22 | 22 | { |
23 | - private $colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default']; |
|
24 | - private $started = []; |
|
23 | + private $colors = [ 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default' ]; |
|
24 | + private $started = [ ]; |
|
25 | 25 | private $count = -1; |
26 | 26 | |
27 | 27 | /** |
@@ -29,11 +29,11 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @return string |
31 | 31 | */ |
32 | - public function start(string $id, string $message, string $prefix = 'RUN') |
|
32 | + public function start( string $id, string $message, string $prefix = 'RUN' ) |
|
33 | 33 | { |
34 | - $this->started[$id] = ['border' => ++$this->count % \count($this->colors)]; |
|
34 | + $this->started[ $id ] = [ 'border' => ++$this->count % \count( $this->colors ) ]; |
|
35 | 35 | |
36 | - return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message); |
|
36 | + return sprintf( "%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder( $id ), $prefix, $message ); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
@@ -41,32 +41,32 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return string |
43 | 43 | */ |
44 | - public function progress(string $id, string $buffer, bool $error = false, string $prefix = 'OUT', string $errorPrefix = 'ERR') |
|
44 | + public function progress( string $id, string $buffer, bool $error = false, string $prefix = 'OUT', string $errorPrefix = 'ERR' ) |
|
45 | 45 | { |
46 | 46 | $message = ''; |
47 | 47 | |
48 | - if ($error) { |
|
49 | - if (isset($this->started[$id]['out'])) { |
|
48 | + if ( $error ) { |
|
49 | + if ( isset( $this->started[ $id ][ 'out' ] ) ) { |
|
50 | 50 | $message .= "\n"; |
51 | - unset($this->started[$id]['out']); |
|
51 | + unset( $this->started[ $id ][ 'out' ] ); |
|
52 | 52 | } |
53 | - if (!isset($this->started[$id]['err'])) { |
|
54 | - $message .= sprintf('%s<bg=red;fg=white> %s </> ', $this->getBorder($id), $errorPrefix); |
|
55 | - $this->started[$id]['err'] = true; |
|
53 | + if ( ! isset( $this->started[ $id ][ 'err' ] ) ) { |
|
54 | + $message .= sprintf( '%s<bg=red;fg=white> %s </> ', $this->getBorder( $id ), $errorPrefix ); |
|
55 | + $this->started[ $id ][ 'err' ] = true; |
|
56 | 56 | } |
57 | 57 | |
58 | - $message .= str_replace("\n", sprintf("\n%s<bg=red;fg=white> %s </> ", $this->getBorder($id), $errorPrefix), $buffer); |
|
58 | + $message .= str_replace( "\n", sprintf( "\n%s<bg=red;fg=white> %s </> ", $this->getBorder( $id ), $errorPrefix ), $buffer ); |
|
59 | 59 | } else { |
60 | - if (isset($this->started[$id]['err'])) { |
|
60 | + if ( isset( $this->started[ $id ][ 'err' ] ) ) { |
|
61 | 61 | $message .= "\n"; |
62 | - unset($this->started[$id]['err']); |
|
62 | + unset( $this->started[ $id ][ 'err' ] ); |
|
63 | 63 | } |
64 | - if (!isset($this->started[$id]['out'])) { |
|
65 | - $message .= sprintf('%s<bg=green;fg=white> %s </> ', $this->getBorder($id), $prefix); |
|
66 | - $this->started[$id]['out'] = true; |
|
64 | + if ( ! isset( $this->started[ $id ][ 'out' ] ) ) { |
|
65 | + $message .= sprintf( '%s<bg=green;fg=white> %s </> ', $this->getBorder( $id ), $prefix ); |
|
66 | + $this->started[ $id ][ 'out' ] = true; |
|
67 | 67 | } |
68 | 68 | |
69 | - $message .= str_replace("\n", sprintf("\n%s<bg=green;fg=white> %s </> ", $this->getBorder($id), $prefix), $buffer); |
|
69 | + $message .= str_replace( "\n", sprintf( "\n%s<bg=green;fg=white> %s </> ", $this->getBorder( $id ), $prefix ), $buffer ); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | return $message; |
@@ -77,24 +77,24 @@ discard block |
||
77 | 77 | * |
78 | 78 | * @return string |
79 | 79 | */ |
80 | - public function stop(string $id, string $message, bool $successful, string $prefix = 'RES') |
|
80 | + public function stop( string $id, string $message, bool $successful, string $prefix = 'RES' ) |
|
81 | 81 | { |
82 | - $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : ''; |
|
82 | + $trailingEOL = isset( $this->started[ $id ][ 'out' ] ) || isset( $this->started[ $id ][ 'err' ] ) ? "\n" : ''; |
|
83 | 83 | |
84 | - if ($successful) { |
|
85 | - return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message); |
|
84 | + if ( $successful ) { |
|
85 | + return sprintf( "%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder( $id ), $prefix, $message ); |
|
86 | 86 | } |
87 | 87 | |
88 | - $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message); |
|
88 | + $message = sprintf( "%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder( $id ), $prefix, $message ); |
|
89 | 89 | |
90 | - unset($this->started[$id]['out'], $this->started[$id]['err']); |
|
90 | + unset( $this->started[ $id ][ 'out' ], $this->started[ $id ][ 'err' ] ); |
|
91 | 91 | |
92 | 92 | return $message; |
93 | 93 | } |
94 | 94 | |
95 | - private function getBorder(string $id): string |
|
95 | + private function getBorder( string $id ): string |
|
96 | 96 | { |
97 | - return sprintf('<bg=%s> </>', $this->colors[$this->started[$id]['border']]); |
|
97 | + return sprintf( '<bg=%s> </>', $this->colors[ $this->started[ $id ][ 'border' ] ] ); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | /** |
27 | 27 | * {@inheritdoc} |
28 | 28 | */ |
29 | - public function setHelperSet(HelperSet $helperSet = null) |
|
29 | + public function setHelperSet( HelperSet $helperSet = null ) |
|
30 | 30 | { |
31 | 31 | $this->helperSet = $helperSet; |
32 | 32 | } |
@@ -46,49 +46,49 @@ discard block |
||
46 | 46 | * |
47 | 47 | * @return int The length of the string |
48 | 48 | */ |
49 | - public static function strlen(?string $string) |
|
49 | + public static function strlen( ?string $string ) |
|
50 | 50 | { |
51 | - trigger_deprecation('symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::width() or Helper::length() instead.', __METHOD__); |
|
51 | + trigger_deprecation( 'symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::width() or Helper::length() instead.', __METHOD__ ); |
|
52 | 52 | |
53 | - return self::width($string); |
|
53 | + return self::width( $string ); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | 57 | * Returns the width of a string, using mb_strwidth if it is available. |
58 | 58 | * The width is how many characters positions the string will use. |
59 | 59 | */ |
60 | - public static function width(?string $string): int |
|
60 | + public static function width( ?string $string ): int |
|
61 | 61 | { |
62 | 62 | $string ?? $string = ''; |
63 | 63 | |
64 | - if (preg_match('//u', $string)) { |
|
65 | - return (new UnicodeString($string))->width(false); |
|
64 | + if ( preg_match( '//u', $string ) ) { |
|
65 | + return ( new UnicodeString( $string ) )->width( false ); |
|
66 | 66 | } |
67 | 67 | |
68 | - if (false === $encoding = mb_detect_encoding($string, null, true)) { |
|
69 | - return \strlen($string); |
|
68 | + if ( false === $encoding = mb_detect_encoding( $string, null, true ) ) { |
|
69 | + return \strlen( $string ); |
|
70 | 70 | } |
71 | 71 | |
72 | - return mb_strwidth($string, $encoding); |
|
72 | + return mb_strwidth( $string, $encoding ); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |
76 | 76 | * Returns the length of a string, using mb_strlen if it is available. |
77 | 77 | * The length is related to how many bytes the string will use. |
78 | 78 | */ |
79 | - public static function length(?string $string): int |
|
79 | + public static function length( ?string $string ): int |
|
80 | 80 | { |
81 | 81 | $string ?? $string = ''; |
82 | 82 | |
83 | - if (preg_match('//u', $string)) { |
|
84 | - return (new UnicodeString($string))->length(); |
|
83 | + if ( preg_match( '//u', $string ) ) { |
|
84 | + return ( new UnicodeString( $string ) )->length(); |
|
85 | 85 | } |
86 | 86 | |
87 | - if (false === $encoding = mb_detect_encoding($string, null, true)) { |
|
88 | - return \strlen($string); |
|
87 | + if ( false === $encoding = mb_detect_encoding( $string, null, true ) ) { |
|
88 | + return \strlen( $string ); |
|
89 | 89 | } |
90 | 90 | |
91 | - return mb_strlen($string, $encoding); |
|
91 | + return mb_strlen( $string, $encoding ); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -96,82 +96,82 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @return string The string subset |
98 | 98 | */ |
99 | - public static function substr(?string $string, int $from, int $length = null) |
|
99 | + public static function substr( ?string $string, int $from, int $length = null ) |
|
100 | 100 | { |
101 | 101 | $string ?? $string = ''; |
102 | 102 | |
103 | - if (false === $encoding = mb_detect_encoding($string, null, true)) { |
|
104 | - return substr($string, $from, $length); |
|
103 | + if ( false === $encoding = mb_detect_encoding( $string, null, true ) ) { |
|
104 | + return substr( $string, $from, $length ); |
|
105 | 105 | } |
106 | 106 | |
107 | - return mb_substr($string, $from, $length, $encoding); |
|
107 | + return mb_substr( $string, $from, $length, $encoding ); |
|
108 | 108 | } |
109 | 109 | |
110 | - public static function formatTime($secs) |
|
110 | + public static function formatTime( $secs ) |
|
111 | 111 | { |
112 | 112 | static $timeFormats = [ |
113 | - [0, '< 1 sec'], |
|
114 | - [1, '1 sec'], |
|
115 | - [2, 'secs', 1], |
|
116 | - [60, '1 min'], |
|
117 | - [120, 'mins', 60], |
|
118 | - [3600, '1 hr'], |
|
119 | - [7200, 'hrs', 3600], |
|
120 | - [86400, '1 day'], |
|
121 | - [172800, 'days', 86400], |
|
113 | + [ 0, '< 1 sec' ], |
|
114 | + [ 1, '1 sec' ], |
|
115 | + [ 2, 'secs', 1 ], |
|
116 | + [ 60, '1 min' ], |
|
117 | + [ 120, 'mins', 60 ], |
|
118 | + [ 3600, '1 hr' ], |
|
119 | + [ 7200, 'hrs', 3600 ], |
|
120 | + [ 86400, '1 day' ], |
|
121 | + [ 172800, 'days', 86400 ], |
|
122 | 122 | ]; |
123 | 123 | |
124 | - foreach ($timeFormats as $index => $format) { |
|
125 | - if ($secs >= $format[0]) { |
|
126 | - if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0]) |
|
127 | - || $index == \count($timeFormats) - 1 |
|
124 | + foreach ( $timeFormats as $index => $format ) { |
|
125 | + if ( $secs >= $format[ 0 ] ) { |
|
126 | + if ( ( isset( $timeFormats[ $index + 1 ] ) && $secs < $timeFormats[ $index + 1 ][ 0 ] ) |
|
127 | + || $index == \count( $timeFormats ) - 1 |
|
128 | 128 | ) { |
129 | - if (2 == \count($format)) { |
|
130 | - return $format[1]; |
|
129 | + if ( 2 == \count( $format ) ) { |
|
130 | + return $format[ 1 ]; |
|
131 | 131 | } |
132 | 132 | |
133 | - return floor($secs / $format[2]).' '.$format[1]; |
|
133 | + return floor( $secs / $format[ 2 ] ) . ' ' . $format[ 1 ]; |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | } |
137 | 137 | } |
138 | 138 | |
139 | - public static function formatMemory(int $memory) |
|
139 | + public static function formatMemory( int $memory ) |
|
140 | 140 | { |
141 | - if ($memory >= 1024 * 1024 * 1024) { |
|
142 | - return sprintf('%.1f GiB', $memory / 1024 / 1024 / 1024); |
|
141 | + if ( $memory >= 1024 * 1024 * 1024 ) { |
|
142 | + return sprintf( '%.1f GiB', $memory / 1024 / 1024 / 1024 ); |
|
143 | 143 | } |
144 | 144 | |
145 | - if ($memory >= 1024 * 1024) { |
|
146 | - return sprintf('%.1f MiB', $memory / 1024 / 1024); |
|
145 | + if ( $memory >= 1024 * 1024 ) { |
|
146 | + return sprintf( '%.1f MiB', $memory / 1024 / 1024 ); |
|
147 | 147 | } |
148 | 148 | |
149 | - if ($memory >= 1024) { |
|
150 | - return sprintf('%d KiB', $memory / 1024); |
|
149 | + if ( $memory >= 1024 ) { |
|
150 | + return sprintf( '%d KiB', $memory / 1024 ); |
|
151 | 151 | } |
152 | 152 | |
153 | - return sprintf('%d B', $memory); |
|
153 | + return sprintf( '%d B', $memory ); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
157 | 157 | * @deprecated since Symfony 5.3 |
158 | 158 | */ |
159 | - public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) |
|
159 | + public static function strlenWithoutDecoration( OutputFormatterInterface $formatter, ?string $string ) |
|
160 | 160 | { |
161 | - trigger_deprecation('symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::removeDecoration() instead.', __METHOD__); |
|
161 | + trigger_deprecation( 'symfony/console', '5.3', 'Method "%s()" is deprecated and will be removed in Symfony 6.0. Use Helper::removeDecoration() instead.', __METHOD__ ); |
|
162 | 162 | |
163 | - return self::width(self::removeDecoration($formatter, $string)); |
|
163 | + return self::width( self::removeDecoration( $formatter, $string ) ); |
|
164 | 164 | } |
165 | 165 | |
166 | - public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) |
|
166 | + public static function removeDecoration( OutputFormatterInterface $formatter, ?string $string ) |
|
167 | 167 | { |
168 | 168 | $isDecorated = $formatter->isDecorated(); |
169 | - $formatter->setDecorated(false); |
|
169 | + $formatter->setDecorated( false ); |
|
170 | 170 | // remove <...> formatting |
171 | - $string = $formatter->format($string ?? ''); |
|
171 | + $string = $formatter->format( $string ?? '' ); |
|
172 | 172 | // remove already formatted characters |
173 | - $string = preg_replace("/\033\[[^m]*m/", '', $string ?? ''); |
|
174 | - $formatter->setDecorated($isDecorated); |
|
173 | + $string = preg_replace( "/\033\[[^m]*m/", '', $string ?? '' ); |
|
174 | + $formatter->setDecorated( $isDecorated ); |
|
175 | 175 | |
176 | 176 | return $string; |
177 | 177 | } |