]> source.dussan.org Git - nextcloud-server.git/commitdiff
prefer scalar type hints over phpdoc annotation
authorBernhard Posselt <dev@bernhard-posselt.com>
Thu, 2 Jul 2015 09:54:17 +0000 (11:54 +0200)
committerRoeland Jago Douma <rullzer@owncloud.com>
Thu, 24 Dec 2015 08:20:26 +0000 (09:20 +0100)
use method exists lookup to be safe and not break on old hhvm versions

add test that checks if type hint is preferred over annotation

lib/private/appframework/utility/controllermethodreflector.php
tests/lib/appframework/utility/ControllerMethodReflectorTest.php

index 63cf5ac24f06e7335d0bf98690731cff306826c7..1118332f9305b55e297187385d03cb5455fee2e9 100644 (file)
@@ -60,16 +60,18 @@ class ControllerMethodReflector implements IControllerMethodReflector{
 
                // extract type parameter information
                preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
-               // this is just a fix for PHP 5.3 (array_combine raises warning if called with
-               // two empty arrays
-               if($matches['var'] === array() && $matches['type'] === array()) {
-                       $this->types = array();
-               } else {
-                       $this->types = array_combine($matches['var'], $matches['type']);
-               }
+               $this->types = array_combine($matches['var'], $matches['type']);
 
-               // get method parameters
                foreach ($reflection->getParameters() as $param) {
+                       // extract type information from PHP 7 scalar types and prefer them
+                       // over phpdoc annotations
+                       if (method_exists($param, 'getType')) {
+                               $type = $param->getType();
+                               if ($type !== null) {
+                                       $this->types[$param->getName()] = (string) $type;
+                               }
+                       }
+
                        if($param->isOptional()) {
                                $default = $param->getDefaultValue();
                        } else {
@@ -82,9 +84,9 @@ class ControllerMethodReflector implements IControllerMethodReflector{
 
        /**
         * Inspects the PHPDoc parameters for types
-        * @param string $parameter the parameter whose type comments should be 
+        * @param string $parameter the parameter whose type comments should be
         * parsed
-        * @return string|null type in the type parameters (@param int $something) 
+        * @return string|null type in the type parameters (@param int $something)
         * would return int or null if not existing
         */
        public function getType($parameter) {
index a584b5481ba523f2ea43c52373e629ad7fe1212b..38bd537e07312ac4eb397692a93b224150dc8d28 100644 (file)
@@ -104,6 +104,25 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
                $this->assertEquals('int', $reader->getType('test'));
        }
 
+       /**
+        * @Annotation
+        * @param int $a
+        * @param int $b
+        * @requires PHP 7
+        */
+       public function testReadTypeIntAnnotationsScalarTypes($a, float $b, int $c, $d){
+               $reader = new ControllerMethodReflector();
+               $reader->reflect(
+                       '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
+                       'testReadTypeIntAnnotationsScalarTypes'
+               );
+
+               $this->assertEquals('int', $reader->getType('a'));
+               $this->assertEquals('float', $reader->getType('b'));
+               $this->assertEquals('int', $reader->getType('c'));
+               $this->assertNull($reader->getType('d'));
+       }
+
 
        /**
         * @Annotation