Merge pull request #464 from TonisOrmisson/static-code-analyzer

Static code analyzer
This commit is contained in:
Lorenzo Milesi
2022-08-11 09:50:53 +02:00
committed by GitHub
39 changed files with 106 additions and 81 deletions

View File

@ -69,6 +69,9 @@ jobs:
- name: Run tests - name: Run tests
run: XDEBUG_MODE=coverage php vendor/bin/codecept run --coverage --coverage-xml run: XDEBUG_MODE=coverage php vendor/bin/codecept run --coverage --coverage-xml
- name: Run static code analysis
run: vendor/bin/phpstan analyse
- name: Archive failed tests artifacts - test output & log - name: Archive failed tests artifacts - test output & log
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
if: failure() if: failure()

View File

@ -52,6 +52,8 @@
"2amigos/qrcode-library": "Needed if you want to enable 2FA with QR Code generation. Require version ^1.1" "2amigos/qrcode-library": "Needed if you want to enable 2FA with QR Code generation. Require version ^1.1"
}, },
"require-dev": { "require-dev": {
"2amigos/2fa-library": "^2.0",
"2amigos/qrcode-library": "^2.0",
"friendsofphp/php-cs-fixer": "^3", "friendsofphp/php-cs-fixer": "^3",
"php": ">=7.4", "php": ">=7.4",
"yiisoft/yii2-symfonymailer": "~2.0.0", "yiisoft/yii2-symfonymailer": "~2.0.0",
@ -63,7 +65,8 @@
"codeception/module-filesystem": "^1.0", "codeception/module-filesystem": "^1.0",
"codeception/module-yii2": "^1.1", "codeception/module-yii2": "^1.1",
"codeception/module-asserts": "^1.1", "codeception/module-asserts": "^1.1",
"codeception/module-db": "^1.0" "codeception/module-db": "^1.0",
"phpstan/phpstan": "^1.8"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -76,6 +79,9 @@
} }
}, },
"config": { "config": {
"platform": {
"php": "7.4"
},
"preferred-install": { "preferred-install": {
"*": "auto" "*": "auto"
}, },

8
phpstan.neon Normal file
View File

@ -0,0 +1,8 @@
parameters:
level: 1
paths:
- src
excludePaths:
- 'src/resources/i18n/*'
bootstrapFiles:
- stan_autoload.php

View File

@ -19,7 +19,7 @@ use yii\base\Event;
* @property-read Token $token * @property-read Token $token
* @property-read RecoveryForm $form * @property-read RecoveryForm $form
*/ */
class ResetPasswordEvent extends Event final class ResetPasswordEvent extends Event
{ {
const EVENT_BEFORE_TOKEN_VALIDATE = 'beforeTokenValidate'; const EVENT_BEFORE_TOKEN_VALIDATE = 'beforeTokenValidate';
const EVENT_AFTER_TOKEN_VALIDATE = 'afterTokenValidate'; const EVENT_AFTER_TOKEN_VALIDATE = 'afterTokenValidate';

View File

@ -67,6 +67,7 @@ class SocialNetworkAuthenticateService implements ServiceInterface
$event = Yii::createObject(SocialNetworkAuthEvent::class, [$account, $this->client]); $event = Yii::createObject(SocialNetworkAuthEvent::class, [$account, $this->client]);
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE, $event); $this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE, $event);
$result = false;
if ($account->user instanceof User) { if ($account->user instanceof User) {
if ($account->user->getIsBlocked()) { if ($account->user->getIsBlocked()) {
@ -75,12 +76,15 @@ class SocialNetworkAuthenticateService implements ServiceInterface
} else { } else {
Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifespan); Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifespan);
$this->authAction->setSuccessUrl(Yii::$app->getUser()->getReturnUrl()); $this->authAction->setSuccessUrl(Yii::$app->getUser()->getReturnUrl());
$result = true;
} }
} else { } else {
$this->authAction->setSuccessUrl($account->getConnectionUrl()); $this->authAction->setSuccessUrl($account->getConnectionUrl());
$result = true;
} }
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_AUTHENTICATE, $event); $this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_AUTHENTICATE, $event);
return $result;
} }
protected function createAccount() protected function createAccount()

View File

