Skip to content

Commit

Permalink
Fix OOM-Error in Printer::block (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
webflo authored Dec 14, 2020
1 parent 1f22a41 commit 9548271
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
5 changes: 5 additions & 0 deletions requirement-checker/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
bootstrap="vendor/autoload.php"
colors="true">

<php>
<!-- The width is matching the Travis one to avoid any issues with the CI -->
<env name="COLUMNS" value="80" />
</php>

<testsuites>
<testsuite name="RequirementChecker Test Suite">
<directory>tests/</directory>
Expand Down
23 changes: 13 additions & 10 deletions requirement-checker/src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public function __construct($verbosity, $supportColors, $width = null)
{
if (null === $width) {
$terminal = new Terminal();
$width = min($terminal->getWidth(), 80);
$width = $terminal->getWidth();
}

$this->verbosity = $verbosity;
$this->supportColors = $supportColors;
$this->width = $width;
$this->width = $width ?: 80;
}

/**
Expand Down Expand Up @@ -112,24 +112,27 @@ public function getRequirementErrorMessage(Requirement $requirement)
public function block($title, $message, $verbosity, $style = null)
{
$prefix = ' ['.$title.'] ';
$lineLength = $this->width - strlen($prefix) - 1;
if ($lineLength < 0) {
$lineLength = 0;
}
$message = $prefix.trim($message);

$lines = array();

$remainingMessage = $message;

while ($remainingMessage !== '') {
$wrapped = wordwrap($remainingMessage, $this->width - 3, '¬');
$exploded = explode('¬', $wrapped);
$line = $exploded[0];
$remainingMessage = ltrim(substr($remainingMessage, \strlen($line)));
$wrapped = wordwrap($remainingMessage, $lineLength, '¬');
$wrapped = explode('¬', $wrapped);

if ($remainingMessage !== '') {
$remainingMessage = str_repeat(' ', \strlen($prefix)).$remainingMessage;
do {
$line = array_shift($wrapped);
if ($lines && $lineLength > 0) {
$line = str_repeat(' ', \strlen($prefix)).ltrim($line);
}

$lines[] = str_pad($line, $this->width, ' ', STR_PAD_RIGHT);
}
while (count($wrapped));

$this->printvln('', $verbosity);
$this->printvln(str_repeat(' ', $this->width), $verbosity, $style);
Expand Down
63 changes: 51 additions & 12 deletions requirement-checker/tests/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ public function provideBlocks(): Generator
<<<'EOF'
[OK] This is a block
[OK] This is a
block


EOF
Expand All @@ -242,7 +243,8 @@ public function provideBlocks(): Generator
<<<'EOF'

 
 [OK] This is a block 
 [OK] This is a 
 block 
 

EOF
Expand All @@ -258,7 +260,8 @@ public function provideBlocks(): Generator
<<<'EOF'
[OK] This is a block
[OK] This is a
block


EOF
Expand All @@ -284,12 +287,12 @@ public function provideBlocks(): Generator
<<<'EOF'
[OK] This is a
very long
[OK] This is
a very long
block that
should be
displayed
on 5 lines
displayed on
5 lines


EOF
Expand All @@ -300,19 +303,55 @@ public function provideBlocks(): Generator
true,
20,
'OK',
'This is a very long block that should be displayed on 5 lines',
'This is a very long block that should be displayed on 6 lines',
IO::VERBOSITY_NORMAL,
<<<'EOF'

 
[0m[30;42m [OK] This is a [0m
[0m[30;42m very long [0m
[0m[30;42m [OK] This is [0m
[0m[30;42m a very long [0m
 block that 
 should be 
 displayed 
 on 5 lines 
 displayed on 
 6 lines 
 

EOF
];

yield [
IO::VERBOSITY_NORMAL,
true,
20,
'OK',
'Your system is ready to run the application.',
IO::VERBOSITY_NORMAL,
<<<'EOF'

 
 [OK] Your 
 system is 
 ready to run 
 the 
 application. 
 

EOF
];

yield [
IO::VERBOSITY_NORMAL,
true,
0,
'OK',
'Your system is ready to run the application.',
IO::VERBOSITY_NORMAL,
<<<'EOF'

 
 [OK] Your system is ready to run the application. 
 

EOF
];
}
Expand Down

0 comments on commit 9548271

Please sign in to comment.