Skip to content

Commit

Permalink
Add type hint all classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jul 11, 2023
1 parent 3607c94 commit 922d132
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
3 changes: 3 additions & 0 deletions assets/AppAsset.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
Expand Down
9 changes: 7 additions & 2 deletions commands/HelloController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
Expand All @@ -18,14 +21,16 @@
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class HelloController extends Controller
final class HelloController extends Controller
{
/**
* This command echoes what you have entered as the message.
*
* @param string $message the message to be echoed.
*
* @return int Exit code
*/
public function actionIndex($message = 'hello world')
public function actionIndex(string $message = 'hello world'): int
{
echo $message . "\n";

Expand Down
18 changes: 10 additions & 8 deletions controllers/SiteController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace app\controllers;

use Yii;
Expand All @@ -10,12 +12,12 @@
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
final class SiteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
public function behaviors(): array
{
return [
'access' => [
Expand All @@ -41,7 +43,7 @@ public function behaviors()
/**
* {@inheritdoc}
*/
public function actions()
public function actions(): array
{
return [
'error' => [
Expand All @@ -59,7 +61,7 @@ public function actions()
*
* @return string
*/
public function actionIndex()
public function actionIndex(): string
{
return $this->render('index');
}
Expand All @@ -69,7 +71,7 @@ public function actionIndex()
*
* @return Response|string
*/
public function actionLogin()
public function actionLogin(): Response|string
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
Expand All @@ -91,7 +93,7 @@ public function actionLogin()
*
* @return Response
*/
public function actionLogout()
public function actionLogout(): Response
{
Yii::$app->user->logout();

Expand All @@ -103,7 +105,7 @@ public function actionLogout()
*
* @return Response|string
*/
public function actionContact()
public function actionContact(): Response|string
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Expand All @@ -121,7 +123,7 @@ public function actionContact()
*
* @return string
*/
public function actionAbout()
public function actionAbout(): string
{
return $this->render('about');
}
Expand Down
22 changes: 13 additions & 9 deletions models/ContactForm.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace app\models;

use Yii;
Expand All @@ -8,19 +10,19 @@
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
final class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
public string $name = '';
public string $email = '';
public string $subject = '';
public string $body = '';
public string $verifyCode = '';


/**
* @return array the validation rules.
*/
public function rules()
public function rules(): array
{
return [
// name, email, subject and body are required
Expand All @@ -35,7 +37,7 @@ public function rules()
/**
* @return array customized attribute labels
*/
public function attributeLabels()
public function attributeLabels(): array
{
return [
'verifyCode' => 'Verification Code',
Expand All @@ -44,10 +46,12 @@ public function attributeLabels()

/**
* Sends an email to the specified email address using the information collected by this model.
*
* @param string $email the target email address
*
* @return bool whether the model passes validation
*/
public function contact($email)
public function contact(string $email): bool
{
if ($this->validate()) {
Yii::$app->mailer->compose()
Expand Down
31 changes: 17 additions & 14 deletions models/LoginForm.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace app\models;

use Yii;
Expand All @@ -11,19 +13,19 @@
* @property-read User|null $user
*
*/
class LoginForm extends Model
final class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
public string $username = '';
public string $password = '';
public bool $rememberMe = true;

private $_user = false;
private bool|null|User $user = false;


/**
* @return array the validation rules.
*/
public function rules()
public function rules(): array
{
return [
// username and password are both required
Expand All @@ -40,9 +42,9 @@ public function rules()
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
* @param array|null $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
public function validatePassword(string $attribute, array $params = null): void
{
if (!$this->hasErrors()) {
$user = $this->getUser();
Expand All @@ -55,9 +57,10 @@ public function validatePassword($attribute, $params)

/**
* Logs in a user using the provided username and password.
*
* @return bool whether the user is logged in successfully
*/
public function login()
public function login(): bool
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
Expand All @@ -68,14 +71,14 @@ public function login()
/**
* Finds user by [[username]]
*
* @return User|null
* @return User|null the user identity
*/
public function getUser()
public function getUser(): User|null
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
if ($this->user === false) {
$this->user = User::findByUsername($this->username);
}

return $this->_user;
return $this->user;
}
}
36 changes: 21 additions & 15 deletions models/User.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php

declare(strict_types=1);

namespace app\models;

class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface
use yii\web\IdentityInterface;

final class User extends \yii\base\BaseObject implements IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
public string $id;
public string $username;
public string $password;
public string $authKey;
public string $accessToken;

private static array $users = [
'100' => [
Expand All @@ -31,15 +35,15 @@ class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface
/**
* {@inheritdoc}
*/
public static function findIdentity($id)
public static function findIdentity($id): IdentityInterface|null
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}

/**
* {@inheritdoc}
*/
public static function findIdentityByAccessToken($token, $type = null)
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface|null
{
foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
Expand All @@ -53,10 +57,11 @@ public static function findIdentityByAccessToken($token, $type = null)
/**
* Finds user by username
*
* @param string $username
* @return static|null
* @param string $username the username
*
* @return static|null the user object
*/
public static function findByUsername($username)
public static function findByUsername($username): static|null
{
foreach (self::$users as $user) {
if (strcasecmp((string) $user['username'], $username) === 0) {
Expand All @@ -70,23 +75,23 @@ public static function findByUsername($username)
/**
* {@inheritdoc}
*/
public function getId()
public function getId(): string
{
return $this->id;
}

/**
* {@inheritdoc}
*/
public function getAuthKey()
public function getAuthKey(): string
{
return $this->authKey;
}

/**
* {@inheritdoc}
*/
public function validateAuthKey($authKey)
public function validateAuthKey($authKey): bool
{
return $this->authKey === $authKey;
}
Expand All @@ -95,9 +100,10 @@ public function validateAuthKey($authKey)
* Validates password
*
* @param string $password password to validate
*
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
public function validatePassword(string $password): bool
{
return $this->password === $password;
}
Expand Down
8 changes: 5 additions & 3 deletions widgets/Alert.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace app\widgets;

use Yii;
Expand All @@ -23,15 +25,15 @@
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
*/
class Alert extends \yii\bootstrap5\Widget
final class Alert extends \yii\bootstrap5\Widget
{
/**
* @var array the alert types configuration for the flash messages.
* This array is setup as $key => $value, where:
* - key: the name of the session flash variable
* - value: the bootstrap alert type (i.e. danger, success, info, warning)
*/
public $alertTypes = [
public array $alertTypes = [
'error' => 'alert-danger',
'danger' => 'alert-danger',
'success' => 'alert-success',
Expand All @@ -42,7 +44,7 @@ class Alert extends \yii\bootstrap5\Widget
* @var array the options for rendering the close button tag.
* Array will be passed to [[\yii\bootstrap\Alert::closeButton]].
*/
public $closeButton = [];
public array $closeButton = [];


/**
Expand Down

0 comments on commit 922d132

Please sign in to comment.