@ -65,5 +65,6 @@ class SwitchIdentityService implements ServiceInterface
/** @var IdentityInterface $user */ /** @var IdentityInterface $user */
Yii::$app->user->switchIdentity($user, $session->timeout); Yii::$app->user->switchIdentity($user, $session->timeout);
$this->controller->trigger(UserEvent::EVENT_AFTER_SWITCH_IDENTITY, $event); $this->controller->trigger(UserEvent::EVENT_AFTER_SWITCH_IDENTITY, $event);
return true;
} }
} }

View File

@ -57,7 +57,7 @@ class UserCreateService implements ServiceInterface
$model->confirmed_at = time(); $model->confirmed_at = time();
$model->password = !empty($model->password) $model->password = !empty($model->password)
? $model->password ? $model->password
: $this->securityHelper->generatePassword(8, $this->getModule('user')->minPasswordRequirements); : $this->securityHelper->generatePassword(8, $this->getModule()->minPasswordRequirements);
/** @var UserEvent $event */ /** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$model]); $event = $this->make(UserEvent::class, [$model]);
@ -76,14 +76,14 @@ class UserCreateService implements ServiceInterface
['email' => $model->email] ['email' => $model->email]
); );
// from web display a flash message (if enabled) // from web display a flash message (if enabled)
if ($this->getModule()->enableFlashMessages === true && is_a(Yii::$app, yii\web\Application::class)) { if ($this->getModule()->enableFlashMessages === true && is_a(Yii::$app, "yii\web\Application")) {
Yii::$app->session->setFlash( Yii::$app->session->setFlash(
'warning', 'warning',
$error_msg $error_msg
); );
} }
// if we're from console add an error to the model in order to return an error message // if we're from console add an error to the model in order to return an error message
if (is_a(Yii::$app, yii\console\Application::class)) { if (is_a(Yii::$app, "yii\console\Application")) {
$model->addError('username', $error_msg); $model->addError('username', $error_msg);
} }
$transaction->rollBack(); $transaction->rollBack();

View File

@ -51,7 +51,7 @@ class UserRegisterService implements ServiceInterface
try { try {
$model->confirmed_at = $this->getModule()->enableEmailConfirmation ? null : time(); $model->confirmed_at = $this->getModule()->enableEmailConfirmation ? null : time();
$model->password = $this->getModule()->generatePasswords $model->password = $this->getModule()->generatePasswords
? $this->securityHelper->generatePassword(8, $this->getModule('user')->minPasswordRequirements) ? $this->securityHelper->generatePassword(8, $this->getModule()->minPasswordRequirements)
: $model->password; : $model->password;
$event = $this->make(UserEvent::class, [$model]); $event = $this->make(UserEvent::class, [$model]);

View File

@ -32,9 +32,12 @@ class AjaxRequestModelValidator implements ValidatorInterface
if ($request->getIsAjax() && $this->model->load($request->post())) { if ($request->getIsAjax() && $this->model->load($request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON; Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->data = ActiveForm::validate($this->model); $result = ActiveForm::validate($this->model);
Yii::$app->response->data = $result;
Yii::$app->response->send(); Yii::$app->response->send();
Yii::$app->end(); Yii::$app->end();
} return $result;
}
return false;
} }
} }

View File

@ -12,8 +12,8 @@
use yii\bootstrap\ActiveForm; use yii\bootstrap\ActiveForm;
use yii\helpers\Html; use yii\helpers\Html;
/* @var yii\web\View $this */ /** @var yii\web\View $this */
/* @var Da\User\Model\User $user */ /** @var Da\User\Model\User $user */
?> ?>

View File

@ -11,9 +11,9 @@
use Da\User\Widget\AssignmentsWidget; use Da\User\Widget\AssignmentsWidget;
/* @var yii\web\View $this */ /** @var yii\web\View $this */
/* @var Da\User\Model\User $user */ /** @var Da\User\Model\User $user */
/* @var string[] $params */ /** @var string[] $params */
?> ?>

View File

@ -9,10 +9,9 @@
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.
*/ */
/** /** @var yii\web\View $this */
* @var yii\web\View /** @var Da\User\Model\User $user */
* @var \Da\User\Model\User $user
*/
?> ?>
<?php $this->beginContent('@Da/User/resources/views/admin/update.php', ['user' => $user]) ?> <?php $this->beginContent('@Da/User/resources/views/admin/update.php', ['user' => $user]) ?>

View File

@ -10,7 +10,7 @@
*/ */
/** /**
* @var yii\widgets\ActiveForm * @var yii\widgets\ActiveForm $form
* @var \Da\User\Model\User $user * @var \Da\User\Model\User $user
*/ */
?> ?>

View File

