From 89fc4358ba9720976d1ea22d778454cff2a3253c Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Fri, 28 Oct 2016 10:34:00 +0200 Subject: [PATCH] Use substr and explode instead of a regex Signed-off-by: Lukas Reschke --- .../App/AppStore/Version/VersionParser.php | 47 +++++++++++++------ .../AppStore/Version/VersionParserTest.php | 7 +++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/private/App/AppStore/Version/VersionParser.php b/lib/private/App/AppStore/Version/VersionParser.php index 1058a8bc0fa..b548ef386d9 100644 --- a/lib/private/App/AppStore/Version/VersionParser.php +++ b/lib/private/App/AppStore/Version/VersionParser.php @@ -27,6 +27,14 @@ namespace OC\App\AppStore\Version; * @package OC\App\AppStore */ class VersionParser { + /** + * @param string $versionString + * @return bool + */ + private function isValidVersionString($versionString) { + return (bool)preg_match('/^[0-9.]+$/', $versionString); + } + /** * Returns the version for a version string * @@ -42,23 +50,34 @@ class VersionParser { // Count the amount of =, if it is one then it's either maximum or minimum // version. If it is two then it is maximum and minimum. - if (preg_match_all('/(?:>|<)(?:=|)[0-9.]+/', $versionSpec, $matches)) { - switch(count($matches[0])) { - case 1: - if(substr($matches[0][0], 0, 1) === '>') { - return new Version(substr($matches[0][0], 2), ''); - } else { - return new Version('', substr($matches[0][0], 2)); - } + $versionElements = explode(' ', $versionSpec); + $firstVersion = isset($versionElements[0]) ? $versionElements[0] : ''; + $firstVersionNumber = substr($firstVersion, 2); + $secondVersion = isset($versionElements[1]) ? $versionElements[1] : ''; + $secondVersionNumber = substr($secondVersion, 2); + + switch(count($versionElements)) { + case 1: + if(!$this->isValidVersionString($firstVersionNumber)) { break; - case 2: - return new Version(substr($matches[0][0], 2), substr($matches[0][1], 2)); + } + if(substr($firstVersion, 0, 1) === '>') { + return new Version($firstVersionNumber, ''); + } else { + return new Version('', $firstVersionNumber); + } + case 2: + if(!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) { break; - default: - throw new \Exception('Version cannot be parsed'); - } + } + return new Version($firstVersionNumber, $secondVersionNumber); } - throw new \Exception('Version cannot be parsed'); + throw new \Exception( + sprintf( + 'Version cannot be parsed: %s', + $versionSpec + ) + ); } } diff --git a/tests/lib/App/AppStore/Version/VersionParserTest.php b/tests/lib/App/AppStore/Version/VersionParserTest.php index ccf557eefbc..77c289e5474 100644 --- a/tests/lib/App/AppStore/Version/VersionParserTest.php +++ b/tests/lib/App/AppStore/Version/VersionParserTest.php @@ -81,4 +81,11 @@ class VersionParserTest extends TestCase { $this->assertEquals($expected, $this->versionParser->getVersion($input)); } + /** + * @expectedException \Exception + * @expectedExceptionMessage Version cannot be parsed: BogusVersion + */ + public function testGetVersionException() { + $this->versionParser->getVersion('BogusVersion'); + } } -- 2.39.5