Skip to content

Commit 7188f68

Browse files
committed
Raise PHPStan to level 1 and fix all findings
Bump the analysis level from 0 to 1 and resolve every new finding in code rather than baselining, matching the level-0 approach. Real bugs fixed: - extract_cname() checked an undefined $test['query'] instead of the $parsed_url['query'] parameter, so the query-string CNAME branch never ran. - connection-string.php passed two args to wp_kses_post() (1 arg) and the string was not translated; use esc_html__() with a translators comment. - remove_filter() in class-video.php was called with 4 args (max 3); drop the stray accepted_args value. Defensive / dead code simplified: - upgrade_connection() initialised $data and now bails when no cloudinary_url is present, instead of accessing a possibly-undefined variable. - Api::upload() initialises $result before the chunk loop so an empty file returns a WP_Error rather than reading an undefined variable. - Removed always-falsy $errors guard in Admin::save_settings(). - Removed redundant empty()/elseif branches in Delivery and Upgrade. Static-analysis support: - url_hash added to the Relationship @Property list (valid magic property backed by a DB column). - State and REST_API constructors now use the injected $plugin instead of get_plugin_instance()/discarding it, removing unused-parameter warnings. - New tests/phpstan/stubs/constants.php declares runtime-defined constants (CLDN_*, CLOUDINARY_ENDPOINTS_*, CLOUDINARY_CONNECTION_STRING) and the WP core constants WPINC/LOGGED_IN_COOKIE that the stub package omits. - Exclude php/templates/* since view files are included into a class scope, so $this is bound at runtime in a way PHPStan cannot model standalone. Run with: composer phpstan
1 parent f6b8ef3 commit 7188f68

12 files changed

Lines changed: 70 additions & 11 deletions

php/class-admin.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ public function init_setting_save() {
433433
*/
434434
protected function save_settings( $submission, $data ) {
435435
$page = $this->settings->get_setting( $submission );
436-
$errors = array();
437436
$pending = false;
438437
foreach ( $data as $key => $value ) {
439438
$slug = $submission . $page->separator . $key;
@@ -451,7 +450,7 @@ protected function save_settings( $submission, $data ) {
451450
$pending = true;
452451
}
453452

454-
if ( empty( $errors ) && true === $pending ) {
453+
if ( true === $pending ) {
455454
$results = $this->settings->save();
456455
if ( ! empty( $results ) ) {
457456
$this->add_admin_notice( 'error_notice', __( 'Settings updated successfully', 'cloudinary' ), 'success' );

php/class-connect.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public function test_ping() {
564564
protected function extract_cname( $parsed_url ) {
565565
$cname = null;
566566

567-
if ( ! empty( $test['query'] ) ) {
567+
if ( ! empty( $parsed_url['query'] ) ) {
568568
$config_params = array();
569569
wp_parse_str( $parsed_url['query'], $config_params );
570570
$cname = isset( $config_params['cname'] ) ? $config_params['cname'] : $cname;
@@ -874,6 +874,7 @@ public function get_notices() {
874874
*/
875875
public function upgrade_connection( $old_version ) {
876876

877+
$data = array();
877878
if ( version_compare( $old_version, '2.0.0', '>' ) ) {
878879
// Post V1 - quick check all details are valid.
879880
$data = $this->settings->get_value( 'connect' );
@@ -904,6 +905,9 @@ public function upgrade_connection( $old_version ) {
904905
}
905906

906907
// Test upgraded details.
908+
if ( empty( $data['cloudinary_url'] ) ) {
909+
return; // Nothing to upgrade.
910+
}
907911
$data['cloudinary_url'] = str_replace( 'CLOUDINARY_URL=', '', $data['cloudinary_url'] );
908912
$test = $this->test_connection( $data['cloudinary_url'] );
909913

php/class-delivery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ protected function is_content_dir( $url ) {
17441744
$dirname = trim( substr( $path, strlen( $base ), 8 ), DIRECTORY_SEPARATOR );
17451745
if ( empty( $dirname ) || preg_match( '/\d{4}\/\d{2}/', $dirname ) ) {
17461746
$is_local = true;
1747-
} elseif ( ! empty( $dirname ) ) {
1747+
} else {
17481748
$is_local = false;
17491749
}
17501750
}

php/class-rest-api.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class REST_API {
2626
*/
2727
public $endpoints;
2828

29+
/**
30+
* Holds the plugin instance.
31+
*
32+
* @var Plugin
33+
*/
34+
protected $plugin;
35+
2936
/**
3037
* The nonce key used for WordPress REST API authentication.
3138
*
@@ -38,7 +45,8 @@ class REST_API {
3845
*
3946
* @param Plugin $plugin Instance of the global Plugin.
4047
*/
41-
public function __construct( Plugin $plugin ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
48+
public function __construct( Plugin $plugin ) {
49+
$this->plugin = $plugin;
4250
add_action( 'rest_api_init', array( $this, 'rest_api_init' ), PHP_INT_MAX );
4351
}
4452

php/connect/class-api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ public function upload_large( $attachment_id, $args ) {
439439
$chunk_size = 20000000;
440440
$index = 0;
441441
$file_size = filesize( $args['file'] );
442+
$result = new \WP_Error( 'upload_error', __( 'No data to upload.', 'cloudinary' ) );
442443
while ( ! feof( $src ) ) {
443444
$current_loc = $index * $chunk_size;
444445
if ( $current_loc >= $file_size ) {

php/media/class-upgrade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function migrate_legacy_meta( $attachment_id ) {
248248
update_post_meta( $attachment_id, '_wp_attachment_metadata', $old_meta );
249249

250250
// migrate from pre v2 meta.
251-
if ( empty( $v2_meta ) && empty( $v3_meta ) ) {
251+
if ( empty( $v2_meta ) ) {
252252
// Attempt old post meta.
253253
$public_id = get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
254254
if ( ! empty( $public_id ) ) {

php/media/class-video.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function admin_enqueue_scripts() {
221221
*/
222222
public function filter_video_block_render_block( $block_content, array $block ) {
223223
if ( 'core/video' === $block['blockName'] ) {
224-
remove_filter( 'render_block', array( $this, 'filter_video_block_render_block' ), 10, 2 );
224+
remove_filter( 'render_block', array( $this, 'filter_video_block_render_block' ), 10 );
225225

226226
$filtered_block = $this->filter_video_block_pre_render( $block, $block );
227227
$block_content = render_block( $filtered_block );

php/relate/class-relationship.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @property string|null $text_overlay
2323
* @property string|null $image_overlay
2424
* @property string|null $sized_url
25+
* @property string|null $url_hash
2526
* @property string|null $media_context
2627
*/
2728
class Relationship {

php/templates/connection-string.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<p>
1515
<?php
1616
printf(
17-
wp_kses_post( 'After creating your %s:', 'cloudinary' ),
17+
// translators: %s is the "Cloudinary account" link.
18+
esc_html__( 'After creating your %s:', 'cloudinary' ),
1819
sprintf(
1920
// translators: Link to create a Cloudinary account.
2021
esc_html__( '%1$sCloudinary account%2$s', 'cloudinary' ),

php/ui/class-state.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class State {
6161
*
6262
* @param Plugin $plugin Instance of the plugin.
6363
*/
64-
public function __construct( Plugin $plugin ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
65-
$this->plugin = get_plugin_instance();
64+
public function __construct( Plugin $plugin ) {
65+
$this->plugin = $plugin;
6666
$this->user_id = get_current_user_id();
6767

6868
if ( ! empty( $this->user_id ) ) {

0 commit comments

Comments
 (0)