diff --git a/lib/SimpleSAML/Error/Assertion.php b/lib/SimpleSAML/Error/Assertion.php index 28f88b5f021ae7180330e5e87c0c28af6181b7f8..bf16f5b32fc4c9715eef53b3446869ccfe31c875 100644 --- a/lib/SimpleSAML/Error/Assertion.php +++ b/lib/SimpleSAML/Error/Assertion.php @@ -30,10 +30,8 @@ class Assertion extends Exception * @param string|null $assertion The assertion which failed, or null if the assert-function was * given an expression. */ - public function __construct($assertion = null) + public function __construct(string $assertion = null) { - Assert::nullOrString($assertion); - $msg = 'Assertion failed: ' . var_export($assertion, true); parent::__construct($msg); @@ -46,7 +44,7 @@ class Assertion extends Exception * * @return string|null The assertion which failed, or null if the assert-function was called with an expression. */ - public function getAssertion() + public function getAssertion(): ?string { return $this->assertion; } @@ -59,9 +57,8 @@ class Assertion extends Exception * disabled. * @return void */ - public static function installHandler() + public static function installHandler(): void { - assert_options(ASSERT_WARNING, 0); assert_options(ASSERT_QUIET_EVAL, 0); assert_options(ASSERT_CALLBACK, [Assertion::class, 'onAssertion']); @@ -78,9 +75,8 @@ class Assertion extends Exception * @param mixed $message The expression which was passed to the assert-function. * @return void */ - public static function onAssertion($file, $line, $message) + public static function onAssertion(string $file, int $line, $message): void { - if (!empty($message)) { $exception = new self($message); } else { diff --git a/lib/SimpleSAML/Error/AuthSource.php b/lib/SimpleSAML/Error/AuthSource.php index fcc3f3c027e4eb9a840559c466ebdb77db48aea5..3e8d589be86794216a4ea7ffbbdb37089a74f103 100644 --- a/lib/SimpleSAML/Error/AuthSource.php +++ b/lib/SimpleSAML/Error/AuthSource.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace SimpleSAML\Error; +use Exception; use Webmozart\Assert\Assert; /** @@ -36,11 +37,8 @@ class AuthSource extends Error * @param string $reason Description of the error. * @param \Exception|null $cause */ - public function __construct($authsource, $reason, $cause = null) + public function __construct(string $authsource, string $reason, Exception $cause = null) { - Assert::string($authsource); - Assert::string($reason); - $this->authsource = $authsource; $this->reason = $reason; parent::__construct( @@ -61,7 +59,7 @@ class AuthSource extends Error * * @return string Authsource module name. */ - public function getAuthSource() + public function getAuthSource(): string { return $this->authsource; } @@ -72,7 +70,7 @@ class AuthSource extends Error * * @return string The reason why the request was invalid. */ - public function getReason() + public function getReason(): string { return $this->reason; } diff --git a/lib/SimpleSAML/Error/BadRequest.php b/lib/SimpleSAML/Error/BadRequest.php index 77463b452bc945259669e802c19df9d713507f03..272cee3570192ff45b510aae7499158f9577caa8 100644 --- a/lib/SimpleSAML/Error/BadRequest.php +++ b/lib/SimpleSAML/Error/BadRequest.php @@ -30,10 +30,8 @@ class BadRequest extends Error * * @param string $reason Description of why the request was unacceptable. */ - public function __construct($reason) + public function __construct(string $reason) { - Assert::string($reason); - $this->reason = $reason; parent::__construct(['BADREQUEST', '%REASON%' => $this->reason]); $this->httpCode = 400; @@ -45,7 +43,7 @@ class BadRequest extends Error * * @return string The reason why the request was invalid. */ - public function getReason() + public function getReason(): string { return $this->reason; } diff --git a/lib/SimpleSAML/Error/ConfigurationError.php b/lib/SimpleSAML/Error/ConfigurationError.php index 1cd98b09e5d6fad7a50c169bb1db0255ad43a505..66032f46b3432a405d861e94562921ab45adf786 100644 --- a/lib/SimpleSAML/Error/ConfigurationError.php +++ b/lib/SimpleSAML/Error/ConfigurationError.php @@ -35,7 +35,7 @@ class ConfigurationError extends Error * @param string|null $file The configuration file that originated this error. * @param array|null $config The configuration array that led to this problem. */ - public function __construct($reason = null, $file = null, array $config = null) + public function __construct(string $reason = null, string $file = null, array $config = null) { $file_str = ''; $reason_str = '.'; @@ -61,7 +61,7 @@ class ConfigurationError extends Error * * @return null|string The reason for this exception. */ - public function getReason() + public function getReason(): ?string { return $this->reason; } @@ -72,7 +72,7 @@ class ConfigurationError extends Error * * @return null|string The configuration file that caused this exception. */ - public function getConfFile() + public function getConfFile(): ?string { return $this->config_file; } diff --git a/lib/SimpleSAML/Error/CriticalConfigurationError.php b/lib/SimpleSAML/Error/CriticalConfigurationError.php index 8ddfd4f9f87fba3bd8997c4da7349150bec88ef8..98293061e4e205a58cc707a24654a113d0942891 100644 --- a/lib/SimpleSAML/Error/CriticalConfigurationError.php +++ b/lib/SimpleSAML/Error/CriticalConfigurationError.php @@ -49,7 +49,7 @@ class CriticalConfigurationError extends ConfigurationError * @param string|null $file The configuration file that originated this error. * @param array|null $config The configuration array that led to this problem. */ - public function __construct($reason = null, $file = null, $config = null) + public function __construct(string $reason = null, string $file = null, array $config = null) { if ($config === null) { $config = self::$minimum_config; @@ -68,9 +68,9 @@ class CriticalConfigurationError extends ConfigurationError /** * @param \Exception $exception * - * @return CriticalConfigurationError + * @return \Exception */ - public static function fromException(\Exception $exception) + public static function fromException(\Exception $exception): \Exception { $reason = null; $file = null; diff --git a/lib/SimpleSAML/Error/Error.php b/lib/SimpleSAML/Error/Error.php index 1f0e87d970857716f4a7eafdd54c0cfa6560cf7d..6dee40ac0547dbaa545dfcb513ed4bd325c94bb8 100644 --- a/lib/SimpleSAML/Error/Error.php +++ b/lib/SimpleSAML/Error/Error.php @@ -79,7 +79,7 @@ class Error extends Exception * @param \Exception $cause The exception which caused this fatal error (if any). Optional. * @param int|null $httpCode The HTTP response code to use. Optional. */ - public function __construct($errorCode, \Exception $cause = null, $httpCode = null) + public function __construct($errorCode, \Exception $cause = null, ?int $httpCode = null) { Assert::true(is_string($errorCode) || is_array($errorCode)); @@ -121,7 +121,7 @@ class Error extends Exception * * @return string The error code. */ - public function getErrorCode() + public function getErrorCode(): string { return $this->errorCode; } @@ -132,7 +132,7 @@ class Error extends Exception * * @return array The parameters. */ - public function getParameters() + public function getParameters(): array { return $this->parameters; } @@ -143,7 +143,7 @@ class Error extends Exception * * @return string The error title tag. */ - public function getDictTitle() + public function getDictTitle(): string { return $this->dictTitle; } @@ -154,7 +154,7 @@ class Error extends Exception * * @return string The error description tag. */ - public function getDictDescr() + public function getDictDescr(): string { return $this->dictDescr; } @@ -166,7 +166,7 @@ class Error extends Exception * This should be overridden by subclasses who want a different return code than 500 Internal Server Error. * @return void */ - protected function setHTTPCode() + protected function setHTTPCode(): void { http_response_code($this->httpCode); } @@ -177,7 +177,7 @@ class Error extends Exception * * @return array The array with the error report data. */ - protected function saveError() + protected function saveError(): array { $data = $this->format(true); $emsg = array_shift($data); @@ -220,7 +220,7 @@ class Error extends Exception * This method displays a standard SimpleSAMLphp error page and exits. * @return void */ - public function show() + public function show(): void { $this->setHTTPCode(); diff --git a/lib/SimpleSAML/Error/ErrorCodes.php b/lib/SimpleSAML/Error/ErrorCodes.php index 4f90de7bfcfab022507f963883a7d4bdef3878fe..e62393dc12cab1f31488b6a4cc0ca6706a94393d 100644 --- a/lib/SimpleSAML/Error/ErrorCodes.php +++ b/lib/SimpleSAML/Error/ErrorCodes.php @@ -20,7 +20,7 @@ class ErrorCodes * * @return array A map from error code to error code title */ - final public static function defaultGetAllErrorCodeTitles() + final public static function defaultGetAllErrorCodeTitles() : array { return [ 'ACSPARAMS' => Translate::noop('{errors:title_ACSPARAMS}'), @@ -67,7 +67,7 @@ class ErrorCodes * * @return array A map from error code to error code title */ - public static function getAllErrorCodeTitles() + public static function getAllErrorCodeTitles(): array { return self::defaultGetAllErrorCodeTitles(); } @@ -78,7 +78,7 @@ class ErrorCodes * * @return array A map from error code to error code description */ - final public static function defaultGetAllErrorCodeDescriptions() + final public static function defaultGetAllErrorCodeDescriptions(): array { return [ 'ACSPARAMS' => Translate::noop('{errors:descr_ACSPARAMS}'), @@ -124,7 +124,7 @@ class ErrorCodes * * @return array A map from error code to error code description */ - public static function getAllErrorCodeDescriptions() + public static function getAllErrorCodeDescriptions(): array { return self::defaultGetAllErrorCodeDescriptions(); } @@ -137,7 +137,7 @@ class ErrorCodes * * @return array An array containing both errorcode maps. */ - public static function getAllErrorCodeMessages() + public static function getAllErrorCodeMessages(): array { return [ 'title' => self::getAllErrorCodeTitles(), @@ -153,7 +153,7 @@ class ErrorCodes * * @return string A string to translate */ - public static function getErrorCodeTitle($errorCode) + public static function getErrorCodeTitle(string $errorCode): string { $errorCodeTitles = self::getAllErrorCodeTitles(); return $errorCodeTitles[$errorCode]; @@ -167,7 +167,7 @@ class ErrorCodes * * @return string A string to translate */ - public static function getErrorCodeDescription($errorCode) + public static function getErrorCodeDescription(string $errorCode): string { $errorCodeDescriptions = self::getAllErrorCodeDescriptions(); return $errorCodeDescriptions[$errorCode]; @@ -183,7 +183,7 @@ class ErrorCodes * * @return array An array containing both errorcode strings. */ - public static function getErrorCodeMessage($errorCode) + public static function getErrorCodeMessage(string $errorCode): array { return [ 'title' => self::getErrorCodeTitle($errorCode), diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php index 885ef1e8db8d8afd9fb113f61a49cd66f636372e..1a2cfc37b5eb6de524ed29ba717193b30bd2c5de 100644 --- a/lib/SimpleSAML/Error/Exception.php +++ b/lib/SimpleSAML/Error/Exception.php @@ -33,7 +33,7 @@ class Exception extends \Exception /** * The cause of this exception. * - * @var Exception|null + * @var \Exception|null */ private $cause = null; @@ -48,11 +48,8 @@ class Exception extends \Exception * @param int $code Error code * @param \Exception|null $cause The cause of this exception. */ - public function __construct($message, $code = 0, \Exception $cause = null) + public function __construct(string $message, int $code = 0, \Exception $cause = null) { - Assert::string($message); - Assert::integer($code); - parent::__construct($message, $code); $this->initBacktrace($this); @@ -68,9 +65,9 @@ class Exception extends \Exception * * @param \Exception $e The exception. * - * @return Exception The new exception. + * @return \Exception The new exception. */ - public static function fromException(\Exception $e) + public static function fromException(\Exception $e): \Exception { if ($e instanceof Exception) { return $e; @@ -85,7 +82,7 @@ class Exception extends \Exception * @param \Exception $exception The exception we should fetch the backtrace from. * @return void */ - protected function initBacktrace(\Exception $exception) + protected function initBacktrace(\Exception $exception): void { $this->backtrace = []; @@ -116,7 +113,7 @@ class Exception extends \Exception * * @return array An array where each function call is a single item. */ - public function getBacktrace() + public function getBacktrace(): array { return $this->backtrace; } @@ -125,9 +122,9 @@ class Exception extends \Exception /** * Retrieve the cause of this exception. * - * @return Exception|null The cause of this exception. + * @return \Exception|null The cause of this exception. */ - public function getCause() + public function getCause(): ?\Exception { return $this->cause; } @@ -138,7 +135,7 @@ class Exception extends \Exception * * @return string The name of the class. */ - public function getClass() + public function getClass(): string { return get_class($this); } @@ -153,7 +150,7 @@ class Exception extends \Exception * * @return array Log lines that should be written out. */ - public function format($anonymize = false) + public function format(bool $anonymize = false): array { $ret = [ $this->getClass() . ': ' . $this->getMessage(), @@ -171,7 +168,7 @@ class Exception extends \Exception * * @return array All lines of the backtrace, properly formatted. */ - public function formatBacktrace($anonymize = false) + public function formatBacktrace(bool $anonymize = false): array { $ret = []; $basedir = Configuration::getInstance()->getBaseDir(); @@ -203,7 +200,7 @@ class Exception extends \Exception * @param int $level * @return void */ - protected function logBacktrace($level = Logger::DEBUG) + protected function logBacktrace(int $level = Logger::DEBUG): void { // see if debugging is enabled for backtraces $debug = Configuration::getInstance()->getArrayize('debug', ['backtraces' => false]); @@ -245,7 +242,7 @@ class Exception extends \Exception * @param int $default_level The log level to use if this method was not overridden. * @return void */ - public function log($default_level) + public function log(int $default_level): void { $fn = [ Logger::ERR => 'logError', @@ -263,7 +260,7 @@ class Exception extends \Exception * This function will write this exception to the log, including a full backtrace. * @return void */ - public function logError() + public function logError(): void { Logger::error($this->getClass() . ': ' . $this->getMessage()); $this->logBacktrace(Logger::ERR); @@ -276,7 +273,7 @@ class Exception extends \Exception * This function will write this exception to the log, including a full backtrace. * @return void */ - public function logWarning() + public function logWarning(): void { Logger::warning($this->getClass() . ': ' . $this->getMessage()); $this->logBacktrace(Logger::WARNING); @@ -289,7 +286,7 @@ class Exception extends \Exception * This function will write this exception to the log, including a full backtrace. * @return void */ - public function logInfo() + public function logInfo(): void { Logger::info($this->getClass() . ': ' . $this->getMessage()); $this->logBacktrace(Logger::INFO); @@ -302,7 +299,7 @@ class Exception extends \Exception * This function will write this exception to the log, including a full backtrace. * @return void */ - public function logDebug() + public function logDebug(): void { Logger::debug($this->getClass() . ': ' . $this->getMessage()); $this->logBacktrace(Logger::DEBUG); @@ -317,7 +314,7 @@ class Exception extends \Exception * * @return array Array with the variables that should be serialized. */ - public function __sleep() + public function __sleep(): array { $ret = array_keys((array) $this); diff --git a/lib/SimpleSAML/Error/MetadataNotFound.php b/lib/SimpleSAML/Error/MetadataNotFound.php index 8ef13f5cde57785f1e5f3b6b350cbad2f876fd87..5065a7568082046868b9e5419feebab9022c9243 100644 --- a/lib/SimpleSAML/Error/MetadataNotFound.php +++ b/lib/SimpleSAML/Error/MetadataNotFound.php @@ -19,10 +19,8 @@ class MetadataNotFound extends Error * * @param string $entityId The entityID we were unable to locate. */ - public function __construct($entityId) + public function __construct(string $entityId) { - Assert::string($entityId); - $this->includeTemplate = 'core:no_metadata.tpl.php'; parent::__construct([ 'METADATANOTFOUND', diff --git a/lib/SimpleSAML/Error/NotFound.php b/lib/SimpleSAML/Error/NotFound.php index 498427eabd503a50feb86c849c372eb1a7eb31c5..f7a37dfc5fbef3996c3addb3892cb5f48ce48665 100644 --- a/lib/SimpleSAML/Error/NotFound.php +++ b/lib/SimpleSAML/Error/NotFound.php @@ -30,10 +30,8 @@ class NotFound extends Error * * @param string $reason Optional description of why the given page could not be found. */ - public function __construct($reason = null) + public function __construct(?string $reason = null) { - Assert::nullOrString($reason); - $url = Utils\HTTP::getSelfURL(); if ($reason === null) { @@ -54,7 +52,7 @@ class NotFound extends Error * * @return string|null The reason why the page could not be found. */ - public function getReason() + public function getReason(): ?string { return $this->reason; } @@ -68,7 +66,7 @@ class NotFound extends Error * * @return array */ - public function format($anonymize = false) + public function format(bool $anonymize = false): array { return [ $this->getClass() . ': ' . $this->getMessage(), diff --git a/lib/SimpleSAML/Error/UnserializableException.php b/lib/SimpleSAML/Error/UnserializableException.php index dcee7b0a55b1a5f9171988cd03324c59ce1039de..1b762c7f68336716e1749174f4fd18e75a6bc0ed 100644 --- a/lib/SimpleSAML/Error/UnserializableException.php +++ b/lib/SimpleSAML/Error/UnserializableException.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace SimpleSAML\Error; +use PDOException; + /** * Class for saving normal exceptions for serialization. * @@ -39,7 +41,7 @@ class UnserializableException extends Exception $msg = $original->getMessage(); $code = $original->getCode(); - if (is_string($code)) { + if ($original instanceof PDOException) { // PDOException uses a string as the code. Filter it out here. $code = -1; } @@ -54,7 +56,7 @@ class UnserializableException extends Exception * * @return string The classname. */ - public function getClass() + public function getClass() : string { return $this->class; } diff --git a/modules/saml/lib/Error.php b/modules/saml/lib/Error.php index 26c51bcb921eb66b2b7c0a414a0b030f0befe961..7bbfce42f866e52fc88cc07bbe3a29b0a96c1315 100644 --- a/modules/saml/lib/Error.php +++ b/modules/saml/lib/Error.php @@ -108,9 +108,9 @@ class Error extends \SimpleSAML\Error\Exception * status codes from an arbitrary exception. * * @param \Exception $exception The original exception. - * @return \SimpleSAML\Module\saml\Error The new exception. + * @return \Exception The new exception. */ - public static function fromException(\Exception $exception) + public static function fromException(\Exception $exception) : \Exception { if ($exception instanceof \SimpleSAML\Module\saml\Error) { // Return the original exception unchanged diff --git a/modules/saml/lib/IdP/SAML2.php b/modules/saml/lib/IdP/SAML2.php index 48403c62bca14966380e82daa02db7483fe24441..da474da24406bdeed92155e013c469cafca6c40c 100644 --- a/modules/saml/lib/IdP/SAML2.php +++ b/modules/saml/lib/IdP/SAML2.php @@ -150,6 +150,7 @@ class SAML2 $idpMetadata = $idp->getConfig(); + /** @var \SimpleSAML\Module\saml\Error $error */ $error = \SimpleSAML\Module\saml\Error::fromException($exception); Logger::warning("Returning error to SP with entity ID '" . var_export($spEntityId, true) . "'.");