Merge pull request #17333 from owncloud/php7-scalar-types-controllers
Prefer scalar type hints over phpdoc annotation
This commit is contained in:
commit
eb509bcbe6
|
@ -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
|
|
||||||
// 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']);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get method parameters
|
|
||||||
foreach ($reflection->getParameters() as $param) {
|
|
||||||
if($param->isOptional()) {
|
if($param->isOptional()) {
|
||||||
$default = $param->getDefaultValue();
|
$default = $param->getDefaultValue();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -104,6 +104,29 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
|
||||||
$this->assertEquals('int', $reader->getType('test'));
|
$this->assertEquals('int', $reader->getType('test'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
* @param int $a
|
||||||
|
* @param int $b
|
||||||
|
*/
|
||||||
|
public function arguments3($a, float $b, int $c, $d){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 7
|
||||||
|
*/
|
||||||
|
public function testReadTypeIntAnnotationsScalarTypes(){
|
||||||
|
$reader = new ControllerMethodReflector();
|
||||||
|
$reader->reflect(
|
||||||
|
'\OC\AppFramework\Utility\ControllerMethodReflectorTest',
|
||||||
|
'arguments3'
|
||||||
|
);
|
||||||
|
|
||||||
|
$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
|
||||||
|
|
Loading…
Reference in New Issue