diff options
author | Danilo Šegan <danilo@segan.org> | 2012-05-31 16:40:00 +0800 |
---|---|---|
committer | Danilo Šegan <danilo@segan.org> | 2012-05-31 16:40:00 +0800 |
commit | 208589a0be985e440a8ed05d927ffd8fb698a5f4 (patch) | |
tree | ad79f93132d90d3f052df0b3284c260d6569924c | |
parent | c2bb2a285df7cb808c84ed7cf0a567105b862618 (diff) |
Refactor parseContinuation.
-rw-r--r-- | licenses/BuildInfo.php | 27 | ||||
-rw-r--r-- | tests/BuildInfoTest.php | 22 |
2 files changed, 40 insertions, 9 deletions
diff --git a/licenses/BuildInfo.php b/licenses/BuildInfo.php index 3a4ef9a..881fa24 100644 --- a/licenses/BuildInfo.php +++ b/licenses/BuildInfo.php @@ -193,6 +193,21 @@ class BuildInfo } } + public function parseContinuation($lines, &$line_no) { + $text = ''; + $total_lines = count($lines); + while ($line_no < $total_lines && + strlen($lines[$line_no]) > 0) { + if ($lines[$line_no][0] == ' ') { + $text .= "\n" . substr($lines[$line_no], 1); + $line_no++; + } else { + break; + } + } + return $text; + } + /** * `data` should be array of lines. */ @@ -213,19 +228,13 @@ class BuildInfo $values = $this->parseLine($line); if (array_key_exists("License-Text", $values)) { $text = $values["License-Text"]; - $total_lines = count($data); - while (($line_no + 1) < $total_lines && - strlen($data[$line_no + 1]) > 0) { - if ($data[$line_no + 1][0] == ' ') { - $text .= "\n" . substr($data[$line_no + 1], 1); - $line_no++; - } - } + $line_no++; + $text .= $this->parseContinuation($data, $line_no); $result["License-Text"] = $text; } else { + $line_no++; $result = array_merge($result, $values); } - $line_no++; } return $result; } diff --git a/tests/BuildInfoTest.php b/tests/BuildInfoTest.php index abe7998..00addc8 100644 --- a/tests/BuildInfoTest.php +++ b/tests/BuildInfoTest.php @@ -112,6 +112,28 @@ class BuildInfoTest extends PHPUnit_Framework_TestCase $values); } + public function test_parseContinuation_no_continuation() { + $buildinfo = new BuildInfo(""); + $lineno = 0; + $this->assertEquals( + "", + $buildinfo->parseContinuation(array("no-space"), $lineno)); + } + + public function test_parseContinuation_indexed() { + $buildinfo = new BuildInfo(""); + $lineno = 0; + $this->assertEquals("", + $buildinfo->parseContinuation(array("no-space", " space"), $lineno)); + } + + public function test_parseContinuation() { + $buildinfo = new BuildInfo(""); + $lineno = 1; + $value = $buildinfo->parseContinuation(array("no-space", " line1", " line2"), $lineno); + $this->assertEquals("\nline1\nline2", $value); + } + /** * @expectedException InvalidArgumentException */ |