Skip to content

Commit

Permalink
Fixes #927 Add PHPStan support (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona authored Nov 28, 2024
1 parent 6f204e3 commit e1cd33b
Show file tree
Hide file tree
Showing 38 changed files with 319 additions and 172 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/lint_phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PHP Stan lint

on:
pull_request:
branches:
- trunk
- develop
- branch-*
- feature/*
- enhancement/*

jobs:
run:
runs-on: ${{ matrix.operating-system }}

strategy:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-versions: ['8.2']

name: WPRocket lint with PHP Stan. PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}.
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none # XDebug can be enabled here 'coverage: xdebug'
tools: composer:v2

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-scripts

- name: Lint with PHP Stan
run: composer run-stan -- --error-format=github

1 change: 1 addition & 0 deletions Tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Imagify\Tests\Unit;

use ReflectionException;
use ReflectionObject;
use WPMedia\PHPUnit\Unit\TestCase as PHPUnitTestCase;

Expand Down
7 changes: 5 additions & 2 deletions classes/Auth/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @since 1.9.5
* @author Grégory Viguier
*/
class Basic {
final class Basic {
use \Imagify\Traits\InstanceGetterTrait;

/**
Expand Down Expand Up @@ -48,6 +48,9 @@ public function get_auth_url( $url ) {
return $url;
}

$user = '';
$pass = '';

if ( defined( 'IMAGIFY_AUTH_USER' ) && defined( 'IMAGIFY_AUTH_PASSWORD' ) && IMAGIFY_AUTH_USER && IMAGIFY_AUTH_PASSWORD ) {
$user = IMAGIFY_AUTH_USER;
$pass = IMAGIFY_AUTH_PASSWORD;
Expand All @@ -60,7 +63,7 @@ public function get_auth_url( $url ) {
}
}

if ( empty( $user ) || empty( $pass ) ) {
if ( empty( $user ) ) {
// No credentials.
return $url;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Avif/IIS.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IIS extends AbstractIISDirConfFile {
/**
* Get unfiltered new contents to write into the file.
*
* @return array
* @return string
*/
protected function get_raw_new_contents() {
return trim( '
Expand Down
22 changes: 14 additions & 8 deletions classes/Bulk/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

use Exception;
use Imagify\Traits\InstanceGetterTrait;
use Imagify\Optimization\Process\ProcessInterface;
use WP_Error;

/**
* Bulk optimization
*/
class Bulk {
final class Bulk {
use InstanceGetterTrait;

/**
Expand All @@ -17,7 +19,7 @@ class Bulk {
*/
public function init() {
add_action( 'imagify_optimize_media', [ $this, 'optimize_media' ], 10, 3 );
add_action( 'imagify_convert_next_gen', [ $this, 'generate_nextgen_versions' ], 10, 2 );
add_action( 'imagify_convert_next_gen', [ $this, 'generate_nextgen_versions' ], 10, 2 ); // @phpstan-ignore-line
add_action( 'wp_ajax_imagify_bulk_optimize', [ $this, 'bulk_optimize_callback' ] );
add_action( 'wp_ajax_imagify_missing_nextgen_generation', [ $this, 'missing_nextgen_callback' ] );
add_action( 'wp_ajax_imagify_get_folder_type_data', [ $this, 'get_folder_type_data_callback' ] );
Expand Down Expand Up @@ -61,6 +63,11 @@ public function check_optimization_status( $process, $item ) {
}

$data = $process->get_data();

if ( ! $data ) {
return;
}

$progress = get_transient( 'imagify_bulk_optimization_result' );

if ( $data->is_optimized() ) {
Expand Down Expand Up @@ -149,7 +156,7 @@ private function decrease_counter( string $context ) {
* @param int $optimization_level Optimization level.
*/
public function optimize_media( int $media_id, string $context, int $optimization_level ) {
if ( ! $media_id || ! $context || ! is_numeric( $optimization_level ) ) {
if ( ! $media_id || ! $context ) {
$this->decrease_counter( $context );

return;
Expand Down Expand Up @@ -316,7 +323,7 @@ private function get_bulk_class_name( string $context ): string {
*
* @since 1.9
*
* @param int $class_name The class name.
* @param string $class_name The class name.
* @param string $context The context name.
*/
$class_name = apply_filters( 'imagify_bulk_class_name', $class_name, $context );
Expand Down Expand Up @@ -558,7 +565,7 @@ public function bulk_get_stats_callback() {
imagify_check_nonce( 'imagify-bulk-optimize' );

$folder_types = filter_input( INPUT_GET, 'types', FILTER_REQUIRE_ARRAY );
$folder_types = is_array( $folder_types ) ? array_filter( $folder_types, 'is_string' ) : [];
$folder_types = is_array( $folder_types ) ? $folder_types : [];

if ( ! $folder_types ) {
imagify_die( __( 'Invalid request', 'imagify' ) );
Expand Down Expand Up @@ -637,7 +644,7 @@ public function get_contexts() {
$types = apply_filters( 'imagify_bulk_page_types', $types );
$types = array_filter( (array) $types );

if ( isset( $types['library|wp'] ) && ! in_array( 'wp', $contexts, true ) ) {
if ( isset( $types['library|wp'] ) ) {
$contexts[] = 'wp';
}

Expand All @@ -649,12 +656,11 @@ public function get_contexts() {
if ( ! in_array( 'wp', $contexts, true ) ) {
$contexts[] = 'wp';
}
} elseif ( $folders_instance->has_active_folders() && ! in_array( 'custom-folders', $contexts, true ) ) {
} elseif ( $folders_instance->has_active_folders() ) {
$contexts[] = 'custom-folders';
}
}

return $contexts;
}

}
6 changes: 2 additions & 4 deletions classes/CDN/PushCDNInterface.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Imagify\CDN;

defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );

/**
* Interface to use for Push CDNs.
*
Expand Down Expand Up @@ -80,7 +78,7 @@ public function send_to_cdn( $is_new_upload );
* @param string $file_name Name of the file. Leave empty for the full size file.
* @return string URL to the file.
*/
public function get_file_url( $file_name = false );
public function get_file_url( $file_name = '' );

/**
* Get a file path.
Expand All @@ -92,5 +90,5 @@ public function get_file_url( $file_name = false );
* @param string $file_name Name of the file. Leave empty for the full size file. Use 'original' to get the path to the original file.
* @return string Path to the file.
*/
public function get_file_path( $file_name = false );
public function get_file_path( $file_name = '' );
}
9 changes: 5 additions & 4 deletions classes/Context/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class AbstractContext implements ContextInterface {
* @since 1.9
* @author Grégory Viguier
*/
protected $context;
protected $context = '';

/**
* Tell if the media/context is network-wide.
Expand Down Expand Up @@ -55,7 +55,7 @@ abstract class AbstractContext implements ContextInterface {
* @since 1.9
* @author Grégory Viguier
*/
protected $thumbnail_sizes;
protected $thumbnail_sizes = [];

/**
* Tell if the optimization process is allowed to backup in this context.
Expand All @@ -64,7 +64,7 @@ abstract class AbstractContext implements ContextInterface {
* @since 1.9
* @author Grégory Viguier
*/
protected $can_backup;
protected $can_backup = false;

/**
* Get the context "short name".
Expand Down Expand Up @@ -161,7 +161,7 @@ public function can_backup() {
* @return bool
*/
public function current_user_can( $describer, $media_id = null ) {
return $this->user_can( null, $describer, $media_id );
return $this->user_can( 0, $describer, $media_id );
}

/**
Expand All @@ -176,6 +176,7 @@ public function current_user_can( $describer, $media_id = null ) {
* @return bool
*/
public function user_can( $user_id, $describer, $media_id = null ) {
$user = 0;
$current_user_id = get_current_user_id();

if ( ! $user_id ) {
Expand Down
6 changes: 1 addition & 5 deletions classes/Context/CustomFolders.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @since 1.9
* @author Grégory Viguier
*/
class CustomFolders extends AbstractContext {
final class CustomFolders extends AbstractContext {
use InstanceGetterTrait;

/**
Expand Down Expand Up @@ -60,10 +60,6 @@ public function get_resizing_threshold() {
* @return bool
*/
public function can_backup() {
if ( isset( $this->can_backup ) ) {
return $this->can_backup;
}

$this->can_backup = get_imagify_option( 'backup' );

return $this->can_backup;
Expand Down
2 changes: 1 addition & 1 deletion classes/Context/Noop.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @since 1.9
* @author Grégory Viguier
*/
class Noop implements ContextInterface {
final class Noop implements ContextInterface {
use InstanceGetterTrait;

/**
Expand Down
24 changes: 6 additions & 18 deletions classes/Context/WP.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
namespace Imagify\Context;

use \Imagify\Traits\InstanceGetterTrait;

/**
* Context class used for the WP media library.
*
* @since 1.9
* @author Grégory Viguier
*/
class WP extends AbstractContext {
use \Imagify\Traits\InstanceGetterTrait;
final class WP extends AbstractContext {
use InstanceGetterTrait;

/**
* Context "short name".
Expand All @@ -26,7 +28,7 @@ class WP extends AbstractContext {
* @since 1.9.8
* @author Grégory Viguier
*/
protected $resizing_threshold;
protected $resizing_threshold = 0;

/**
* Get the thumbnail sizes for this context, except the full size.
Expand All @@ -45,10 +47,6 @@ class WP extends AbstractContext {
* }
*/
public function get_thumbnail_sizes() {
if ( isset( $this->thumbnail_sizes ) ) {
return $this->thumbnail_sizes;
}

$this->thumbnail_sizes = get_imagify_thumbnail_sizes();

return $this->thumbnail_sizes;
Expand All @@ -64,13 +62,7 @@ public function get_thumbnail_sizes() {
* @return int
*/
public function get_resizing_threshold() {
if ( isset( $this->resizing_threshold ) ) {
return $this->resizing_threshold;
}

if ( ! get_imagify_option( 'resize_larger' ) ) {
$this->resizing_threshold = 0;
} else {
if ( get_imagify_option( 'resize_larger' ) ) {
$this->resizing_threshold = max( 0, get_imagify_option( 'resize_larger_w' ) );
}

Expand All @@ -86,10 +78,6 @@ public function get_resizing_threshold() {
* @return bool
*/
public function can_backup() {
if ( isset( $this->can_backup ) ) {
return $this->can_backup;
}

$this->can_backup = get_imagify_option( 'backup' );

return $this->can_backup;
Expand Down
4 changes: 2 additions & 2 deletions classes/Imagifybeat/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @since 1.9.3
*/
class Actions {
final class Actions {
use InstanceGetterTrait;

/**
Expand Down Expand Up @@ -301,7 +301,7 @@ private function get_modified_optimization_statuses( $data ) {
// Sanitize the IDs: IDs come as strings, prefixed with an undescore character (to prevent JavaScript from screwing everything).
$media_ids = array_keys( $media_statuses );
$media_ids = array_map( function( $media_id ) {
return (int) substr( $media_id, 1 );
return substr( $media_id, 1 );
}, $media_ids );
$media_ids = array_filter( $media_ids );

Expand Down
6 changes: 3 additions & 3 deletions classes/Imagifybeat/Core.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
namespace Imagify\Imagifybeat;

defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
use \Imagify\Traits\InstanceGetterTrait;

/**
* Imagifybeat core.
*
* @since 1.9.3
* @author Grégory Viguier
*/
class Core {
use \Imagify\Traits\InstanceGetterTrait;
final class Core {
use InstanceGetterTrait;

/**
* Class init: launch hooks.
Expand Down
4 changes: 2 additions & 2 deletions classes/Job/MediaOptimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @since 1.9
*/
class MediaOptimization extends \Imagify_Abstract_Background_Process {
final class MediaOptimization extends \Imagify_Abstract_Background_Process {
use InstanceGetterTrait;

/**
Expand All @@ -24,7 +24,7 @@ class MediaOptimization extends \Imagify_Abstract_Background_Process {
/**
* The optimization process instance.
*
* @var ProcessInterface
* @var ?ProcessInterface
* @since 1.9
*/
protected $optimization_process;
Expand Down
2 changes: 1 addition & 1 deletion classes/Media/AbstractMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractMedia implements MediaInterface {
/**
* Context (where the media "comes from").
*
* @var ContextInterface
* @var string
* @since 1.9
* @access protected
* @author Grégory Viguier
Expand Down
Loading

0 comments on commit e1cd33b

Please sign in to comment.