diff options
author | Max Semenik <maxsem.wiki@gmail.com> | 2019-11-09 20:59:25 -0800 |
---|---|---|
committer | Jforrester <jforrester@wikimedia.org> | 2019-11-19 15:01:09 +0000 |
commit | b86088857ac541f4e205773706f8e48b914b3727 (patch) | |
tree | d97da8bdbd2085563e9f514b93d3d946f64f3fa5 /includes/password/PasswordFactory.php | |
parent | 68b763d6eda413e2aed5d5d6370a883ac70a7957 (diff) | |
download | mediawikicore-b86088857ac541f4e205773706f8e48b914b3727.tar.gz mediawikicore-b86088857ac541f4e205773706f8e48b914b3727.zip |
Use strict types in includes/password
This is a well isolated area of code, without functions that are
likely to receive PHP's trademark "garbage in, garbage out" data
as parameters. Capitalize on this and require strict types there.
Change-Id: I9f1c172e737018d058ddc1700d8234832b58efa6
Diffstat (limited to 'includes/password/PasswordFactory.php')
-rw-r--r-- | includes/password/PasswordFactory.php | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/includes/password/PasswordFactory.php b/includes/password/PasswordFactory.php index 161782512782..42af8de75411 100644 --- a/includes/password/PasswordFactory.php +++ b/includes/password/PasswordFactory.php @@ -20,6 +20,8 @@ * @file */ +declare( strict_types = 1 ); + /** * Factory class for creating and checking Password objects * @@ -53,7 +55,7 @@ final class PasswordFactory { * @see PasswordFactory::register * @see PasswordFactory::setDefaultType */ - public function __construct( array $config = [], $default = '' ) { + public function __construct( array $config = [], string $default = '' ) { foreach ( $config as $type => $options ) { $this->register( $type, $options ); } @@ -71,7 +73,7 @@ final class PasswordFactory { * @param array $config Array of configuration options. 'class' is required (the Password * subclass name), everything else is passed to the constructor of that class. */ - public function register( $type, array $config ) { + public function register( string $type, array $config ) : void { $config['type'] = $type; $this->types[$type] = $config; } @@ -85,7 +87,7 @@ final class PasswordFactory { * @param string $type Password hash type * @throws InvalidArgumentException If the type is not registered */ - public function setDefaultType( $type ) { + public function setDefaultType( string $type ) : void { if ( !isset( $this->types[$type] ) ) { throw new InvalidArgumentException( "Invalid password type $type." ); } @@ -97,7 +99,7 @@ final class PasswordFactory { * * @return string */ - public function getDefaultType() { + public function getDefaultType() : string { return $this->default; } @@ -108,7 +110,7 @@ final class PasswordFactory { * * @param Config $config Configuration object to load data from */ - public function init( Config $config ) { + public function init( Config $config ) : void { foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) { $this->register( $type, $options ); } @@ -121,7 +123,7 @@ final class PasswordFactory { * * @return array */ - public function getTypes() { + public function getTypes() : array { return $this->types; } @@ -136,7 +138,7 @@ final class PasswordFactory { * @return Password * @throws PasswordError If hash is invalid or type is not recognized */ - public function newFromCiphertext( $hash ) { + public function newFromCiphertext( ?string $hash ) : Password { if ( $hash === null || $hash === false || $hash === '' ) { return new InvalidPassword( $this, [ 'type' => '' ], null ); } elseif ( $hash[0] !== ':' ) { @@ -160,7 +162,7 @@ final class PasswordFactory { * @return Password * @throws PasswordError If hash is invalid or type is not recognized */ - public function newFromType( $type ) { + public function newFromType( string $type ) : Password { if ( !isset( $this->types[$type] ) ) { throw new PasswordError( "Unrecognized password hash type $type." ); } @@ -180,7 +182,7 @@ final class PasswordFactory { * @param Password|null $existing Optional existing hash to get options from * @return Password */ - public function newFromPlaintext( $password, Password $existing = null ) { + public function newFromPlaintext( ?string $password, Password $existing = null ) : Password { if ( $password === null ) { return new InvalidPassword( $this, [ 'type' => '' ], null ); } @@ -207,7 +209,7 @@ final class PasswordFactory { * * @return bool True if needs update, false otherwise */ - public function needsUpdate( Password $password ) { + public function needsUpdate( Password $password ) : bool { if ( $password->getType() !== $this->default ) { return true; } else { @@ -221,7 +223,7 @@ final class PasswordFactory { * @param int $minLength Minimum length of password to generate * @return string */ - public static function generateRandomPasswordString( $minLength = 10 ) { + public static function generateRandomPasswordString( int $minLength = 10 ) : string { // Decide the final password length based on our min password length, // stopping at a minimum of 10 chars. $length = max( 10, $minLength ); @@ -237,7 +239,7 @@ final class PasswordFactory { * * @return InvalidPassword */ - public static function newInvalidPassword() { + public static function newInvalidPassword() : InvalidPassword { static $password = null; if ( $password === null ) { |