Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable phpstan bleading edge and strict rules #100

Merged
merged 6 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

$finder = PhpCsFixer\Finder::create()
->in([__DIR__])
->exclude([
'cache',
'build',
'vendor',
]);
->exclude(['vendor']);

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
Expand Down Expand Up @@ -48,9 +44,6 @@
'no_superfluous_elseif' => false,
'ordered_class_elements' => false,
'php_unit_internal_class' => false,
'php_unit_test_case_static_method_calls' => [
'call_type' => 'this',
],
'php_unit_test_class_requires_covers' => false,
'phpdoc_add_missing_param_annotation' => false,
'return_assignment' => false,
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ Displays email and 2 password fields (for confirmation). If filled successfully

``` php
\Atk4\Login\LoginForm::addTo($app, [
'auth'=>$app->auth,
//'successLink'=>['dashboard'],
//'forgotLink'=>['forgot'],
'auth'=>$app->auth,
//'successLink'=>['dashboard'],
//'forgotLink'=>['forgot'],
]);
```

Expand All @@ -124,7 +124,7 @@ To check if user is currently logged in:

``` php
if ($app->auth->user->isLoaded()) {
// logged-in
// logged-in
}
```

Expand Down
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@
},
"require-release": {
"php": ">=7.4 <8.2",
"atk4/ui": "~3.2.0"
"atk4/ui": "~4.0.0"
},
"require-dev": {
"behat/behat": "^3.9",
"behat/mink": "^1.9",
"behat/mink-extension": "^2.3.1",
"behat/mink-selenium2-driver": "^1.5",
"ergebnis/composer-normalize": "^2.13",
"friendsofphp/php-cs-fixer": "^3.0",
"instaclick/php-webdriver": "^1.4.7",
"johnkary/phpunit-speedtrap": "^3.3",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpunit/phpunit": "^9.5.5",
"symfony/console": "^4.4.30 || ^5.3.7",
"symfony/css-selector": "^4.4.24 || ^5.2.9"
"phpstan/phpstan-strict-rules": "^1.3",
"phpunit/phpunit": "^9.5.5"
},
"conflict": {
"behat/behat": "<3.9",
"behat/mink": "<1.9",
"instaclick/php-webdriver": "<1.4.13",
"symfony/console": "<4.4.30 || >=5 <5.3.7",
"symfony/css-selector": "<4.4.24 || >=5 <5.2.9"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
7 changes: 2 additions & 5 deletions demos/_includes/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Atk4\Login\Auth;
use Atk4\Ui\Layout;

/**
* Example implementation of your Authenticated application.
*/
class App extends \Atk4\Ui\App
{
/** @var Auth */
Expand All @@ -24,7 +21,7 @@ protected function init(): void

$this->initLayout([Layout\Admin::class]);

// Construct menu
// construct menu
$this->layout->menuLeft->addItem(['Dashboard', 'icon' => 'info'], ['index']);
$this->layout->menuLeft->addItem(['Setup demo database', 'icon' => 'cogs'], ['admin-setup']);

Expand All @@ -51,7 +48,7 @@ public function initAuth(bool $check = true): void
{
$this->auth = new Auth($this, ['check' => $check, 'pageDashboard' => 'index']);

// Cannot setmodel at this stage :(
// cannot set model at this stage :(
$m = new \Atk4\Login\Model\User($this->db);
$this->auth->setModel($m);
}
Expand Down
11 changes: 4 additions & 7 deletions demos/_includes/MigratorConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
use Atk4\Core\DynamicMethodTrait;
use Atk4\Core\Factory;
use Atk4\Core\HookTrait;
use Atk4\Data\Model;
use Atk4\Data\Schema\Migrator;
use Atk4\Ui\Console;

/**
* Makes sure your database is adjusted for one or several models,
* that you specify.
*/
class MigratorConsole extends Console
{
use AppScopeTrait;
Expand All @@ -33,9 +30,9 @@ class MigratorConsole extends Console
/**
* Provided with array of models, perform migration for each of them.
*
* @param array $models
* @param array<Model|array> $models
*/
public function migrateModels($models): void
public function migrateModels(array $models): void
{
// run inside callback
$this->set(function ($console) use ($models) {
Expand All @@ -45,7 +42,7 @@ public function migrateModels($models): void

foreach ($models as $model) {
if (!is_object($model)) {
$model = Factory::factory((array) $model);
$model = Factory::factory($model);
$model->setPersistence($console->getApp()->db);
}

Expand Down
2 changes: 1 addition & 1 deletion demos/admin-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
$c->notice('Data imported');
});

$c1->migrateModels([Role::class, User::class, AccessRule::class, Client::class]);
$c1->migrateModels([[Role::class], [User::class], [AccessRule::class], [Client::class]]);

// button to execute migration
$b = Button::addTo($v, ['Run migration', 'icon' => 'check']);
Expand Down
29 changes: 11 additions & 18 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: 6
paths:
- ./
excludePaths:
- cache/
- build/
- vendor/

# TODO review once we drop PHP 7.x support
treatPhpDocTypesAsCertain: false

# some extra rules
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkExplicitMixedMissingReturn: true
checkFunctionNameCase: true
# TODO checkMissingClosureNativeReturnTypehintRule: true
reportMaybesInMethodSignatures: true
reportStaticMethodSignatures: true
checkTooWideReturnTypesInProtectedAndPublicMethods: true
checkMissingIterableValueType: false # TODO

ignoreErrors:
#- '~^Unsafe usage of new static\(\)\.$~'
- '~^(Property .+ has|Method .+\(\) (has parameter \$.+ with|return type has)) no value type specified in iterable type .+\.~'

# relax strict rules
- '~^Only booleans are allowed in .+, .+ given( on the (left|right) side)?\.~'

# TODO these rules are generated, this ignores should be fixed in the code
-
Expand Down Expand Up @@ -58,3 +47,7 @@ parameters:
message: '~^Call to an undefined method Atk4\\Ui\\Form\\Control::addAction\(\)\.$~'
count: 1
path: src/UserAdmin.php
-
message: '~^Call to an undefined method Atk4\\Ui\\View::hide\(\)\.$~'
count: 1
path: src/UserAdmin.php
10 changes: 5 additions & 5 deletions src/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Atk4\Data\Exception;
use Atk4\Data\Model;
use Atk4\Login\Model\AccessRule;
use Atk4\Login\Model\User;

/**
* Access Control Layer. Create one and pass it to your Auth controller.
Expand All @@ -23,11 +25,11 @@ class Acl
/**
* Returns AccessRules model for logged in user and in model scope.
*
* @return \Atk4\Login\Model\AccessRule
* @return AccessRule
*/
public function getRules(Model $model)
{
/** @var \Atk4\Login\Model\User */
/** @var User */
$user = $this->auth->user;

if (!$user->isLoaded()) {
Expand Down Expand Up @@ -80,9 +82,7 @@ public function applyRestrictions(Model $m): void
}

// add conditions on model
/*
* this will work in future when we will have json encoded condition structure stored in here
* for now let's comment this out
/* this will work in future when we will have json encoded condition structure stored in here
if ($rule['conditions']) {
$this->applyConditions($m, $rule['conditions']);
}
Expand Down
30 changes: 12 additions & 18 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Login\Cache\Session;
use Atk4\Login\Form as LoginForm;
use Atk4\Login\Layout\Narrow;
use Atk4\Login\Model\User;
use Atk4\Ui\App;
use Atk4\Ui\Layout\Admin;
use Atk4\Ui\Form;
use Atk4\Ui\Layout;
use Atk4\Ui\VirtualPage;

/**
* Authentication controller. Add this to your application somewhere
* and it will work wonders.
*/
class Auth
{
use AppScopeTrait;
Expand Down Expand Up @@ -82,7 +80,7 @@ class Auth
*
* @var array
*/
public $formLoginSeed = [Form\Login::class];
public $formLoginSeed = [LoginForm\Login::class];

/** @var array Seed that would create VirtualPage for adding Preference page content */
public $preferencePage = [VirtualPage::class];
Expand Down Expand Up @@ -271,11 +269,11 @@ public function check(): void
public function addUserMenu(): void
{
// add admin menu
if ($this->hasUserMenu && $this->getApp()->layout instanceof Admin) {
if ($this->hasUserMenu && $this->getApp()->layout instanceof Layout\Admin) {
$menu = $this->getApp()->layout->menuRight->addMenu($this->user->getTitle());

if ($this->hasPreferences) {
$userPage = VirtualPage::assertInstanceOf($this->getApp()->add($this->preferencePage));
$userPage = VirtualPage::addToWithCl($this->getApp(), $this->preferencePage);
$this->setPreferencePage($userPage);

$menu->addItem(['Preferences', 'icon' => 'user'], $userPage->getUrl());
Expand All @@ -290,7 +288,7 @@ public function addUserMenu(): void
*/
public function setPreferencePage(VirtualPage $page): void
{
$f = \Atk4\Ui\Form::addTo($page);
$f = Form::addTo($page);
$f->addHeader(['User Preferences', 'subHeader' => $this->user->getTitle(), 'icon' => 'user']);
$f->setModel($this->user);
$f->onSubmit(function ($f) {
Expand All @@ -314,15 +312,11 @@ public function displayLoginForm(array $seed = []): void
$app->initLayout([Narrow::class]);
$app->title .= ' - Login Required';
$app->layout->template->set('title', $app->title);
$app->add(array_merge(
$this->formLoginSeed,
[
'auth' => $this,
'linkSuccess' => [$this->pageAfterLogin],
'linkForgot' => false,
],
$seed
));
$app->add(Factory::factory($this->formLoginSeed, array_merge([
'auth' => $this,
'linkSuccess' => [$this->pageAfterLogin],
'linkForgot' => false,
], $seed)));
$app->run();
$app->callExit();
}
Expand Down
3 changes: 0 additions & 3 deletions src/Cache/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Atk4\Ui\App;
use Atk4\Ui\SessionTrait;

/**
* Session cache for authentication controller.
*/
class Session // implements CacheInterface
{
use AppScopeTrait;
Expand Down
9 changes: 4 additions & 5 deletions src/Feature/PasswordManagementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ private function calculatePasswordStrength(string $pw): int
if ($length > 2) {
// consecutive letters and numbers
foreach (['/[a-z]{2,}/', '/[A-Z]{2,}/', '/[0-9]{2,}/'] as $re) {
preg_match_all($re, $pw, $matches, \PREG_SET_ORDER);
if (!empty($matches)) {
if (preg_match_all($re, $pw, $matches, \PREG_SET_ORDER)) {
foreach ($matches as $match) {
$score -= (strlen($match[0]) - 1) * 2;
}
Expand Down Expand Up @@ -262,17 +261,17 @@ private function findSequence(array $charLocs, string $src): array
$charDistance = ord($charNext) - ord($charHere);
if ($distance === 1 && $charDistance === 1) {
// We find a pair of sequential chars!
if (empty($sequence)) {
if ($sequence === []) {
$sequence = [$charHere, $charNext];
} else {
$sequence[] = $charNext;
}
} elseif (!empty($sequence)) {
} elseif ($sequence !== []) {
$sequences[] = $sequence;
$sequence = [];
}
}
if (!empty($sequence)) {
if ($sequence !== []) {
$sequences[] = $sequence;
}

Expand Down
1 change: 0 additions & 1 deletion src/Feature/SetupAccessRuleModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ protected function init()
// And this new class should be separated to let's say AccessRule/Model class so we can
// also have AccessRule/Interface or AccessRule/View or AccessRule/Page class in future
// with different config properties

}
});
*/
Expand Down
9 changes: 1 addition & 8 deletions src/Form/Control/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,21 @@

use Atk4\Data\Model;

/**
* Form field to choose one or multiple model actions.
*/
class Actions extends GenericDropdown
{
public function setModel(Model $model, array $fields = null): void
{
// set function for dropdown row rendering
$this->renderRowFunction = function ($action) {
return [
'value' => $action->shortName,
'title' => $action->caption ?: $action->shortName,
'title' => $action->caption ?? $action->shortName,
'icon' => ($action->ui['icon'] ?? null),
];
};

parent::setModel($model);
}

/**
* Renders view.
*/
protected function renderView(): void
{
$model = $this->getModel();
Expand Down
Loading