@ -15,10 +15,10 @@ use yii\web\View;
use yii\widgets\Pjax; use yii\widgets\Pjax;
/** /**
* @var $this yii\web\View * @var yii\web\View $this
* @var $dataProvider yii\data\ActiveDataProvider * @var yii\data\ActiveDataProvider $dataProvider
* @var $searchModel Da\User\Search\UserSearch * @var Da\User\Search\UserSearch $searchModel
* @var $module Da\User\Module * @var Da\User\Module $module
*/ */
$this->title = Yii::t('usuario', 'Manage users'); $this->title = Yii::t('usuario', 'Manage users');

View File

@ -10,8 +10,8 @@
*/ */
/** /**
* @var \yii\web\View * @var \yii\web\View $this
* @var yii\mail\BaseMessage $content * @var string $content
*/ */
?> ?>
<?php $this->beginPage() ?> <?php $this->beginPage() ?>

View File

@ -10,7 +10,9 @@
*/ */
/** /**
* @var string main view render result /**
* @var \yii\web\View $this
* @var string $content
*/ */
?> ?>

View File

@ -10,7 +10,6 @@
*/ */
/** /**
* @var \Da\User\Model\User
* @var \Da\User\Model\Token $token * @var \Da\User\Model\Token $token
*/ */
?> ?>

View File

@ -10,7 +10,7 @@
*/ */
/** /**
* @var \Da\User\Model\Token * @var \Da\User\Model\Token $token
*/ */
?> ?>
<?= Yii::t('usuario', 'Hello') ?>, <?= Yii::t('usuario', 'Hello') ?>,

View File

@ -10,7 +10,6 @@
*/ */
/** /**
* @var \Da\User\Model\User
* @var \Da\User\Model\Token $token * @var \Da\User\Model\Token $token
*/ */
?> ?>

View File

@ -15,9 +15,9 @@ use yii\helpers\Html;
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
/** /**
* @var $this yii\web\View * @var yii\web\View $this
* @var $model Da\User\Model\Permission * @var Da\User\Model\Permission $model
* @var $unassignedItems string[] * @var string[] $unassignedItems
*/ */
?> ?>

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var \Da\User\Model\Permission * @var yii\web\View $this
* @var $this yii\web\View * @var Da\User\Model\Permission $model
* @var $unassignedItems string[] * @var string[] $unassignedItems
*/ */
$this->title = Yii::t('usuario', 'Create new permission'); $this->title = Yii::t('usuario', 'Create new permission');

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var $dataProvider \yii\data\ActiveDataProvider * @var \yii\data\ActiveDataProvider $dataProvider
* @var $this yii\web\View * @var yii\web\View $this
* @var $searchModel \Da\User\Search\PermissionSearch * @var \Da\User\Search\PermissionSearch $searchModel
*/ */
use yii\grid\ActionColumn; use yii\grid\ActionColumn;
use yii\grid\GridView; use yii\grid\GridView;

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var \Da\User\Model\Permission * @var yii\web\View $this
* @var $this yii\web\View * @var Da\User\Model\Permission $model
* @var $unassignedItems string[] * @var string[] $unassignedItems
*/ */
$this->title = Yii::t('usuario', 'Update permission'); $this->title = Yii::t('usuario', 'Update permission');
@ -31,4 +31,3 @@ $this->params['breadcrumbs'][] = $this->title;
) ?> ) ?>
<?php $this->endContent() ?> <?php $this->endContent() ?>

View File

@ -10,8 +10,8 @@
*/ */
/** /**
* @var $this yii\web\View * @var yii\web\View $this
* @var $model \Da\User\Model\Role * @var \Da\User\Model\Role $model
*/ */
use Da\User\Helper\AuthHelper; use Da\User\Helper\AuthHelper;

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var \Da\User\Model\Role * @var yii\web\View $this
* @var $this yii\web\View * @var \Da\User\Model\Role $model
* @var $unassignedItems string[] * @var string[] $unassignedItems
*/ */
$this->title = Yii::t('usuario', 'Create new role'); $this->title = Yii::t('usuario', 'Create new role');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;

View File

