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

Fix all PHPStan problems #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
]
},
"require": {
"php": "^5.3 || ^7.0",
"deliciousbrains/wp-migrations": "^2.0"
},
"require-dev": {
"humanmade/coding-standards": "^1.1"
"humanmade/coding-standards": "^1.1",
"szepeviktor/phpstan-wordpress": "^0.7.7"
},
"scripts": {
"phpcbf": "phpcbf --standard=phpcs.xml.dist",
"phpcs": "phpcs --standard=phpcs.xml.dist"
"phpcbf": "phpcbf --standard=phpcs.xml.dist",
"phpcs": "phpcs --standard=phpcs.xml.dist"
}
}
8 changes: 8 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
}

if ( ! function_exists( 'dbi_get_auto_login_url' ) ) {
/**
* @param string $url
* @param int $user_id
* @param array<mixed> $args
* @param null|int $expires_in
*
* @return string
*/
function dbi_get_auto_login_url( $url, $user_id, $args = array(), $expires_in = null ) {
$autologin = \DeliciousBrains\WPAutoLogin\AutoLogin::instance();

Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
level: max
inferPrivatePropertyTypeFromConstructor: true
scanFiles:
- tests/bootstrap.php
paths:
- functions.php
- src/
ignoreErrors:
- '#^Parameter \#2 \$callable of static method WP_CLI::add_command\(\) expects callable\(\):#'
12 changes: 7 additions & 5 deletions src/AutoLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AutoLogin {
* @return AutoLogin Instance
*/
public static function instance( $command_name = 'dbi', $expires = 10_368_000 ) {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof AutoLogin ) ) {
if ( ! ( self::$instance instanceof AutoLogin ) ) {
self::$instance = new AutoLogin();
self::$instance->init( $command_name, $expires );
}
Expand All @@ -47,6 +47,8 @@ public static function instance( $command_name = 'dbi', $expires = 10_368_000 )
*
* @param string $command_name Name to used for WP-CLI command.
* @param int $expires Key expiry in seconds.
*
* @return void
*/
public function init( $command_name, $expires ) {
Migrator::instance();
Expand Down Expand Up @@ -197,10 +199,10 @@ public function remove_expired_keys() {
}

/**
* @param string $url
* @param int $user_id
* @param array $args
* @param null|int $expires_in Seconds
* @param string $url
* @param int $user_id
* @param array<mixed> $args
* @param null|int $expires_in Seconds
*
* @return string
*/
Expand Down
25 changes: 13 additions & 12 deletions src/CLI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ class Command extends \WP_CLI_Command {
* default: 172800
* ---
*
* @param array $args
* @param array $assoc_args
* @param array<mixed> $args
* @param array<string, mixed> $assoc_args
*
* @return null
* @return void
*/
public function auto_login_url( $args, $assoc_args ) {
if ( empty( $args[0] ) ) {
return \WP_CLI::warning( 'User ID or email address not supplied' );
\WP_CLI::warning( 'User ID or email address not supplied' );
return;
}

// Validate and fetch user
$field = is_numeric( $args[0] ) ? 'ID' : 'email';
$user = get_user_by( $field, $args[0] );
if ( ! $user ) {
return \WP_CLI::warning( 'User not found' );
\WP_CLI::warning( 'User not found' );
return;
}

// Validate expiry
Expand All @@ -49,26 +51,25 @@ public function auto_login_url( $args, $assoc_args ) {
$url = empty( $args[1] ) ? home_url() : $args[1];
$key_url = AutoLogin::instance()->create_url( $url, $user->ID, array(), $assoc_args['expiry'] );

return \WP_CLI::success( 'Auto-login URL generated: ' . $key_url );
\WP_CLI::success( 'Auto-login URL generated: ' . $key_url );
}

/**
* Purge expired auto-login keys from the database.
*
* @param array $args
* @param array $assoc_args
*
* @return null
* @param array<mixed> $args
* @param array<string, mixed> $assoc_args
* @return void
*/
public function purge_autologin_keys( $args, $assoc_args ) {
$legacy_keys_deleted = AutoLoginKey::delete_legacy_keys();
$regular_keys_deleted = AutoLoginKey::delete_expired_keys();
$total_keys_deleted = $legacy_keys_deleted + $regular_keys_deleted;

if ( false === $legacy_keys_deleted || false === $regular_keys_deleted ) {
return \WP_CLI::error( 'An error occurred while deleting expired keys. ' . $total_keys_deleted . ' keys were deleted.' );
\WP_CLI::error( 'An error occurred while deleting expired keys. ' . $total_keys_deleted . ' keys were deleted.' );
}

return \WP_CLI::success( $total_keys_deleted . ' keys were deleted.' );
\WP_CLI::success( $total_keys_deleted . ' keys were deleted.' );
}
}
2 changes: 1 addition & 1 deletion src/Model/AutoLoginKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AutoLoginKey {
/**
* Constructor
*
* @param array $attributes
* @param array<string, mixed> $attributes
*/
public function __construct( $attributes = array() ) {
foreach ( $attributes as $key => $value ) {
Expand Down
6 changes: 6 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Limit login attempts stub.
function is_limit_login_ok() {
return true;
}