Merge pull request #8552 from nextcloud/strict_session

Make ISession strict
This commit is contained in:
Morris Jobke 2018-02-27 10:52:55 +01:00 committed by GitHub
commit 8d8b22e294
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 35 deletions

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -55,7 +56,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
*/
public function __construct(ISession $session,
ICrypto $crypto,
$passphrase) {
string $passphrase) {
$this->crypto = $crypto;
$this->session = $session;
$this->passphrase = $passphrase;
@ -92,7 +93,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
* @param string $key
* @param mixed $value
*/
public function set($key, $value) {
public function set(string $key, $value) {
$this->sessionValues[$key] = $value;
$this->isModified = true;
}
@ -103,7 +104,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
* @param string $key
* @return string|null Either the value or null
*/
public function get($key) {
public function get(string $key) {
if(isset($this->sessionValues[$key])) {
return $this->sessionValues[$key];
}
@ -117,7 +118,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
* @param string $key
* @return bool
*/
public function exists($key) {
public function exists(string $key): bool {
return isset($this->sessionValues[$key]);
}
@ -126,7 +127,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
*
* @param string $key
*/
public function remove($key) {
public function remove(string $key) {
$this->isModified = true;
unset($this->sessionValues[$key]);
$this->session->remove(self::encryptedSessionName);
@ -151,7 +152,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
* @param bool $deleteOldSession Whether to delete the old associated session file or not.
* @return void
*/
public function regenerateId($deleteOldSession = true) {
public function regenerateId(bool $deleteOldSession = true) {
$this->session->regenerateId($deleteOldSession);
}
@ -162,7 +163,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId() {
public function getId(): string {
return $this->session->getId();
}
@ -182,7 +183,7 @@ class CryptoSessionData implements \ArrayAccess, ISession {
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset) {
public function offsetExists($offset): bool {
return $this->exists($offset);
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -43,8 +44,8 @@ class Internal extends Session {
* @param string $name
* @throws \Exception
*/
public function __construct($name) {
set_error_handler(array($this, 'trapError'));
public function __construct(string $name) {
set_error_handler([$this, 'trapError']);
$this->invoke('session_name', [$name]);
try {
$this->invoke('session_start');
@ -61,7 +62,7 @@ class Internal extends Session {
* @param string $key
* @param integer $value
*/
public function set($key, $value) {
public function set(string $key, $value) {
$this->validateSession();
$_SESSION[$key] = $value;
}
@ -70,7 +71,7 @@ class Internal extends Session {
* @param string $key
* @return mixed
*/
public function get($key) {
public function get(string $key) {
if (!$this->exists($key)) {
return null;
}
@ -81,14 +82,14 @@ class Internal extends Session {
* @param string $key
* @return bool
*/
public function exists($key) {
public function exists(string $key): bool {
return isset($_SESSION[$key]);
}
/**
* @param string $key
*/
public function remove($key) {
public function remove(string $key) {
if (isset($_SESSION[$key])) {
unset($_SESSION[$key]);
}
@ -112,7 +113,7 @@ class Internal extends Session {
* @param bool $deleteOldSession Whether to delete the old associated session file or not.
* @return void
*/
public function regenerateId($deleteOldSession = true) {
public function regenerateId(bool $deleteOldSession = true) {
try {
@session_regenerate_id($deleteOldSession);
} catch (\Error $e) {
@ -127,7 +128,7 @@ class Internal extends Session {
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId() {
public function getId(): string {
$id = $this->invoke('session_id', [], true);
if ($id === '') {
throw new SessionNotAvailableException();
@ -147,7 +148,7 @@ class Internal extends Session {
* @param string $errorString
* @throws \ErrorException
*/
public function trapError($errorNumber, $errorString) {
public function trapError(int $errorNumber, string $errorString) {
throw new \ErrorException($errorString);
}
@ -167,7 +168,7 @@ class Internal extends Session {
* @throws \ErrorException via trapError
* @return mixed
*/
private function invoke($functionName, array $parameters = [], $silence = false) {
private function invoke(string $functionName, array $parameters = [], bool $silence = false) {
try {
if($silence) {
return @call_user_func_array($functionName, $parameters);

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -40,16 +41,16 @@ use OCP\Session\Exceptions\SessionNotAvailableException;
class Memory extends Session {
protected $data;
public function __construct($name) {
public function __construct(string $name) {
//no need to use $name since all data is already scoped to this instance
$this->data = array();
$this->data = [];
}
/**
* @param string $key
* @param integer $value
*/
public function set($key, $value) {
public function set(string $key, $value) {
$this->validateSession();
$this->data[$key] = $value;
}
@ -58,7 +59,7 @@ class Memory extends Session {
* @param string $key
* @return mixed
*/
public function get($key) {
public function get(string $key) {
if (!$this->exists($key)) {
return null;
}
@ -69,20 +70,20 @@ class Memory extends Session {
* @param string $key
* @return bool
*/
public function exists($key) {
public function exists(string $key): bool {
return isset($this->data[$key]);
}
/**
* @param string $key
*/
public function remove($key) {
public function remove(string $key) {
$this->validateSession();
unset($this->data[$key]);
}
public function clear() {
$this->data = array();
$this->data = [];
}
/**
@ -90,7 +91,7 @@ class Memory extends Session {
*
* @param bool $deleteOldSession
*/
public function regenerateId($deleteOldSession = true) {}
public function regenerateId(bool $deleteOldSession = true) {}
/**
* Wrapper around session_id
@ -99,7 +100,7 @@ class Memory extends Session {
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId() {
public function getId(): string {
throw new SessionNotAvailableException('Memory session does not have an ID');
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -38,13 +39,13 @@ abstract class Session implements \ArrayAccess, ISession {
*
* @param string $name
*/
abstract public function __construct($name);
abstract public function __construct(string $name);
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset) {
public function offsetExists($offset): bool {
return $this->exists($offset);
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -34,6 +35,8 @@
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP;
use OCP\Session\Exceptions\SessionNotAvailableException;
/**
* Interface ISession
*
@ -49,7 +52,7 @@ interface ISession {
* @param mixed $value
* @since 6.0.0
*/
public function set($key, $value);
public function set(string $key, $value);
/**
* Get a value from the session
@ -58,7 +61,7 @@ interface ISession {
* @return mixed should return null if $key does not exist
* @since 6.0.0
*/
public function get($key);
public function get(string $key);
/**
* Check if a named key exists in the session
@ -67,7 +70,7 @@ interface ISession {
* @return bool
* @since 6.0.0
*/
public function exists($key);
public function exists(string $key): bool;
/**
* Remove a $key/$value pair from the session
@ -75,7 +78,7 @@ interface ISession {
* @param string $key
* @since 6.0.0
*/
public function remove($key);
public function remove(string $key);
/**
* Reset and recreate the session
@ -96,7 +99,7 @@ interface ISession {
* @return void
* @since 9.0.0
*/
public function regenerateId($deleteOldSession = true);
public function regenerateId(bool $deleteOldSession = true);
/**
* Wrapper around session_id
@ -105,5 +108,5 @@ interface ISession {
* @throws SessionNotAvailableException
* @since 9.1.0
*/
public function getId();
public function getId(): string;
}