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

View File

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

View File

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

View File

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

View File

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