@ -14,9 +14,9 @@ use yii\grid\GridView;
use yii\helpers\Url; use yii\helpers\Url;
/** /**
* @var $dataProvider array * @var \yii\data\DataProviderInterface $dataProvider
* @var $searchModel \Da\User\Search\RoleSearch * @var \Da\User\Search\RoleSearch $searchModel
* @var $this yii\web\View * @var yii\web\View $this
*/ */
$this->title = Yii::t('usuario', 'Roles'); $this->title = Yii::t('usuario', 'Roles');

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var yii\web\View $this
* @var \Da\User\Model\Role $model * @var \Da\User\Model\Role $model
* @var $this yii\web\View * @var string[] $unassignedItems
* @var $unassignedItems string[]
*/ */
$this->title = Yii::t('usuario', 'Update role'); $this->title = Yii::t('usuario', 'Update role');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;

View File

@ -1,8 +1,8 @@
<?php <?php
/** /**
* @var $this yii\web\View * @var yii\web\View $this
* @var $model \Da\User\Model\Rule * @var \Da\User\Model\Rule $model
*/ */
use yii\helpers\Html; use yii\helpers\Html;

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var yii\web\View $this
* @var \Da\User\Model\Rule $model * @var \Da\User\Model\Rule $model
* @var $this yii\web\View * @var string[] $unassignedItems
* @var $unassignedItems string[]
*/ */
$this->title = Yii::t('usuario', 'Create new rule'); $this->title = Yii::t('usuario', 'Create new rule');
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;

View File

@ -6,9 +6,9 @@ use yii\helpers\Url;
use yii\rbac\Rule; use yii\rbac\Rule;
/** /**
* @var $dataProvider \yii\data\ActiveDataProvider * @var \yii\data\ActiveDataProvider $dataProvider
* @var $searchModel \Da\User\Search\RuleSearch * @var \Da\User\Search\RuleSearch $searchModel
* @var $this yii\web\View * @var yii\web\View $this
*/ */
$this->title = Yii::t('usuario', 'Rules'); $this->title = Yii::t('usuario', 'Rules');

View File

@ -10,9 +10,9 @@
*/ */
/** /**
* @var yii\web\View $this
* @var \Da\User\Model\Rule $model * @var \Da\User\Model\Rule $model
* @var $this yii\web\View * @var string[] $unassignedItems
* @var $unassignedItems string[]
*/ */
$this->title = Yii::t('usuario', 'Update rule'); $this->title = Yii::t('usuario', 'Update rule');
$this->params['breadcrumbs'][] = ['label' => Yii::t('usuario', 'Rules'), 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => Yii::t('usuario', 'Rules'), 'url' => ['index']];

View File

@ -11,7 +11,7 @@
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
use yii\helpers\Html; use yii\helpers\Html;
/* @var $model \Da\User\Form\GdprDeleteForm */ /** @var \Da\User\Form\GdprDeleteForm $model */
?> ?>
<div class="row"> <div class="row">

View File

@ -2,7 +2,8 @@
use yii\helpers\Html; use yii\helpers\Html;
/* @var $module \Da\User\Module */ /** @var \yii\web\View $this */
/** @var \Da\User\Module $module */
$this->title = Yii::t('usuario', 'Privacy settings'); $this->title = Yii::t('usuario', 'Privacy settings');

View File

@ -11,9 +11,8 @@
use yii\bootstrap\Alert; use yii\bootstrap\Alert;
/** /** @var \Da\User\Module $module */
* @var $module Da\User\Module
*/
?> ?>
<?php if ($module->enableFlashMessages): ?> <?php if ($module->enableFlashMessages): ?>

View File

@ -11,9 +11,8 @@
use yii\helpers\Html; use yii\helpers\Html;
/** /** @var \yii\web\View $this */
* @var $content string /** @var string $content */
*/
?> ?>
<div class="clearfix"></div> <div class="clearfix"></div>

View File

@ -9,11 +9,11 @@
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.
*/ */
/** /** @var \yii\web\View $this */
* @var yii\web\View /** @var string $content */
* @var \Da\User\Module $module /** @var string $title */
* @var string $title /** @var \Da\User\Module $module */
*/
$this->title = $title; $this->title = $title;

View File

@ -14,11 +14,10 @@ use yii\bootstrap\Alert;
use yii\helpers\Html; use yii\helpers\Html;
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
/** /** @var \yii\web\View $this */
* @var $this yii\web\View /** @var string[] $availableItems */
* @var $model Da\User\Model\Assignment /** @var Da\User\Model\Assignment $model */
* @var $availableItems string[]
*/
?> ?>

4
stan_autoload.php Normal file
View File

@ -0,0 +1,4 @@
<?php
// PHPStan fails to autoload Yii somehow
include 'vendor/yiisoft/yii2/Yii.php';