From aa060f5332ecabbd94cc1511c0b254a30cc7beb6 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 21 Feb 2018 14:06:51 +0100 Subject: [PATCH 1/2] Strict OCP\AppFramework\Utility\IControllerMethodReflector Signed-off-by: Roeland Jago Douma --- .../Utility/ControllerMethodReflector.php | 20 +++++++++---------- .../Utility/IControllerMethodReflector.php | 9 +++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php index 7c777c52c12..0f9d406ab73 100644 --- a/lib/private/AppFramework/Utility/ControllerMethodReflector.php +++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php @@ -1,4 +1,5 @@ getDocComment(); @@ -50,7 +51,7 @@ class ControllerMethodReflector implements IControllerMethodReflector { preg_match_all('/^\h+\*\h+@(?P[A-Z]\w+)((?P.*))?$/m', $docs, $matches); foreach($matches['annotation'] as $key => $annontation) { $annotationValue = $matches['parameter'][$key]; - if(isset($annotationValue[0]) && $annotationValue[0] === '(' && $annotationValue[strlen($annotationValue) - 1] === ')') { + if(isset($annotationValue[0]) && $annotationValue[0] === '(' && $annotationValue[\strlen($annotationValue) - 1] === ')') { $cutString = substr($annotationValue, 1, -1); $cutString = str_replace(' ', '', $cutString); $splittedArray = explode(',', $cutString); @@ -78,10 +79,9 @@ class ControllerMethodReflector implements IControllerMethodReflector { } } + $default = null; if($param->isOptional()) { $default = $param->getDefaultValue(); - } else { - $default = null; } $this->parameters[$param->name] = $default; } @@ -94,18 +94,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 +114,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 +125,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 @@ 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; } From 48597758938f474579f9ff1b21884f6bfb7c8c8f Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 21 Feb 2018 20:38:14 +0100 Subject: [PATCH 2/2] Don't try to match on false Signed-off-by: Roeland Jago Douma --- .../Utility/ControllerMethodReflector.php | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php index 0f9d406ab73..ef4a1959d66 100644 --- a/lib/private/AppFramework/Utility/ControllerMethodReflector.php +++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php @@ -47,28 +47,30 @@ class ControllerMethodReflector implements IControllerMethodReflector { $reflection = new \ReflectionMethod($object, $method); $docs = $reflection->getDocComment(); - // extract everything prefixed by @ and first letter uppercase - preg_match_all('/^\h+\*\h+@(?P[A-Z]\w+)((?P.*))?$/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[A-Z]\w+)((?P.*))?$/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\w+)\h+\$(?P\w+)/', $docs, $matches); + $this->types = array_combine($matches['var'], $matches['type']); } - // extract type parameter information - preg_match_all('/@param\h+(?P\w+)\h+\$(?P\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