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
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
uses: actions/upload-artifact@v2
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"
},
"require-dev": {
"2amigos/2fa-library": "^2.0",
"2amigos/qrcode-library": "^2.0",
"friendsofphp/php-cs-fixer": "^3",
"php": ">=7.4",
"yiisoft/yii2-symfonymailer": "~2.0.0",
@ -63,7 +65,8 @@
"codeception/module-filesystem": "^1.0",
"codeception/module-yii2": "^1.1",
"codeception/module-asserts": "^1.1",
"codeception/module-db": "^1.0"
"codeception/module-db": "^1.0",
"phpstan/phpstan": "^1.8"
},
"autoload": {
"psr-4": {
@ -76,6 +79,9 @@
}
},
"config": {
"platform": {
"php": "7.4"
},
"preferred-install": {
"*": "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 RecoveryForm $form
*/
class ResetPasswordEvent extends Event
final class ResetPasswordEvent extends Event
{
const EVENT_BEFORE_TOKEN_VALIDATE = 'beforeTokenValidate';
const EVENT_AFTER_TOKEN_VALIDATE = 'afterTokenValidate';

View File

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

View File

@ -65,5 +65,6 @@ class SwitchIdentityService implements ServiceInterface
/** @var IdentityInterface $user */
Yii::$app->user->switchIdentity($user, $session->timeout);
$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->password = !empty($model->password)
? $model->password
: $this->securityHelper->generatePassword(8, $this->getModule('user')->minPasswordRequirements);
: $this->securityHelper->generatePassword(8, $this->getModule()->minPasswordRequirements);
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$model]);
@ -76,14 +76,14 @@ class UserCreateService implements ServiceInterface
['email' => $model->email]
);
// 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(
'warning',
$error_msg
);
}
// 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);
}
$transaction->rollBack();

View File

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

View File

@ -32,9 +32,12 @@ class AjaxRequestModelValidator implements ValidatorInterface
if ($request->getIsAjax() && $this->model->load($request->post())) {
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->end();
return $result;
}
return false;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,8 +10,8 @@
*/
/**
* @var \yii\web\View
* @var yii\mail\BaseMessage $content
* @var \yii\web\View $this
* @var string $content
*/
?>
<?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
*/
?>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,8 @@
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');

View File

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

View File

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

View File

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

View File

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

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';