API: code style fixes

This commit is contained in:
Andreas Gohr 2024-01-07 13:32:25 +01:00
parent ebae58f7ac
commit d48c2b252a
16 changed files with 52 additions and 64 deletions

View File

@ -48,7 +48,7 @@ abstract class RemotePlugin extends Plugin
if ($method_name[0] === '_') {
continue;
}
if($method_name === 'getMethods') {
if ($method_name === 'getMethods') {
continue; // skip self, if overridden
}

View File

@ -135,9 +135,7 @@ class Api
// invoke the ApiCall
try {
return $methods[$method]($args);
} catch (\InvalidArgumentException $e) {
throw new RemoteException($e->getMessage(), -32602);
} catch (\ArgumentCountError $e) {
} catch (\InvalidArgumentException | \ArgumentCountError $e) {
throw new RemoteException($e->getMessage(), -32602);
}
}

View File

@ -2,7 +2,6 @@
namespace dokuwiki\Remote;
use dokuwiki\Remote\OpenApiDoc\DocBlockMethod;
use InvalidArgumentException;
use ReflectionException;
@ -76,67 +75,80 @@ class ApiCall
} catch (ReflectionException $e) {
throw new RuntimeException('Failed to parse API method documentation', 0, $e);
}
}
return $this->docs;
}
/**
* Is this a public method?
*
* Public methods can be called without authentication
*
* @return bool
*/
public function isPublic(): bool
public function isPublic()
{
return $this->isPublic;
}
/**
* Set the public flag
*
* @param bool $isPublic
* @return $this
*/
public function setPublic(bool $isPublic = true): self
public function setPublic(bool $isPublic = true)
{
$this->isPublic = $isPublic;
return $this;
}
/**
* Get information about the argument of this call
*
* @return array
*/
public function getArgs(): array
public function getArgs()
{
return $this->getDocs()->getParameters();
}
/**
* Get information about the return value of this call
*
* @return array
*/
public function getReturn(): array
public function getReturn()
{
return $this->getDocs()->getReturn();
}
/**
* Get the summary of this call
*
* @return string
*/
public function getSummary(): string
public function getSummary()
{
return $this->getDocs()->getSummary();
}
/**
* Get the description of this call
*
* @return string
*/
public function getDescription(): string
public function getDescription()
{
return $this->getDocs()->getDescription();
}
/**
* Get the category of this call
*
* @return string
*/
public function getCategory(): string
public function getCategory()
{
return $this->category;
}
@ -155,16 +167,13 @@ class ApiCall
foreach ($this->getDocs()->getParameters() as $arg => $arginfo) {
if (isset($params[$arg])) {
$args[] = $params[$arg];
} elseif ($arginfo['optional'] && array_key_exists('default', $arginfo)) {
$args[] = $arginfo['default'];
} else {
if ($arginfo['optional'] && array_key_exists('default', $arginfo)) {
$args[] = $arginfo['default'];
} else {
throw new InvalidArgumentException("Missing argument $arg");
}
throw new InvalidArgumentException("Missing argument $arg");
}
}
return $args;
}
}

View File

