summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2018-02-21 22:56:40 +0100
committerGitHub <noreply@github.com>2018-02-21 22:56:40 +0100
commitfb41a93a95cd7908a22c1a6372ce9955a225a19d (patch)
tree81943207e78335c525e4fcfea6cf760eb028b2c0 /lib
parentf01754a389cf30cbebd391f812e46548dc062cc2 (diff)
parent48597758938f474579f9ff1b21884f6bfb7c8c8f (diff)
downloadnextcloud-server-fb41a93a95cd7908a22c1a6372ce9955a225a19d.tar.gz
nextcloud-server-fb41a93a95cd7908a22c1a6372ce9955a225a19d.zip
Merge pull request #8473 from nextcloud/strict_cmr
Strict OCP\AppFramework\Utility\IControllerMethodReflector
Diffstat (limited to 'lib')
-rw-r--r--lib/private/AppFramework/Utility/ControllerMethodReflector.php54
-rw-r--r--lib/public/AppFramework/Utility/IControllerMethodReflector.php9
2 files changed, 33 insertions, 30 deletions
diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php
index 7c777c52c12..ef4a1959d66 100644
--- a/lib/private/AppFramework/Utility/ControllerMethodReflector.php
+++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php
@@ -1,4 +1,5 @@
<?php
+declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@@ -42,32 +43,34 @@ class ControllerMethodReflector implements IControllerMethodReflector {
* @param object $object an object or classname
* @param string $method the method which we want to inspect
*/
- public function reflect($object, $method){
+ public function reflect($object, string $method){
$reflection = new \ReflectionMethod($object, $method);
$docs = $reflection->getDocComment();
- // extract everything prefixed by @ and first letter uppercase
- preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)((?P<parameter>.*))?$/m', $docs, $matches);
- foreach($matches['annotation'] as $key => $annontation) {
- $annotationValue = $matches['parameter'][$key];
- if(isset($annotationValue[0]) && $annotationValue[0] === '(' && $annotationValue[strlen($annotationValue) - 1] === ')') {
- $cutString = substr($annotationValue, 1, -1);
- $cutString = str_replace(' ', '', $cutString);
- $splittedArray = explode(',', $cutString);
- foreach($splittedArray as $annotationValues) {
- list($key, $value) = explode('=', $annotationValues);
- $this->annotations[$annontation][$key] = $value;
+ if ($docs !== false) {
+ // extract everything prefixed by @ and first letter uppercase
+ preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)((?P<parameter>.*))?$/m', $docs, $matches);
+ foreach ($matches['annotation'] as $key => $annontation) {
+ $annotationValue = $matches['parameter'][$key];
+ if (isset($annotationValue[0]) && $annotationValue[0] === '(' && $annotationValue[\strlen($annotationValue) - 1] === ')') {
+ $cutString = substr($annotationValue, 1, -1);
+ $cutString = str_replace(' ', '', $cutString);
+ $splittedArray = explode(',', $cutString);
+ foreach ($splittedArray as $annotationValues) {
+ list($key, $value) = explode('=', $annotationValues);
+ $this->annotations[$annontation][$key] = $value;
+ }
+ continue;
}
- continue;
+
+ $this->annotations[$annontation] = [$annotationValue];
}
- $this->annotations[$annontation] = [$annotationValue];
+ // extract type parameter information
+ preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
+ $this->types = array_combine($matches['var'], $matches['type']);
}
- // extract type parameter information
- preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
- $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
@@ -78,10 +81,9 @@ class ControllerMethodReflector implements IControllerMethodReflector {
}
}
+ $default = null;
if($param->isOptional()) {
$default = $param->getDefaultValue();
- } else {
- $default = null;
}
$this->parameters[$param->name] = $default;
}
@@ -94,18 +96,18 @@ class ControllerMethodReflector implements IControllerMethodReflector {
* @return string|null type in the type parameters (@param int $something)
* would return int or null if not existing
*/
- public function getType($parameter) {
+ public function getType(string $parameter) {
if(array_key_exists($parameter, $this->types)) {
return $this->types[$parameter];
- } else {
- return null;
}
+
+ return null;
}
/**
* @return array the arguments of the method with key => default value
*/
- public function getParameters() {
+ public function getParameters(): array {
return $this->parameters;
}
@@ -114,7 +116,7 @@ class ControllerMethodReflector implements IControllerMethodReflector {
* @param string $name the name of the annotation
* @return bool true if the annotation is found
*/
- public function hasAnnotation($name) {
+ public function hasAnnotation(string $name): bool {
return array_key_exists($name, $this->annotations);
}
@@ -125,7 +127,7 @@ class ControllerMethodReflector implements IControllerMethodReflector {
* @param string $key the string of the annotation
* @return string
*/
- public function getAnnotationParameter($name, $key) {
+ public function getAnnotationParameter(string $name, string $key): string {
if(isset($this->annotations[$name][$key])) {
return $this->annotations[$name][$key];
}
diff --git a/lib/public/AppFramework/Utility/IControllerMethodReflector.php b/lib/public/AppFramework/Utility/IControllerMethodReflector.php
index e9fb4a5a969..e2074b9fe00 100644
--- a/lib/public/AppFramework/Utility/IControllerMethodReflector.php
+++ b/lib/public/AppFramework/Utility/IControllerMethodReflector.php
@@ -1,4 +1,5 @@
<?php
+declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@@ -40,7 +41,7 @@ interface IControllerMethodReflector {
* @return void
* @since 8.0.0
*/
- public function reflect($object, $method);
+ public function reflect($object, string $method);
/**
* Inspects the PHPDoc parameters for types
@@ -51,13 +52,13 @@ interface IControllerMethodReflector {
* would return int or null if not existing
* @since 8.0.0
*/
- public function getType($parameter);
+ public function getType(string $parameter);
/**
* @return array the arguments of the method with key => default value
* @since 8.0.0
*/
- public function getParameters();
+ public function getParameters(): array;
/**
* Check if a method contains an annotation
@@ -66,6 +67,6 @@ interface IControllerMethodReflector {
* @return bool true if the annotation is found
* @since 8.0.0
*/
- public function hasAnnotation($name);
+ public function hasAnnotation(string $name): bool;
}