junaidbhura / fly-dynamic-image-resizer

Fly Dynamic Image Resizer plugin for WordPress
https://wordpress.org/plugins/fly-dynamic-image-resizer/
MIT License
162 stars 26 forks source link

Static analysis says #27

Closed szepeviktor closed 5 years ago

szepeviktor commented 5 years ago

Hello @junaidbhura !

inc/class-fly-cli.php is loaded by the autoloader as /inc/class-fly_cli.php Could you rename the file?

diff --git a/fly-dynamic-image-resizer.php b/fly-dynamic-image-resizer.php
index f6a8ceb..f0403da 100755
--- a/fly-dynamic-image-resizer.php
+++ b/fly-dynamic-image-resizer.php
@@ -21,7 +21,7 @@ define( 'JB_FLY_PLUGIN_PATH', __DIR__ );
  * Require files.
  */
 if ( defined( 'WP_CLI' ) && WP_CLI ) {
-       require_once JB_FLY_PLUGIN_PATH . '/inc/class-fly-cli.php';
+       require_once JB_FLY_PLUGIN_PATH . '/inc/class-fly_cli.php';
 }
 require_once JB_FLY_PLUGIN_PATH . '/inc/namespace.php';
 require_once JB_FLY_PLUGIN_PATH . '/inc/helpers.php';
diff --git a/inc/class-core.php b/inc/class-core.php
index a54791f..2e5feff 100644
--- a/inc/class-core.php
+++ b/inc/class-core.php
@@ -14,7 +14,7 @@ class Core {
        /**
         * Get current instance.
         *
-        * @return object
+        * @return Core
         */
        public static function get_instance() {
                if ( ! self::$_instance ) {
@@ -90,7 +90,7 @@ class Core {
         * Add a new row action to media library items.
         *
         * @param  array $actions
-        * @param  object $post
+        * @param  \WP_Post $post
         * @return array
         */
        public function media_row_action( $actions, $post ) {
@@ -360,7 +360,7 @@ class Core {
         * @param  string  $file_name
         * @param  string  $width
         * @param  string  $height
-        * @param  boolean $crop
+        * @param  boolean|array $crop
         * @return string
         */
        public function get_fly_file_name( $file_name, $width, $height, $crop ) {

And some type problems.

szepeviktor commented 5 years ago

$ vendor/bin/phpstan analyze

 ------ ---------------------------------------------------------------------------------------------------
  Line   class-core.php
 ------ ---------------------------------------------------------------------------------------------------
  116    Parameter #1 $path of method JB\FlyImages\Core::get_fly_dir() expects string, int given.
  226    Method JB\FlyImages\Core::get_attachment_image_src() should return array but returns array|false.
  252    Parameter #1 $path of method JB\FlyImages\Core::get_fly_dir() expects string, int given.
  279    Parameter #1 $path of function wp_get_image_editor expects string, string|false given.
  320    Parameter #3 $icon of function wp_get_attachment_image expects bool, array given.
  338    Cannot access property $post_excerpt on WP_Post|null.
  341    Cannot access property $post_title on WP_Post|null.
 ------ ---------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------------------------------------------------
  Line   class-fly_cli.php
 ------ -------------------------------------------------------------------------------------------------------------
  39     Parameter #1 $attachment_id of method JB\FlyImages\Core::delete_attachment_fly_images() expects int, string
         given.
 ------ -------------------------------------------------------------------------------------------------------------

This is the result of static analysis using https://github.com/szepeviktor/phpstan-wordpress

szepeviktor commented 5 years ago

This is phpstan.neon.dist

includes:
    - phar://phpstan.phar/conf/bleedingEdge.neon
    - vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
    level: max
    inferPrivatePropertyTypeFromConstructor: true
    paths:
        - %currentWorkingDirectory%/inc/
    autoload_files:
        - /home/viktor/tmp/action-scheduler/wp-cli-stubs-2.2.0.php
        - %currentWorkingDirectory%/fly-dynamic-image-resizer.php
        - %currentWorkingDirectory%/inc/helpers.php
    ignoreErrors:
        # Uses func_get_args()
        - '#^Function apply_filters invoked with [34567] parameters, 2 required\.$#'
        - '#^Function do_action invoked with [3456] parameters, 1-2 required\.$#'
        # WP-CLI accepts a class as callable
        - '/^Parameter #2 \$callable of static method WP_CLI::add_command\(\) expects callable\(\): mixed, \S+ given\.$/'

wp-cli-stubs-2.2.0.php is from https://github.com/szepeviktor/phpstan-wordpress/blob/master/stub-generators/wp-cli-generate-stubs.sh

Are you interested?

junaidbhura commented 5 years ago

Hey @szepeviktor thanks for the issue. I'm closing it as it doesn't add any functionality to the plugin.

szepeviktor commented 5 years ago

Okay. Static analysis is like adding 100 pairs of 👀 to your project. It prevents errors humans are not always able to detect.

szepeviktor commented 5 years ago

@junaidbhura Please reconsider applying the fixes from the first comment

Thank you.