@ -268,7 +268,7 @@ class ApiCore
$opts['hash'] = $hash;
search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
return array_map(fn($item) => new Page(
return array_map(static fn($item) => new Page(
$item['id'],
0, // we're searching current revisions only
$item['mtime'],
@ -720,7 +720,7 @@ class ApiCore
* @throws AccessDeniedException
* @throws RemoteException
*/
public function appendPage($page, $text, $summary='', $isminor=false)
public function appendPage($page, $text, $summary = '', $isminor = false)
{
$currentpage = $this->getPage($page);
if (!is_string($currentpage)) {
@ -766,7 +766,7 @@ class ApiCore
$dir = utf8_encodeFN(str_replace(':', '/', $namespace));
$data = [];
search($data, $conf['mediadir'], 'search_media', $options, $dir);
return array_map(fn($item) => new Media(
return array_map(static fn($item) => new Media(
$item['id'],
0, // we're searching current revisions only
$item['mtime'],

View File

@ -50,10 +50,10 @@ class JsonRpcServer
}
try {
if($body === '') {
if ($body === '') {
$body = file_get_contents('php://input');
}
if($body !== '') {
if ($body !== '') {
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} else {
$data = [];

View File

@ -2,23 +2,14 @@
namespace dokuwiki\Remote\OpenApiDoc;
class ClassResolver
{
/** @var ClassResolver */
private static $instance;
protected $classUses = [];
protected $classDocs = [];
/**
* @internal Use ClassResolver::getInstance() instead
*/
public function __construct()
{
}
/**
* Get a singleton instance
*
@ -50,11 +41,7 @@ class ClassResolver
}
$classinfo = $this->getClassUses($context);
if (isset($classinfo['uses'][$classalias])) {
return $classinfo['uses'][$classalias];
}
return $classinfo['ownNS'] . '\\' . $classalias;
return $classinfo['uses'][$classalias] ?? $classinfo['ownNS'] . '\\' . $classalias;
}
/**
@ -69,9 +56,9 @@ class ClassResolver
public function document($classalias, $context)
{
$class = $this->resolve($classalias, $context);
if(!class_exists($class)) return null;
if (!class_exists($class)) return null;
if(isset($this->classDocs[$class])) {
if (isset($this->classDocs[$class])) {
$reflector = new \ReflectionClass($class);
$this->classDocs[$class] = new DocBlockClass($reflector);
}
@ -196,5 +183,4 @@ class ClassResolver
return $source;
}
}

View File

@ -68,17 +68,21 @@ class DocBlock
}
/**
* Get the first line of the description
*
* @return string
*/
public function getSummary(): string
public function getSummary()
{
return $this->summary;
}
/**
* Get the full description
*
* @return string
*/
public function getDescription(): string
public function getDescription()
{
return $this->description;
}

View File

@ -67,5 +67,4 @@ class DocBlockClass extends DocBlock
return $this->properties;
}
}

View File

@ -7,7 +7,6 @@ use ReflectionMethod;
class DocBlockMethod extends DocBlock
{
/**
* Parse the given docblock
*
@ -22,9 +21,10 @@ class DocBlockMethod extends DocBlock
$this->refineReturn();
}
/** @inheritdoc */
protected function getContext()
{
if($this->reflector instanceof ReflectionFunction) {
if ($this->reflector instanceof ReflectionFunction) {
return null;
}
return parent::getContext();
@ -67,7 +67,7 @@ class DocBlockMethod extends DocBlock
'optional' => $parameter->isOptional(),
'description' => '',
];
if($parameter->isDefaultValueAvailable()) {
if ($parameter->isDefaultValueAvailable()) {
$result[$parameter->getName()]['default'] = $parameter->getDefaultValue();
}
}
@ -106,7 +106,6 @@ class DocBlockMethod extends DocBlock
[$type, $description] = array_map('trim', sexplode(' ', $return, 2, ''));
$result['type'] = new Type($type, $this->getContext());
$result['description'] = $description;
}
$this->tags['return'] = $result;
}

View File

@ -4,7 +4,6 @@ namespace dokuwiki\Remote\OpenApiDoc;
class DocBlockProperty extends DocBlock
{
/** @var Type */
protected $type;

View File

@ -92,9 +92,7 @@ class OpenAPIGenerator
}
}
usort($codes, function ($a, $b) {
return $a['code'] <=> $b['code'];
});
usort($codes, static fn($a, $b) => $a['code'] <=> $b['code']);
return $codes;
}
@ -277,7 +275,7 @@ class OpenAPIGenerator
$description = $info['description'];
if ($info['optional'] && isset($info['default'])) {
$description .= ' [_default: `' . json_encode($info['default']) . '`_]';
$description .= ' [_default: `' . json_encode($info['default'], JSON_THROW_ON_ERROR) . '`_]';
}
$props[$name] = array_merge(
@ -381,5 +379,4 @@ class OpenAPIGenerator
return $schema;
}
}

View File

@ -10,7 +10,6 @@ namespace dokuwiki\Remote\Response;
*/
abstract class ApiResponse
{
/**
* A string representation of this object
*

View File

@ -47,13 +47,13 @@ class Media extends ApiResponse
$isimage = null,
$hash = '',
$author = ''
)
{
) {
$this->id = $id;
$this->file = mediaFN($this->id, $revision);
$this->revision = $revision ?: $mtime ?: filemtime($this->file);
$this->size = $size ?? filesize($this->file);
$this->permission = $perms ?? auth_quickaclcheck($this->id);;
$this->permission = $perms ?? auth_quickaclcheck($this->id);
;
$this->isimage = (bool)($isimage ?? preg_match("/\.(jpe?g|gif|png)$/", $id));
$this->hash = $hash;
$this->author = $author;

View File

@ -48,8 +48,7 @@ class Page extends ApiResponse
$perms = null,
$hash = '',
$author = ''
)
{
) {
$this->id = $id;
$this->file = wikiFN($this->id, $revision);
$this->revision = $revision ?: $mtime ?: @filemtime($this->file);

View File

@ -59,7 +59,7 @@ if ($INPUT->has('spec')) {
$last = 0;
foreach ($apigen->getErrorCodes() as $code) {
// duplicate codes are only shown with debug
if($code['code'] === $last && !$INPUT->has('debug')) continue;
if ($code['code'] === $last && !$INPUT->has('debug')) continue;
$last = $code['code'];
echo '<tr><td>' . $code['code'] . '</td><td>' . hsc($code['message']) . '</td></tr>';
}

View File

@ -13,7 +13,6 @@ use dokuwiki\Remote\RemoteException;
*/
class remote_plugin_usermanager extends RemotePlugin
{
/**
* Create a new user
*