Skip to content
Snippets Groups Projects
Commit 23c754ae authored by Bernhard Posselt's avatar Bernhard Posselt Committed by Roeland Jago Douma
Browse files

prefer scalar type hints over phpdoc annotation

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
parent 9cdc3f05
No related branches found
No related tags found
No related merge requests found
...@@ -60,16 +60,18 @@ class ControllerMethodReflector implements IControllerMethodReflector{ ...@@ -60,16 +60,18 @@ class ControllerMethodReflector implements IControllerMethodReflector{
// extract type parameter information // extract type parameter information
preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches); 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 $this->types = array_combine($matches['var'], $matches['type']);
// two empty arrays
if($matches['var'] === array() && $matches['type'] === array()) {
$this->types = array();
} else {
$this->types = array_combine($matches['var'], $matches['type']);
}
// get method parameters
foreach ($reflection->getParameters() as $param) { 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()) { if($param->isOptional()) {
$default = $param->getDefaultValue(); $default = $param->getDefaultValue();
} else { } else {
...@@ -82,9 +84,9 @@ class ControllerMethodReflector implements IControllerMethodReflector{ ...@@ -82,9 +84,9 @@ class ControllerMethodReflector implements IControllerMethodReflector{
/** /**
* Inspects the PHPDoc parameters for types * 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 * 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 * would return int or null if not existing
*/ */
public function getType($parameter) { public function getType($parameter) {
......
...@@ -104,6 +104,25 @@ class ControllerMethodReflectorTest extends \Test\TestCase { ...@@ -104,6 +104,25 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
$this->assertEquals('int', $reader->getType('test')); $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 * @Annotation
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment