Skip to content

Commit b38a197

Browse files
author
igor-chepurnoi
committed
added return types
1 parent 5eec015 commit b38a197

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed

controllers/AssignmentController.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AssignmentController extends Controller
4646
/**
4747
* @inheritdoc
4848
*/
49-
public function init()
49+
public function init(): void
5050
{
5151
parent::init();
5252

@@ -65,7 +65,7 @@ public function init()
6565
/**
6666
* @inheritdoc
6767
*/
68-
public function behaviors()
68+
public function behaviors(): array
6969
{
7070
return [
7171
'verbs' => [
@@ -117,7 +117,7 @@ public function actionIndex()
117117
*
118118
* @return mixed
119119
*/
120-
public function actionView($id)
120+
public function actionView(int $id)
121121
{
122122
$model = $this->findModel($id);
123123

@@ -130,11 +130,11 @@ public function actionView($id)
130130
/**
131131
* Assign items
132132
*
133-
* @param string $id
133+
* @param int $id
134134
*
135135
* @return array
136136
*/
137-
public function actionAssign($id)
137+
public function actionAssign(int $id)
138138
{
139139
$items = Yii::$app->getRequest()->post('items', []);
140140
$assignmentModel = $this->findModel($id);
@@ -146,11 +146,11 @@ public function actionAssign($id)
146146
/**
147147
* Remove items
148148
*
149-
* @param string $id
149+
* @param int $id
150150
*
151151
* @return array
152152
*/
153-
public function actionRemove($id)
153+
public function actionRemove(int $id)
154154
{
155155
$items = Yii::$app->getRequest()->post('items', []);
156156
$assignmentModel = $this->findModel($id);
@@ -169,7 +169,7 @@ public function actionRemove($id)
169169
*
170170
* @throws NotFoundHttpException if the model cannot be found
171171
*/
172-
protected function findModel($id)
172+
protected function findModel(int $id)
173173
{
174174
$class = $this->userIdentityClass;
175175

controllers/RouteController.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RouteController extends Controller
2727
*
2828
* @return array
2929
*/
30-
public function behaviors()
30+
public function behaviors(): array
3131
{
3232
return [
3333
'verbs' => [
@@ -67,7 +67,7 @@ public function actionIndex()
6767
*
6868
* @return array
6969
*/
70-
public function actionAssign()
70+
public function actionAssign(): array
7171
{
7272
$routes = Yii::$app->getRequest()->post('routes', []);
7373
$model = Yii::createObject($this->modelClass);
@@ -81,7 +81,7 @@ public function actionAssign()
8181
*
8282
* @return array
8383
*/
84-
public function actionRemove()
84+
public function actionRemove(): array
8585
{
8686
$routes = Yii::$app->getRequest()->post('routes', []);
8787
$model = Yii::createObject($this->modelClass);
@@ -92,8 +92,10 @@ public function actionRemove()
9292

9393
/**
9494
* Refresh cache of routes
95+
*
96+
* @return array
9597
*/
96-
public function actionRefresh()
98+
public function actionRefresh(): array
9799
{
98100
$model = Yii::createObject($this->modelClass);
99101
$model->invalidate();

controllers/RuleController.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RuleController extends Controller
2828
*
2929
* @return array
3030
*/
31-
public function behaviors()
31+
public function behaviors(): array
3232
{
3333
return [
3434
'verbs' => [
@@ -63,11 +63,11 @@ public function actionIndex()
6363
/**
6464
* Displays a single Rule item.
6565
*
66-
* @param string $id
66+
* @param int $id
6767
*
6868
* @return mixed
6969
*/
70-
public function actionView($id)
70+
public function actionView(int $id)
7171
{
7272
$model = $this->findModel($id);
7373

@@ -99,11 +99,11 @@ public function actionCreate()
9999
*
100100
* If update is successful, the browser will be redirected to the 'view' page.
101101
*
102-
* @param string $id
102+
* @param int $id
103103
*
104104
* @return mixed
105105
*/
106-
public function actionUpdate($id)
106+
public function actionUpdate(int $id)
107107
{
108108
$model = $this->findModel($id);
109109

@@ -121,11 +121,11 @@ public function actionUpdate($id)
121121
*
122122
* If deletion is successful, the browser will be redirected to the 'index' page.
123123
*
124-
* @param string $id
124+
* @param int $id
125125
*
126126
* @return mixed
127127
*/
128-
public function actionDelete($id)
128+
public function actionDelete(int $id)
129129
{
130130
$model = $this->findModel($id);
131131
Yii::$app->authManager->remove($model->item);
@@ -139,19 +139,20 @@ public function actionDelete($id)
139139
*
140140
* If the model is not found, a 404 HTTP exception will be thrown.
141141
*
142-
* @param string $id
142+
* @param int $id
143143
*
144144
* @return BizRuleModel the loaded model
145145
*
146146
* @throws \yii\web\NotFoundHttpException
147147
*/
148-
protected function findModel($id)
148+
protected function findModel(int $id)
149149
{
150150
$item = Yii::$app->authManager->getRule($id);
151-
if ($item) {
151+
152+
if (!empty($item)) {
152153
return new BizRuleModel($item);
153-
} else {
154-
throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.'));
155154
}
155+
156+
throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.'));
156157
}
157158
}

models/RouteModel.php

+21-19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace yii2mod\rbac\models;
44

55
use Yii;
6+
use yii\base\Controller;
7+
use yii\base\Module;
68
use yii\base\Object;
79
use yii\caching\TagDependency;
810
use yii\helpers\VarDumper;
@@ -44,7 +46,7 @@ class RouteModel extends Object
4446
*
4547
* @param array $config
4648
*/
47-
public function __construct($config = [])
49+
public function __construct(array $config = [])
4850
{
4951
$this->cache = Yii::$app->cache;
5052
$this->manager = Yii::$app->authManager;
@@ -59,7 +61,7 @@ public function __construct($config = [])
5961
*
6062
* @return bool
6163
*/
62-
public function addNew($routes): bool
64+
public function addNew(array $routes): bool
6365
{
6466
foreach ($routes as $route) {
6567
$this->manager->add($this->manager->createPermission('/' . trim($route, ' /')));
@@ -77,7 +79,7 @@ public function addNew($routes): bool
7779
*
7880
* @return bool
7981
*/
80-
public function remove($routes): bool
82+
public function remove(array $routes): bool
8183
{
8284
foreach ($routes as $route) {
8385
$item = $this->manager->createPermission('/' . trim($route, '/'));
@@ -115,15 +117,15 @@ public function getRoutes(): array
115117
/**
116118
* Get list of application routes
117119
*
118-
* @param string|null $module
120+
* @param null|string $module
119121
*
120122
* @return array
121123
*/
122-
public function getAppRoutes($module = null): array
124+
public function getAppRoutes(string $module = null): array
123125
{
124126
if ($module === null) {
125127
$module = Yii::$app;
126-
} elseif (is_string($module)) {
128+
} else {
127129
$module = Yii::$app->getModule($module);
128130
}
129131

@@ -156,10 +158,10 @@ public function invalidate(): void
156158
/**
157159
* Get route(s) recursive
158160
*
159-
* @param \yii\base\Module $module
161+
* @param Module $module
160162
* @param array $result
161163
*/
162-
protected function getRouteRecursive($module, &$result): void
164+
protected function getRouteRecursive(Module $module, &$result): void
163165
{
164166
if (!in_array($module->id, $this->excludeModules)) {
165167
$token = "Get Route of '" . get_class($module) . "' with id '" . $module->uniqueId . "'";
@@ -191,14 +193,12 @@ protected function getRouteRecursive($module, &$result): void
191193
/**
192194
* Get list controllers under module
193195
*
194-
* @param \yii\base\Module $module
196+
* @param Module $module
195197
* @param string $namespace
196198
* @param string $prefix
197199
* @param mixed $result
198-
*
199-
* @return mixed
200200
*/
201-
protected function getControllerFiles($module, $namespace, $prefix, &$result)
201+
protected function getControllerFiles(Module $module, string $namespace, string $prefix, &$result): void
202202
{
203203
$path = Yii::getAlias('@' . str_replace('\\', '/', $namespace), false);
204204
$token = "Get controllers from '$path'";
@@ -237,16 +237,16 @@ protected function getControllerFiles($module, $namespace, $prefix, &$result)
237237
*
238238
* @param mixed $type
239239
* @param string $id
240-
* @param \yii\base\Module $module
241-
* @param string $result
240+
* @param Module $module
241+
* @param mixed $result
242242
*/
243-
protected function getControllerActions($type, $id, $module, &$result)
243+
protected function getControllerActions($type, $id, Module $module, &$result): void
244244
{
245-
$token = 'Create controller with cofig=' . VarDumper::dumpAsString($type) . " and id='$id'";
245+
$token = 'Create controller with config=' . VarDumper::dumpAsString($type) . " and id='$id'";
246246
Yii::beginProfile($token, __METHOD__);
247247

248248
try {
249-
/* @var $controller \yii\base\Controller */
249+
/* @var $controller Controller */
250250
$controller = Yii::createObject($type, [$id, $module]);
251251
$this->getActionRoutes($controller, $result);
252252
$all = "/{$controller->uniqueId}/*";
@@ -261,13 +261,14 @@ protected function getControllerActions($type, $id, $module, &$result)
261261
/**
262262
* Get route of action
263263
*
264-
* @param \yii\base\Controller $controller
264+
* @param Controller $controller
265265
* @param array $result all controller action
266266
*/
267-
protected function getActionRoutes($controller, &$result)
267+
protected function getActionRoutes(Controller $controller, &$result): void
268268
{
269269
$token = "Get actions of controller '" . $controller->uniqueId . "'";
270270
Yii::beginProfile($token, __METHOD__);
271+
271272
try {
272273
$prefix = '/' . $controller->uniqueId . '/';
273274
foreach ($controller->actions() as $id => $value) {
@@ -286,6 +287,7 @@ protected function getActionRoutes($controller, &$result)
286287
} catch (\Exception $exc) {
287288
Yii::error($exc->getMessage(), __METHOD__);
288289
}
290+
289291
Yii::endProfile($token, __METHOD__);
290292
}
291293
}

0 commit comments

Comments
 (0)