Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f74fb0

Browse filesBrowse files
committed
minor symfony#34869 [Console] Improve speed NullOutput (tienvx)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Console] Improve speed NullOutput | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | N/A <!-- required for new features --> This PR improve NullOutput so that test cases that used it can be run faster. Commits ------- 7538743 [Console] Improve speed NullOutput
2 parents b138fbc + 7538743 commit 9f74fb0
Copy full SHA for 9f74fb0

File tree

6 files changed

+275
-2
lines changed
Filter options

6 files changed

+275
-2
lines changed
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Formatter;
13+
14+
/**
15+
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
16+
*/
17+
final class NullOutputFormatter implements OutputFormatterInterface
18+
{
19+
private $style;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function format(?string $message): void
25+
{
26+
// do nothing
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function getStyle(string $name): OutputFormatterStyleInterface
33+
{
34+
if ($this->style) {
35+
return $this->style;
36+
}
37+
// to comply with the interface we must return a OutputFormatterStyleInterface
38+
return $this->style = new NullOutputFormatterStyle();
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function hasStyle(string $name): bool
45+
{
46+
return false;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function isDecorated(): bool
53+
{
54+
return false;
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function setDecorated(bool $decorated): void
61+
{
62+
// do nothing
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function setStyle(string $name, OutputFormatterStyleInterface $style): void
69+
{
70+
// do nothing
71+
}
72+
}
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Console\Formatter;
12+
13+
/**
14+
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
15+
*/
16+
final class NullOutputFormatterStyle implements OutputFormatterStyleInterface
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function apply(string $text): string
22+
{
23+
return $text;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function setBackground(string $color = null): void
30+
{
31+
// do nothing
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function setForeground(string $color = null): void
38+
{
39+
// do nothing
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function setOption(string $option): void
46+
{
47+
// do nothing
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function setOptions(array $options): void
54+
{
55+
// do nothing
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function unsetOption(string $option): void
62+
{
63+
// do nothing
64+
}
65+
}

‎src/Symfony/Component/Console/Output/NullOutput.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Output/NullOutput.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Output;
1313

14-
use Symfony\Component\Console\Formatter\OutputFormatter;
14+
use Symfony\Component\Console\Formatter\NullOutputFormatter;
1515
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
1616

1717
/**
@@ -24,6 +24,8 @@
2424
*/
2525
class NullOutput implements OutputInterface
2626
{
27+
private $formatter;
28+
2729
/**
2830
* {@inheritdoc}
2931
*/
@@ -37,8 +39,11 @@ public function setFormatter(OutputFormatterInterface $formatter)
3739
*/
3840
public function getFormatter()
3941
{
42+
if ($this->formatter) {
43+
return $this->formatter;
44+
}
4045
// to comply with the interface we must return a OutputFormatterInterface
41-
return new OutputFormatter();
46+
return $this->formatter = new NullOutputFormatter();
4247
}
4348

4449
/**
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Output;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Formatter\NullOutputFormatterStyle;
16+
17+
/**
18+
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
19+
*/
20+
class NullOutputFormatterStyleTest extends TestCase
21+
{
22+
public function testApply()
23+
{
24+
$style = new NullOutputFormatterStyle();
25+
26+
$this->assertSame('foo', $style->apply('foo'));
27+
}
28+
29+
public function testSetForeground()
30+
{
31+
$style = new NullOutputFormatterStyle();
32+
$style->setForeground('black');
33+
$this->assertSame('foo', $style->apply('foo'));
34+
}
35+
36+
public function testSetBackground()
37+
{
38+
$style = new NullOutputFormatterStyle();
39+
$style->setBackground('blue');
40+
$this->assertSame('foo', $style->apply('foo'));
41+
}
42+
43+
public function testOptions()
44+
{
45+
$style = new NullOutputFormatterStyle();
46+
47+
$style->setOptions(['reverse', 'conceal']);
48+
$this->assertSame('foo', $style->apply('foo'));
49+
50+
$style->setOption('bold');
51+
$this->assertSame('foo', $style->apply('foo'));
52+
53+
$style->unsetOption('reverse');
54+
$this->assertSame('foo', $style->apply('foo'));
55+
}
56+
}
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Output;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Formatter\NullOutputFormatter;
16+
use Symfony\Component\Console\Formatter\NullOutputFormatterStyle;
17+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18+
19+
/**
20+
* @author Tien Xuan Vo <tien.xuan.vo@gmail.com>
21+
*/
22+
class NullOutputFormatterTest extends TestCase
23+
{
24+
public function testFormat()
25+
{
26+
$formatter = new NullOutputFormatter();
27+
28+
$message = 'this message will not be changed';
29+
$formatter->format($message);
30+
31+
$this->assertSame('this message will not be changed', $message);
32+
}
33+
34+
public function testGetStyle()
35+
{
36+
$formatter = new NullOutputFormatter();
37+
$this->assertInstanceof(NullOutputFormatterStyle::class, $style = $formatter->getStyle('null'));
38+
$this->assertSame($style, $formatter->getStyle('null'));
39+
}
40+
41+
public function testSetStyle()
42+
{
43+
$formatter = new NullOutputFormatter();
44+
$style = new OutputFormatterStyle();
45+
$formatter->setStyle('null', $style);
46+
$this->assertNotSame($style, $formatter->getStyle('null'));
47+
}
48+
49+
public function testHasStyle()
50+
{
51+
$formatter = new NullOutputFormatter();
52+
$this->assertFalse($formatter->hasStyle('null'));
53+
}
54+
55+
public function testIsDecorated()
56+
{
57+
$formatter = new NullOutputFormatter();
58+
$this->assertFalse($formatter->isDecorated());
59+
}
60+
61+
public function testSetDecorated()
62+
{
63+
$formatter = new NullOutputFormatter();
64+
$formatter->setDecorated(true);
65+
$this->assertFalse($formatter->isDecorated());
66+
}
67+
}

‎src/Symfony/Component/Console/Tests/Output/NullOutputTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Output/NullOutputTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\Output;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Formatter\NullOutputFormatter;
1516
use Symfony\Component\Console\Formatter\OutputFormatter;
1617
use Symfony\Component\Console\Output\NullOutput;
1718
use Symfony\Component\Console\Output\Output;
@@ -40,6 +41,13 @@ public function testVerbosity()
4041
$this->assertSame(OutputInterface::VERBOSITY_QUIET, $output->getVerbosity(), '->getVerbosity() always returns VERBOSITY_QUIET for NullOutput');
4142
}
4243

44+
public function testGetFormatter()
45+
{
46+
$output = new NullOutput();
47+
$this->assertInstanceof(NullOutputFormatter::class, $formatter = $output->getFormatter());
48+
$this->assertSame($formatter, $output->getFormatter());
49+
}
50+
4351
public function testSetFormatter()
4452
{
4553
$output = new NullOutput();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.