Skip to content

Commit

Permalink
Improve container return type annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jul 28, 2022
1 parent 472f4ca commit b169b1e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/private/AppFramework/Utility/SimpleContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCP\AppFramework\QueryException;
use OCP\IContainer;
use Pimple\Container;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionException;
Expand All @@ -53,6 +54,15 @@ public function __construct() {
$this->container = new Container();
}

/**
* @template T
* @param class-string<T>|string $id
* @return T|mixed
* @psalm-template S as class-string<T>|string
* @psalm-param S $id
* @psalm-return (S is class-string<T> ? T : mixed)
* @throws QueryException
*/
public function get(string $id) {
return $this->query($id);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
use OC\Template\JSCombiner;
use OC\User\Listeners\UserChangedListener;
use OC\User\Listeners\UserDeletedListener;
use OC\User\Session;
use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults;
use OCA\Theming\Util;
Expand Down Expand Up @@ -1652,15 +1653,15 @@ public function getUserSession() {
* @deprecated 20.0.0
*/
public function getSession() {
return $this->get(IUserSession::class)->getSession();
return $this->get(Session::class)->getSession();
}

/**
* @param \OCP\ISession $session
*/
public function setSession(\OCP\ISession $session) {
$this->get(SessionStorage::class)->setSession($session);
$this->get(IUserSession::class)->setSession($session);
$this->get(Session::class)->setSession($session);
$this->get(Store::class)->setSession($session);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/private/ServerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Utility\SimpleContainer;
use OCP\AppFramework\QueryException;
use Psr\Container\ContainerExceptionInterface;
use function explode;
use function strtolower;

Expand Down Expand Up @@ -127,6 +128,13 @@ public function has($id, bool $noRecursion = false): bool {
}

/**
* @template T
* @param class-string<T>|string $name
* @return T|mixed
* @psalm-template S as class-string<T>|string
* @psalm-param S $name
* @psalm-return (S is class-string<T> ? T : mixed)
* @throws QueryException
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
*/
public function query(string $name, bool $autoload = true) {
Expand Down
8 changes: 5 additions & 3 deletions lib/public/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
final class Server {
/**
* @template T
* @template S as class-string<T>|string
* @param S $serviceName
* @return (S is class-string<T> ? T : mixed)
* @param class-string<T>|string $serviceName
* @return T|mixed
* @psalm-template S as class-string<T>|string
* @psalm-param S $serviceName
* @psalm-return (S is class-string<T> ? T : mixed)
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @since 25.0.0
Expand Down

0 comments on commit b169b1e

Please sign in to comment.