Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IContainer et all typed #11595

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@
use OCP\RichObjectStrings\IValidator;
use OCP\Encryption\IManager;
use OCA\WorkflowEngine\Manager;
use OC\CapabilitiesManager;

class DIContainer extends SimpleContainer implements IAppContainer {

/**
* @var array
*/
private $middleWares = array();
private $middleWares = [];

/** @var ServerContainer */
private $server;
Expand All @@ -84,7 +85,7 @@ class DIContainer extends SimpleContainer implements IAppContainer {
* @param array $urlParams
* @param ServerContainer|null $server
*/
public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
public function __construct($appName, $urlParams = [], ServerContainer $server = null){
parent::__construct();
$this['AppName'] = $appName;
$this['urlParams'] = $urlParams;
Expand All @@ -104,7 +105,7 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
/**
* Core services
*/
$this->registerService(IOutput::class, function($c){
$this->registerService(IOutput::class, function(){
return new Output($this->getServer()->getWebRoot());
});

Expand Down Expand Up @@ -138,7 +139,7 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser

$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);

$this->registerService(IServerContainer::class, function ($c) {
$this->registerService(IServerContainer::class, function () {
return $this->getServer();
});
$this->registerAlias('ServerContainer', IServerContainer::class);
Expand Down Expand Up @@ -166,7 +167,7 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
return $c->getServer()->getThemingDefaults();
});

$this->registerService(IManager::class, function ($c) {
$this->registerService(IManager::class, function () {
return $this->getServer()->getEncryptionManager();
});

Expand All @@ -178,7 +179,7 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
return $c->query(Validator::class);
});

$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
$this->registerService(\OC\Security\IdentityProof\Manager::class, function () {
return new \OC\Security\IdentityProof\Manager(
$this->getServer()->query(\OC\Files\AppData\Factory::class),
$this->getServer()->getCrypto(),
Expand Down Expand Up @@ -339,40 +340,38 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
/**
* @return \OCP\IServerContainer
*/
public function getServer()
{
public function getServer(): IServerContainer {
return $this->server;
}

/**
* @param string $middleWare
* @return boolean|null
*/
public function registerMiddleWare($middleWare) {
public function registerMiddleWare(string $middleWare) {
$this->middleWares[] = $middleWare;
}

/**
* used to return the appname of the set application
* @return string the name of your application
*/
public function getAppName() {
public function getAppName(): string {
return $this->query('AppName');
}

/**
* @deprecated use IUserSession->isLoggedIn()
* @return boolean
*/
public function isLoggedIn() {
public function isLoggedIn(): bool {
return \OC::$server->getUserSession()->isLoggedIn();
}

/**
* @deprecated use IGroupManager->isAdmin($userId)
* @return boolean
*/
public function isAdminUser() {
public function isAdminUser(): bool {
$uid = $this->getUserId();
return \OC_User::isAdminUser($uid);
}
Expand Down Expand Up @@ -413,8 +412,8 @@ public function log($message, $level) {
*
* @param string $serviceName e.g. 'OCA\Files\Capabilities'
*/
public function registerCapability($serviceName) {
$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
public function registerCapability(string $serviceName) {
$this->query(CapabilitiesManager::class)->registerCapability(function() use ($serviceName) {
return $this->query($serviceName);
});
}
Expand All @@ -424,7 +423,7 @@ public function registerCapability($serviceName) {
* @return mixed
* @throws QueryException if the query could not be resolved
*/
public function query($name) {
public function query(string $name) {
try {
return $this->queryNoFallback($name);
} catch (QueryException $firstException) {
Expand All @@ -444,7 +443,7 @@ public function query($name) {
* @return mixed
* @throws QueryException if the query could not be resolved
*/
public function queryNoFallback($name) {
public function queryNoFallback(string $name) {
$name = $this->sanitizeName($name);

if ($this->offsetExists($name)) {
Expand Down
15 changes: 8 additions & 7 deletions lib/private/AppFramework/Utility/SimpleContainer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -90,7 +91,7 @@ private function buildClass(ReflectionClass $class) {
* @return \stdClass
* @throws QueryException if the class could not be found or instantiated
*/
public function resolve($name) {
public function resolve(string $name): \stdClass {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥 💥 💥
TypeError: Return value of OC\AppFramework\Utility\SimpleContainer::resolve() must be an instance of stdClass, instance of OC\Support\CrashReport\Registry returned in

$baseMsg = 'Could not resolve ' . $name . '!';
try {
$class = new ReflectionClass($name);
Expand All @@ -111,7 +112,7 @@ public function resolve($name) {
* @return mixed registered service for the given $name
* @throws QueryException if the query could not be resolved
*/
public function query($name) {
public function query(string $name) {
$name = $this->sanitizeName($name);
if ($this->offsetExists($name)) {
return $this->offsetGet($name);
Expand All @@ -128,7 +129,7 @@ public function query($name) {
* @param string $name
* @param mixed $value
*/
public function registerParameter($name, $value) {
public function registerParameter(string $name, $value) {
$this[$name] = $value;
}

Expand All @@ -141,7 +142,7 @@ public function registerParameter($name, $value) {
* @param Closure $closure the closure to be called on service creation
* @param bool $shared
*/
public function registerService($name, Closure $closure, $shared = true) {
public function registerService(string $name, Closure $closure, bool $shared = true) {
$name = $this->sanitizeName($name);
if (isset($this[$name])) {
unset($this[$name]);
Expand All @@ -160,17 +161,17 @@ public function registerService($name, Closure $closure, $shared = true) {
* @param string $alias the alias that should be registered
* @param string $target the target that should be resolved instead
*/
public function registerAlias($alias, $target) {
public function registerAlias(string $alias, string $target) {
$this->registerService($alias, function (IContainer $container) use ($target) {
return $container->query($target);
}, false);
}

/*
/**
* @param string $name
* @return string
*/
protected function sanitizeName($name) {
protected function sanitizeName(string $name): string {
if (isset($name[0]) && $name[0] === '\\') {
return ltrim($name, '\\');
}
Expand Down
9 changes: 5 additions & 4 deletions lib/private/ServerContainer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -57,7 +58,7 @@ public function __construct() {
* @param string $appName
* @param string $appNamespace
*/
public function registerNamespace($appName, $appNamespace) {
public function registerNamespace(string $appName, string $appNamespace) {
// Cut of OCA\ and lowercase
$appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
$this->namespaces[$appNamespace] = $appName;
Expand All @@ -67,7 +68,7 @@ public function registerNamespace($appName, $appNamespace) {
* @param string $appName
* @param DIContainer $container
*/
public function registerAppContainer($appName, DIContainer $container) {
public function registerAppContainer(string $appName, DIContainer $container) {
$this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
}

Expand All @@ -77,7 +78,7 @@ public function registerAppContainer($appName, DIContainer $container) {
* @return DIContainer
* @throws QueryException
*/
protected function getAppContainer($namespace, $sensitiveNamespace) {
protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
if (isset($this->appContainers[$namespace])) {
return $this->appContainers[$namespace];
}
Expand All @@ -104,7 +105,7 @@ protected function getAppContainer($namespace, $sensitiveNamespace) {
* @return mixed registered service for the given $name
* @throws QueryException if the query could not be resolved
*/
public function query($name) {
public function query(string $name) {
$name = $this->sanitizeName($name);

// In case the service starts with OCA\ we try to find the service in
Expand Down
10 changes: 5 additions & 5 deletions lib/public/AppFramework/IAppContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCP\AppFramework;

use OCP\IContainer;
use OCP\IServerContainer;

/**
* Class IAppContainer
Expand All @@ -42,26 +43,25 @@ interface IAppContainer extends IContainer {
* @return string the name of your application
* @since 6.0.0
*/
public function getAppName();
public function getAppName(): string;

/**
* @return \OCP\IServerContainer
* @since 6.0.0
*/
public function getServer();
public function getServer(): IServerContainer;

/**
* @param string $middleWare
* @return boolean
* @since 6.0.0
*/
public function registerMiddleWare($middleWare);
public function registerMiddleWare(string $middleWare);

/**
* Register a capability
*
* @param string $serviceName e.g. 'OCA\Files\Capabilities'
* @since 8.2.0
*/
public function registerCapability($serviceName);
public function registerCapability(string $serviceName);
}
11 changes: 6 additions & 5 deletions lib/public/IContainer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -55,7 +56,7 @@ interface IContainer {
* @since 8.2.0
* @throws QueryException if the class could not be found or instantiated
*/
public function resolve($name);
public function resolve(string $name): \stdClass;

/**
* Look up a service for a given name in the container.
Expand All @@ -65,7 +66,7 @@ public function resolve($name);
* @throws QueryException if the query could not be resolved
* @since 6.0.0
*/
public function query($name);
public function query(string $name);

/**
* A value is stored in the container with it's corresponding name
Expand All @@ -75,7 +76,7 @@ public function query($name);
* @return void
* @since 6.0.0
*/
public function registerParameter($name, $value);
public function registerParameter(string $name, $value);

/**
* A service is registered in the container where a closure is passed in which will actually
Expand All @@ -90,7 +91,7 @@ public function registerParameter($name, $value);
* @return void
* @since 6.0.0
*/
public function registerService($name, Closure $closure, $shared = true);
public function registerService(string $name, Closure $closure, bool $shared = true);

/**
* Shortcut for returning a service from a service under a different key,
Expand All @@ -100,5 +101,5 @@ public function registerService($name, Closure $closure, $shared = true);
* @param string $target the target that should be resolved instead
* @since 8.2.0
*/
public function registerAlias($alias, $target);
public function registerAlias(string $alias, string $target);
}