From 9813888a4f2efcc79e5bf711e64ce862c9715ee8 Mon Sep 17 00:00:00 2001 From: ReenExe Date: Thu, 28 Jul 2016 19:22:45 +0300 Subject: [PATCH] undefined offset fix (#19406) --- .../Component/Console/Helper/Table.php | 27 ++++++++++++------- .../Console/Tests/Helper/TableTest.php | 19 +++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/Table.php b/src/Symfony/Component/Console/Helper/Table.php index b1111fec65..0d947843a9 100644 --- a/src/Symfony/Component/Console/Helper/Table.php +++ b/src/Symfony/Component/Console/Helper/Table.php @@ -101,11 +101,11 @@ class Table self::$styles = self::initStyles(); } - if (!self::$styles[$name]) { - throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + if (isset(self::$styles[$name])) { + return self::$styles[$name]; } - return self::$styles[$name]; + throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); } /** @@ -117,13 +117,7 @@ class Table */ public function setStyle($name) { - if ($name instanceof TableStyle) { - $this->style = $name; - } elseif (isset(self::$styles[$name])) { - $this->style = self::$styles[$name]; - } else { - throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); - } + $this->style = $this->resolveStyle($name); return $this; } @@ -611,4 +605,17 @@ class Table 'symfony-style-guide' => $styleGuide, ); } + + private function resolveStyle($name) + { + if ($name instanceof TableStyle) { + return $name; + } + + if (isset(self::$styles[$name])) { + return self::$styles[$name]; + } + + throw new \InvalidArgumentException(sprintf('Style "%s" is not defined.', $name)); + } } diff --git a/src/Symfony/Component/Console/Tests/Helper/TableTest.php b/src/Symfony/Component/Console/Tests/Helper/TableTest.php index c933ff6a69..9628bc36fa 100644 --- a/src/Symfony/Component/Console/Tests/Helper/TableTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/TableTest.php @@ -577,6 +577,25 @@ TABLE; $this->assertEquals($table, $table->addRow(new TableSeparator()), 'fluent interface on addRow() with a single TableSeparator() works'); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Style "absent" is not defined. + */ + public function testIsNotDefinedStyleException() + { + $table = new Table($this->getOutputStream()); + $table->setStyle('absent'); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Style "absent" is not defined. + */ + public function testGetStyleDefinition() + { + Table::getStyleDefinition('absent'); + } + protected function getOutputStream() { return new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, false);