Skip to content

Commit

Permalink
add typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Jun 6, 2024
1 parent 9d6bd38 commit 0a1cdfd
Show file tree
Hide file tree
Showing 46 changed files with 335 additions and 323 deletions.
2 changes: 1 addition & 1 deletion features/fakes/FakeIdBrokerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FakeIdBrokerClient
const CORRECT_VALUE = '111111';
const INCORRECT_VALUE = '999999';

const RATE_LIMITED_MFA_ID = '987';
const RATE_LIMITED_MFA_ID = 987;

/**
* Constructor.
Expand Down
58 changes: 29 additions & 29 deletions modules/expirychecker/lib/Auth/Process/ExpiryDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@ class ExpiryDate extends ProcessingFilter
const HAS_SEEN_SPLASH_PAGE = 'has_seen_splash_page';
const SESSION_TYPE = 'expirychecker';

private $warnDaysBefore = 14;
private $originalUrlParam = 'originalurl';
private $passwordChangeUrl = null;
private $accountNameAttr = null;
private $employeeIdAttr = 'employeeNumber';
private $expiryDateAttr = null;
private $dateFormat = 'Y-m-d';
private int $warnDaysBefore = 14;
private string $originalUrlParam = 'originalurl';
private string|null $passwordChangeUrl = null;
private string|null $accountNameAttr = null;
private string $employeeIdAttr = 'employeeNumber';
private string|null $expiryDateAttr = null;
private string $dateFormat = 'Y-m-d';

/** @var LoggerInterface */
protected $logger;
protected LoggerInterface $logger;

/**
* Initialize this filter.
*
* @param array $config Configuration information about this filter.
* @param mixed $reserved For future use.
*/
public function __construct($config, $reserved)
public function __construct(array $config, mixed $reserved)
{
parent::__construct($config, $reserved);

Expand Down Expand Up @@ -77,7 +76,7 @@ public function __construct($config, $reserved)
]);
}

