]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use substr and explode instead of a regex
authorLukas Reschke <lukas@statuscode.ch>
Fri, 28 Oct 2016 08:34:00 +0000 (10:34 +0200)
committerLukas Reschke <lukas@statuscode.ch>
Mon, 31 Oct 2016 16:17:45 +0000 (17:17 +0100)
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
lib/private/App/AppStore/Version/VersionParser.php
tests/lib/App/AppStore/Version/VersionParserTest.php

index 1058a8bc0fa7215e7e06b586f7044431a12c8e95..b548ef386d9ae0ce30a553080c22a6936807a45a 100644 (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
+                       )
+               );
        }
 }
index ccf557eefbc65374e5cb2a99666e4b99443905e4..77c289e547405e839450b241385366f0e9b0c9a6 100644 (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');
+       }
 }