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

[static-analysis] Fix static analysis level 1 errors. #679

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from

Conversation

bahiirwa
Copy link
Contributor

@bahiirwa bahiirwa commented Dec 28, 2023

Proposed changes

  • Fix variables defined out of block yet are shared.
  • Fix misnamed/missing declaration of variables in code.
  • Added ignores for false positives like in the templates where we pass $VARS from the part including the partial file.
  • Improved the logic for where we have already set the data types like arrays and then still checking it they exist. This returns truthy/falsy errors. I have replaced those with count() You can advise on that. The goal was to maintain the checks in place and not modify functionality.

@bahiirwa bahiirwa force-pushed the feature/static-analysis-level-1-fixes branch from 1b17dbe to 2419e91 Compare January 15, 2024 18:34
@bahiirwa bahiirwa changed the base branch from master to develop January 15, 2024 18:36
includes/class-freemius.php Show resolved Hide resolved
includes/class-freemius.php Outdated Show resolved Hide resolved
includes/class-freemius.php Show resolved Hide resolved
includes/class-freemius.php Show resolved Hide resolved
includes/class-fs-plugin-updater.php Outdated Show resolved Hide resolved
includes/fs-plugin-info-dialog.php Outdated Show resolved Hide resolved
templates/account.php Outdated Show resolved Hide resolved
templates/account.php Outdated Show resolved Hide resolved
@@ -482,7 +483,7 @@ class="dashicons dashicons-image-rotate"></i> <?php fs_esc_html_echo_x_inline( '
$profile[] = array(
'id' => 'bundle_plan',
'title' => $bundle_plan_text,
'value' => $bundle_plan_title
'value' => $bundle_title
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahiirwa Please check the comment above.

@@ -468,10 +468,11 @@ class="dashicons dashicons-image-rotate"></i> <?php fs_esc_html_echo_x_inline( '
);
}
} else {
$bundle_title = ( $has_bundle_license ? ucfirst( $fs->get_module_type() ) : '' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahiirwa Please check the comments below.

@bahiirwa bahiirwa force-pushed the feature/static-analysis-level-1-fixes branch from 83a307d to 9d67cdd Compare April 16, 2024 15:35
@bahiirwa bahiirwa force-pushed the feature/static-analysis-level-1-fixes branch from bfd1c8d to 48aaba0 Compare April 16, 2024 16:13
@Freemius Freemius deleted a comment from fajardoleo Apr 16, 2024
@bahiirwa bahiirwa marked this pull request as ready for review April 16, 2024 16:35
Copy link
Contributor

@swashata swashata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahiirwa please see my comments, thanks.

@@ -8958,7 +8955,7 @@ private function update_plugin_version_event() {
* @author Vova Feldman (@svovaf)
* @since 2.0.0
*
* @param array [string]array $plugins
* @param string[] $plugins
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahiirwa The correct php-doc type would be array<string, array>. This is not an array of string :)

@@ -14104,7 +14101,7 @@ private function activate_license(
$result = $fs->activate_license_on_many_installs( $user, $license_key, $blog_2_install_map );
}

if ( true === $result && ! empty( $site_ids ) ) {
if ( true === $result && count( $site_ids ) > 0 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahiirwa why this change? What rule was it breaking?

@@ -14323,6 +14320,7 @@ private function get_parent_and_addons_installs_info() {
)
);

$installed_addons_ids_map = array_flip( $installed_addons_ids );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a bug fix?

@@ -20061,7 +20057,7 @@ private function _store_licenses( $store = true, $module_id = false, $licenses =
}
}

if ( ! empty( $new_user_licenses_map ) ) {
if ( count( $new_user_licenses_map ) > 0 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something PHPStan is forcing us to do?

@@ -1048,7 +1049,7 @@ function plugins_api_filter( $data, $action = '', $args = null ) {
false
);

if ( ! empty( $addon_plugin_data ) ) {
if ( is_array( $addon_plugin_data ) && isset( $addon_plugin_data['Version'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, that return type of get_plugin_data is array, yet we have to check with is_array here again. Maybe if you feed PHPStan with some WP stub, it will not give these false positives?

@@ -166,7 +166,8 @@ function get( $key, $default = null ) {
isset( $cache_entry->timestamp ) &&
is_numeric( $cache_entry->timestamp )
) {
return $cache_entry->result;
return $cache_entry->result; // @phpstan-ignore-line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this empty line please.

@@ -157,7 +157,7 @@

$site_view_params[] = $view_params;

if ( empty( $install ) ) {
if ( is_object( $install ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug 😓 . if ( ! is_object( $install ) )

$has_trial = $has_trial || ( is_numeric( $plan->trial_period ) && ( $plan->trial_period > 0 ) );

if ( isset( $plan->trial_period ) ) {
$has_trial = ( is_numeric( $plan->trial_period ) && ( $plan->trial_period > 0 ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bahiirwa why not

$has_trial = isset( $plan->trial_period ) && is_numeric(...) && $plan->trial_period > 0;


$active_modules_by_id[ $data->id ] = true;
}
?>
<tr<?php if ( $is_active ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change needed for PHPStan?

@@ -27,7 +27,9 @@
$features_plan_map[ $feature->id ] = array( 'feature' => $feature, 'plans' => array() );
}

$features_plan_map[ $feature->id ]['plans'][ $plan->id ] = $feature;
if ( ! empty( $plan->id ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you add here

CleanShot 2024-05-13 at 19 41 07@2x

/**
 * @var Plan[] $plans
 */

does it still need this empty check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants