Use substr and explode instead of a regex

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
This commit is contained in:
Lukas Reschke 2016-10-28 10:34:00 +02:00
parent 067fb18670
commit 89fc4358ba
No known key found for this signature in database
GPG key ID: B9F6980CF6E759B1
2 changed files with 40 additions and 14 deletions

View file

@ -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
)
);
}
}

View file

@ -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');
}
}