Skip to content

Commit 5359f6f

Browse files
authored
PHPStan issues resolved (#487)
1 parent b2155e5 commit 5359f6f

File tree

9 files changed

+77
-92
lines changed

9 files changed

+77
-92
lines changed

MultisiteLanguageSwitcher.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ function msls_get_query(): ?\lloc\Msls\MslsOptionsQuery {
235235
*
236236
* @return void
237237
*/
238-
if ( ! function_exists( '__return_void' ) ) {
239-
function __return_void(): void {
240-
}
238+
function msls_return_void(): void {
241239
}
242240

243241
lloc\Msls\MslsPlugin::init();

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
"scripts": {
3636
"test": "vendor/bin/phpunit",
3737
"coverage": "@php -d xdebug.mode=coverage vendor/bin/phpunit --coverage-html tests/coverage",
38-
"phpstan": "vendor/bin/phpstan analyze",
38+
"phpstan": "vendor/bin/phpstan analyze --memory-limit=1G",
3939
"php74": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 7.4",
40-
"php81": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.1",
4140
"php82": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.2",
41+
"php83": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.3",
42+
"php84": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.4",
4243
"prepare": "wget -O build/translations.json http://api.wordpress.org/translations/core/1.0/",
4344
"flags-png": "php bin/flags-png.php > flags/flags.php",
4445
"flags-svg": "php bin/flags-svg.php > css/flags.php",

includes/ContentImport/ContentImporter.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ class ContentImporter extends MslsRegistryInstance {
2424
/**
2525
* @var MslsMain
2626
*/
27-
protected $main;
27+
protected MslsMain $main;
28+
2829
protected ?ImportLogger $logger = null;
2930
protected ?Relations $relations = null;
3031

3132
/**
3233
* @var bool Whether the class should handle requests or not.
3334
*/
34-
protected $handle = true;
35+
protected bool $handle = true;
3536

3637
/**
3738
* @var int The ID of the post the class created while handling the request, if any.
3839
*/
39-
protected $has_created_post = 0;
40+
protected int $has_created_post = 0;
4041

4142
/**
4243
* ContentImporter constructor.
@@ -48,30 +49,30 @@ public function __construct( ?MslsMain $main = null ) {
4849
}
4950

5051
/**
51-
* @return \lloc\Msls\ContentImport\ImportLogger
52+
* @return ?ImportLogger
5253
*/
53-
public function get_logger() {
54+
public function get_logger(): ?ImportLogger {
5455
return $this->logger;
5556
}
5657

5758
/**
58-
* @param \lloc\Msls\ContentImport\ImportLogger $logger
59+
* @param ImportLogger $logger
5960
*/
60-
public function set_logger( $logger ): void {
61+
public function set_logger( ImportLogger $logger ): void {
6162
$this->logger = $logger;
6263
}
6364

6465
/**
65-
* @return \lloc\Msls\ContentImport\Relations
66+
* @return ?Relations
6667
*/
67-
public function get_relations() {
68+
public function get_relations(): ?Relations {
6869
return $this->relations;
6970
}
7071

7172
/**
72-
* @param \lloc\Msls\ContentImport\Relations $relations
73+
* @param Relations $relations
7374
*/
74-
public function set_relations( $relations ): void {
75+
public function set_relations( Relations $relations ): void {
7576
$this->relations = $relations;
7677
}
7778

@@ -241,9 +242,9 @@ public function handle( $handle ) {
241242

242243
// also prevent MSLS from saving
243244
if ( false === $handle ) {
244-
add_action( 'msls_main_save', '__return_void' );
245+
add_action( 'msls_main_save', 'msls_return_void' );
245246
} else {
246-
remove_action( 'msls_main_save', '__return_void' );
247+
remove_action( 'msls_main_save', 'msls_return_void' );
247248
}
248249
}
249250

includes/ContentImport/Importers/PostThumbnail/Linking.php

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,65 +60,62 @@ public function import( array $data ) {
6060

6161
switch_to_blog( $this->import_coordinates->dest_blog_id );
6262

63-
if ( $source_post_thumbnail_attachment instanceof \WP_Post ) {
64-
// in some instances the folder sep. `/` might be duplicated, we de-duplicate it
65-
array_walk(
66-
$source_upload_dir,
67-
function ( &$entry ) {
68-
$entry = str_replace( '//', '/', $entry );
69-
}
70-
);
71-
$source_uploads_dir = untrailingslashit(
72-
str_replace(
73-
$source_upload_dir['subdir'],
74-
'',
75-
$source_upload_dir['path']
76-
)
77-
);
78-
$source_post_thumbnail_file = $source_uploads_dir . '/' . $source_post_thumbnail_meta['_wp_attached_file'];
79-
80-
// Check the type of file. We'll use this as the 'post_mime_type'.
81-
$filetype = wp_check_filetype( basename( $source_post_thumbnail_file ), null );
82-
83-
// Prepare an array of post data for the attachment.
84-
$attachment = array(
85-
'guid' => $source_post_thumbnail_attachment->guid,
86-
'post_mime_type' => $filetype['type'],
87-
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $source_post_thumbnail_file ) ),
88-
'post_content' => '',
89-
'post_status' => 'inherit',
90-
);
63+
// in some instances, the folder sep. `/` might be duplicated, we de-duplicate it
64+
array_walk(
65+
$source_upload_dir,
66+
function ( &$entry ) {
67+
$entry = str_replace( '//', '/', $entry );
68+
}
69+
);
70+
$source_uploads_dir = untrailingslashit(
71+
str_replace(
72+
$source_upload_dir['subdir'],
73+
'',
74+
$source_upload_dir['path']
75+
)
76+
);
77+
$source_post_thumbnail_file = $source_uploads_dir . '/' . $source_post_thumbnail_meta['_wp_attached_file'];
78+
79+
// Check the type of file. We'll use this as the 'post_mime_type'.
80+
$filetype = wp_check_filetype( basename( $source_post_thumbnail_file ), null );
81+
82+
// Prepare an array of post data for the attachment.
83+
$attachment = array(
84+
'guid' => $source_post_thumbnail_attachment->guid,
85+
'post_mime_type' => $filetype['type'],
86+
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $source_post_thumbnail_file ) ),
87+
'post_content' => '',
88+
'post_status' => 'inherit',
89+
);
9190

92-
$existing_criteria = array(
91+
$found = get_posts(
92+
array(
9393
'post_type' => 'attachment',
9494
'title' => $attachment['post_title'],
95+
)
96+
);
97+
if ( isset( $found[0]->ID ) ) {
98+
$dest_post_thumbnail_id = $found[0]->ID;
99+
$this->logger->log_success( 'post-thumbnail/existing', $dest_post_thumbnail_id );
100+
} else {
101+
// Insert the attachment.
102+
$dest_post_thumbnail_id = wp_insert_attachment(
103+
$attachment,
104+
$source_post_thumbnail_file,
105+
$dest_post_id
95106
);
96107

97-
$found = get_posts( $existing_criteria );
98-
99-
if ( $found && $found[0] instanceof \WP_Post ) {
100-
$dest_post_thumbnail_id = $found[0]->ID;
101-
$this->logger->log_success( 'post-thumbnail/existing', $dest_post_thumbnail_id );
108+
if ( empty( $dest_post_thumbnail_id ) ) {
109+
$this->logger->log_error( 'post-thumbnail/created', $dest_post_thumbnail_id );
102110
} else {
103-
// Insert the attachment.
104-
$dest_post_thumbnail_id = wp_insert_attachment(
105-
$attachment,
106-
$source_post_thumbnail_file,
107-
$dest_post_id
108-
);
109-
110-
if ( empty( $dest_post_thumbnail_id ) ) {
111-
$this->logger->log_error( 'post-thumbnail/created', $dest_post_thumbnail_id );
112-
} else {
113-
$this->logger->log_success( 'post-thumbnail/created', $dest_post_thumbnail_id );
114-
}
115-
116-
// the `_wp_attached_file` meta has been set before, so we skip it
117-
unset( $source_post_thumbnail_meta['_wp_attached_file'] );
118-
119-
foreach ( $source_post_thumbnail_meta as $key => $value ) {
120-
add_post_meta( $dest_post_thumbnail_id, $key, $value, true );
121-
}
111+
$this->logger->log_success( 'post-thumbnail/created', $dest_post_thumbnail_id );
112+
}
113+
114+
// the `_wp_attached_file` meta has been set before, so we skip it
115+
unset( $source_post_thumbnail_meta['_wp_attached_file'] );
116+
117+
foreach ( $source_post_thumbnail_meta as $key => $value ) {
118+
add_post_meta( $dest_post_thumbnail_id, $key, $value, true );
122119
}
123120

124121
update_post_meta(

includes/MslsAdmin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function get_options_page_link(): string {
8686
*/
8787
public function __call( $method, $args ) {
8888
$parts = explode( '_', $method, 2 );
89-
if ( count( $parts ) > 0 && 'rewrite' === $parts[0] ) {
89+
if ( 2 === count( $parts ) && 'rewrite' === $parts[0] ) {
9090
$this->render_rewrite( $parts[1] );
9191
return;
9292
}

includes/MslsBlog.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,14 @@ public function get_url( $options ) {
127127
*
128128
* @return ?string
129129
*/
130-
protected function get_permalink( $options ) {
130+
protected function get_permalink( OptionsInterface $options ) {
131131
$url = null;
132132

133133
$is_home = is_front_page();
134134

135135
switch_to_blog( $this->obj->userblog_id );
136136

137-
if ( is_object( $options ) && method_exists(
138-
$options,
139-
'has_value'
140-
) && ( $is_home || $options->has_value( $this->get_language() ) ) ) {
137+
if ( $is_home || $options->has_value( $this->get_language() ) ) {
141138
$url = apply_filters( 'mlsl_blog_get_permalink', $options->get_permalink( $this->get_language() ), $this );
142139
}
143140

includes/MslsLink.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MslsLink extends MslsGetSet implements LinkInterface {
1717
protected $format_string = '<img src="{src}" alt="{alt}"/> {txt}';
1818

1919
/**
20-
* Gets all link types as array with "id => name"-items
20+
* Gets all link types as an array with "id => name"-items
2121
*
2222
* @return string[]
2323
*/
@@ -73,13 +73,8 @@ public static function create( ?int $display ): LinkInterface {
7373
/**
7474
* @param LinkInterface $obj
7575
* @param int $display
76-
*
77-
* @return LinkInterface
7876
*/
7977
$obj = apply_filters( 'msls_link_create', $obj, $display );
80-
if ( $obj instanceof LinkInterface ) {
81-
return $obj;
82-
}
8378
}
8479

8580
return $obj;

includes/MslsMetaBox.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use lloc\Msls\Component\Component;
66
use lloc\Msls\Component\Wrapper;
77
use lloc\Msls\ContentImport\MetaBox as ContentImportMetaBox;
8+
use WP_Post;
89

910
/**
1011
* Meta box for the edit mode of the (custom) post types
@@ -75,7 +76,7 @@ public static function suggest(): void {
7576
*/
7677
public static function get_suggested_fields( MslsJson $json, array $args ): MslsJson {
7778
/**
78-
* Overrides the query-args for the suggest fields in the MetaBox
79+
* Overrides the query-args for the 'suggest' fields in the MetaBox
7980
*
8081
* @param array $args<string, mixed>
8182
*
@@ -87,14 +88,13 @@ public static function get_suggested_fields( MslsJson $json, array $args ): Msls
8788
/**
8889
* Manipulates the WP_Post object before using it
8990
*
90-
* @param \WP_Post $post
91+
* @param WP_Post $post
9192
*
9293
* @since 0.9.9
9394
*/
9495
$post = apply_filters( 'msls_meta_box_suggest_post', $post );
95-
if ( is_object( $post ) ) {
96-
$json->add( $post->ID, get_the_title( $post ) );
97-
}
96+
97+
$json->add( $post->ID, get_the_title( $post ) );
9898
}
9999

100100
wp_reset_postdata();

phpstan.neon.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
parameters:
22
level: 6
3-
inferPrivatePropertyTypeFromConstructor: true
4-
treatPhpDocTypesAsCertain: false
53
paths:
64
- MultisiteLanguageSwitcher.php
75
- includes
8-
excludePaths:
9-
- vendor
106
bootstrapFiles:
117
- tests/phpstan/bootstrap.php
128
ignoreErrors:

0 commit comments

Comments
 (0)