protected function loadValuesFromConfig($config, $attributeRules)
protected function loadValuesFromConfig(array $config, array $attributeRules): void
{
foreach ($attributeRules as $attribute => $rules) {
if (array_key_exists($attribute, $config)) {
Expand All @@ -99,7 +98,7 @@ protected function loadValuesFromConfig($config, $attributeRules)
* @param array $state The state data.
* @return mixed The attribute value, or null if not found.
*/
protected function getAttribute($attributeName, $state)
protected function getAttribute(string $attributeName, array $state): mixed
{
$attributeData = $state['Attributes'][$attributeName] ?? null;

Expand All @@ -118,7 +117,7 @@ protected function getAttribute($attributeName, $state)
* expire.
* @return int The number of days remaining
*/
protected function getDaysLeftBeforeExpiry($expiryTimestamp): int
protected function getDaysLeftBeforeExpiry(int $expiryTimestamp): int
{
$now = time();
$end = $expiryTimestamp;
Expand All @@ -135,7 +134,7 @@ protected function getDaysLeftBeforeExpiry($expiryTimestamp): int
* @return int The expiration timestamp.
* @throws \Exception
*/
protected function getExpiryTimestamp($expiryDateAttr, $state)
protected function getExpiryTimestamp(string $expiryDateAttr, array $state): int
{
$expiryDateString = $this->getAttribute($expiryDateAttr, $state);

Expand All @@ -153,7 +152,7 @@ protected function getExpiryTimestamp($expiryDateAttr, $state)
return $expiryTimestamp;
}

public static function hasSeenSplashPageRecently()
public static function hasSeenSplashPageRecently(): bool
{
$session = Session::getSessionFromRequest();
return (bool)$session->getData(
Expand All @@ -162,7 +161,7 @@ public static function hasSeenSplashPageRecently()
);
}

public static function skipSplashPagesFor($seconds)
public static function skipSplashPagesFor(int $seconds): void
{
$session = Session::getSessionFromRequest();
$session->setData(
Expand All @@ -174,7 +173,7 @@ public static function skipSplashPagesFor($seconds)
$session->save();
}

protected function initLogger($config)
protected function initLogger(array $config): void
{
$loggerClass = $config['loggerClass'] ?? Psr3SamlLogger::class;
$this->logger = new $loggerClass();
Expand All @@ -193,7 +192,7 @@ protected function initLogger($config)
* @param int $timestamp The timestamp to check.
* @return bool
*/
public function isDateInPast(int $timestamp)
public function isDateInPast(int $timestamp): bool
{
return ($timestamp < time());
}
Expand All @@ -205,7 +204,7 @@ public function isDateInPast(int $timestamp)
* will expire.
* @return bool
*/
public function isExpired(int $expiryTimestamp)
public function isExpired(int $expiryTimestamp): bool
{
return $this->isDateInPast($expiryTimestamp);
}
Expand All @@ -219,7 +218,7 @@ public function isExpired(int $expiryTimestamp)
* warn the user.
* @return boolean
*/
public function isTimeToWarn($expiryTimestamp, $warnDaysBefore)
public function isTimeToWarn(int $expiryTimestamp, int $warnDaysBefore): bool
{
$daysLeft = $this->getDaysLeftBeforeExpiry($expiryTimestamp);
return ($daysLeft <= $warnDaysBefore);
Expand All @@ -235,12 +234,13 @@ public function isTimeToWarn($expiryTimestamp, $warnDaysBefore)
* @param int $expiryTimestamp The timestamp when the password will expire.
*/
public function redirect2PasswordChange(
&$state,
$accountName,
$passwordChangeUrl,
$change_pwd_session,
$expiryTimestamp
) {
array &$state,
string $accountName,
string $passwordChangeUrl,
string $change_pwd_session,
int $expiryTimestamp
): void
{
$sessionType = 'expirychecker';
/* Save state and redirect. */
$state['expiresAtTimestamp'] = $expiryTimestamp;
Expand Down Expand Up @@ -302,7 +302,7 @@ public function redirect2PasswordChange(
*
* @param array &$state The current state.
*/
public function process(&$state)
public function process(&$state): void
{
$employeeId = $this->getAttribute($this->employeeIdAttr, $state);

Expand Down Expand Up @@ -351,7 +351,7 @@ public function process(&$state)
* @param string $accountName The name of the user account.
* @param int $expiryTimestamp When the password expired.
*/
public function redirectToExpiredPage(&$state, $accountName, $expiryTimestamp)
public function redirectToExpiredPage(array &$state, string $accountName, int $expiryTimestamp): void
{
assert('is_array($state)');

Expand Down Expand Up @@ -379,7 +379,7 @@ public function redirectToExpiredPage(&$state, $accountName, $expiryTimestamp)
* @param string $accountName The name of the user account.
* @param int $expiryTimestamp When the password will expire.
*/
protected function redirectToWarningPage(&$state, $accountName, $expiryTimestamp)
protected function redirectToWarningPage(array &$state, string $accountName, int $expiryTimestamp): void
{
assert('is_array($state)');

Expand Down
19 changes: 11 additions & 8 deletions modules/expirychecker/lib/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Utilities {
*
* Returns a string with the domain portion of the url (e.g. www.insitehome.org)
*/
public static function getUrlDomain($in_url, $start_marker='//',
$end_marker='/') {
public static function getUrlDomain(string $in_url, string $start_marker='//', string $end_marker='/'): string
{

$sm_len = strlen($start_marker);
$em_len = strlen($end_marker);
Expand All @@ -29,9 +29,10 @@ public static function getUrlDomain($in_url, $start_marker='//',
*
* Returns 1 if the domains of the two urls are the same and 0 otherwise.
*/
public static function haveSameDomain($url1, $start_marker1,
$end_marker1, $url2, $start_marker2='//',
$end_marker2='/') {
public static function haveSameDomain(string $url1, string $start_marker1,
string $end_marker1, string $url2, string $start_marker2='//',
string $end_marker2='/'): int
{
$domain1 = self::getUrlDomain($url1, $start_marker1, $end_marker1);
$domain2 = self::getUrlDomain($url2, $start_marker2, $end_marker2);

Expand All @@ -51,8 +52,9 @@ public static function haveSameDomain($url1, $start_marker1,
* for apex to use. If the domains of the change password url and the
* original url are different, it appends the StateId to the output.
*/
public static function convertOriginalUrl($passwordChangeUrl,
$originalUrlParam, $originalUrl, $stateId ) {
public static function convertOriginalUrl(string $passwordChangeUrl,
string $originalUrlParam, string $originalUrl, string $stateId ): array|string
{
$sameDomain = self::haveSameDomain($passwordChangeUrl,
'//', '/', $originalUrl, '//', '/');
$original = $originalUrlParam . ":" . urlencode($originalUrl);
Expand Down Expand Up @@ -80,7 +82,8 @@ public static function convertOriginalUrl($passwordChangeUrl,
* @param string $relayState
* @return string
**/
public static function getUrlFromRelayState($relayState) {
public static function getUrlFromRelayState(string $relayState): string
{
if (strpos($relayState, "http") === 0) {
return $relayState;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/expirychecker/lib/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Validator
* @param LoggerInterface $logger The logger.
* @throws Exception
*/
public static function validate($value, $rules, $logger, $attribute)
public static function validate(mixed $value, array $rules, LoggerInterface $logger, string $attribute): void
{
foreach ($rules as $rule) {
if ( ! self::isValid($value, $rule, $logger)) {
Expand All @@ -47,7 +47,7 @@ public static function validate($value, $rules, $logger, $attribute)
* @return bool
* @throws InvalidArgumentException
*/
protected static function isValid($value, $rule, $logger)
protected static function isValid(mixed $value, string $rule, LoggerInterface $logger): bool
{
switch ($rule) {
case self::INT:
Expand Down
6 changes: 3 additions & 3 deletions modules/mfa/lib/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Assert
* @param string $className The name of the class in question.
* @throws InvalidArgumentException
*/
public static function classExists(string $className)
public static function classExists(string $className): void
{
if (! class_exists($className)) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -32,7 +32,7 @@ public static function classExists(string $className)
* @param mixed $value The value in question.
* @return string
*/
protected static function describe($value)
protected static function describe(mixed $value): string
{
return is_object($value) ? get_class($value) : var_export($value, true);
}
Expand All @@ -44,7 +44,7 @@ protected static function describe($value)
* @param string $className The name/classpath of the class in question.
* @throws InvalidArgumentException
*/
public static function isInstanceOf($object, string $className)
public static function isInstanceOf(mixed $object, string $className): void
{
if (! ($object instanceof $className)) {
throw new InvalidArgumentException(sprintf(
Expand Down
Loading

0 comments on commit 0a1cdfd

Please sign in to comment.