Skip to content

Commit 4025dba

Browse files
committed
PHPStan issues for ImportLogger solved
1 parent 779fc7d commit 4025dba

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

includes/ContentImport/ImportLogger.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ public function get_data(): array {
5151
* Saves the log or prints it some place.
5252
*/
5353
public function save(): void {
54-
$log_writer = $default_log_writer = AdminNoticeLogger::instance();
55-
$log_writer->set_import_coordinates( $this->import_coordinates );
54+
$default_log_writer = AdminNoticeLogger::instance();
55+
$default_log_writer->set_import_coordinates( $this->import_coordinates );
5656

5757
/**
5858
* Filters the log class or object that should be used to write the log to the destination.
5959
*
60-
* @param LogWriter $log_writer
60+
* @param mixed $default_log_writer
6161
* @param ImportCoordinates $import_coordinates
6262
*/
63-
$log_writer = apply_filters( 'msls_content_import_log_writer', $log_writer, $this->import_coordinates );
64-
63+
$log_writer = apply_filters( 'msls_content_import_log_writer', $default_log_writer, $this->import_coordinates );
6564
if ( empty( $log_writer ) ) {
6665
// we assume that was done on purpose to prevent logging
6766
return;
@@ -106,6 +105,12 @@ protected function log( $where, $what, $root = 'info' ): void {
106105
$this->data[ $root ] = array_merge_recursive( $this->data[ $root ], $data );
107106
}
108107

108+
/**
109+
* @param array $path
110+
* @param mixed $what
111+
*
112+
* @return array
113+
*/
109114
protected function build_nested_array( $path, $what = '' ): array {
110115
$json = '{"'
111116
. implode( '":{"', $path )
@@ -124,11 +129,11 @@ protected function build_nested_array( $path, $what = '' ): array {
124129
}
125130

126131
/**
127-
* @param $where
132+
* @param string $where
128133
*
129134
* @return array
130135
*/
131-
protected function build_path( $where ): array {
136+
protected function build_path( string $where ): array {
132137
$where_path = explode( $this->levels_delimiter, $where );
133138

134139
return $where_path;
@@ -165,6 +170,7 @@ public function log_success( $where, $what ): void {
165170
/**
166171
* Logs some generic information.
167172
*
173+
* @param string $key
168174
* @param string $message
169175
*/
170176
public function log_information( $key, $message ): void {

includes/ContentImport/Importers/ImportersBaseFactory.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function make( ImportCoordinates $import_coordinates ) {
3434
*
3535
* Returning an Importer instance here will force the class to return that.
3636
*
37-
* @param $importer Importer
37+
* @param ?Importer $importer
3838
* @param ImportCoordinates $import_coordinates
3939
*/
4040
$importer = apply_filters( "msls_content_import_{$type}_importer", null, $import_coordinates );
@@ -81,21 +81,25 @@ public function details() {
8181
* @return string
8282
*/
8383
public function selected() {
84-
$selected = array_keys( $this->importers_map )[0];
8584
$slug = static::TYPE;
85+
$selected = array_keys( $this->importers_map )[0];
86+
$instance = $this;
8687

8788
/**
8889
* Filters the selected importer that among the available ones.
8990
*
9091
* @param string $selected The selected importer slug.
91-
* @param ImportersFactory $this
92+
* @param ImportersFactory $instance
9293
*/
93-
$selected = apply_filters( "msls_content_import_{$slug}_selected", $selected, $this );
94+
$selected = apply_filters( "msls_content_import_{$slug}_selected", $selected, $instance );
9495

9596
return $selected;
9697
}
9798

98-
protected function importers_info() {
99+
/**
100+
* @return array<string, \stdClass>
101+
*/
102+
protected function importers_info(): array {
99103
return array_combine(
100104
array_keys( $this->importers_map ),
101105
array_map(

includes/ContentImport/Importers/PostFields/Duplicating.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace lloc\Msls\ContentImport\Importers\PostFields;
44

5+
use lloc\Msls\ContentImport\ImportCoordinates;
56
use lloc\Msls\ContentImport\Importers\BaseImporter;
67
use lloc\Msls\ContentImport\Importers\WithRequestPostAttributes;
78

@@ -23,11 +24,11 @@ class Duplicating extends BaseImporter {
2324
* @return \stdClass
2425
*/
2526
public static function info() {
26-
return (object) [
27+
return (object) array(
2728
'slug' => static::TYPE,
2829
'name' => __( 'Duplicating', 'multisite-language-switcher' ),
29-
'description' => __( 'Copies the source post fields to the destination.', 'multisite-language-switcher' )
30-
];
30+
'description' => __( 'Copies the source post fields to the destination.', 'multisite-language-switcher' ),
31+
);
3132
}
3233

3334
public function import( array $data ) {
@@ -39,11 +40,11 @@ public function import( array $data ) {
3940
foreach ( $this->filter_fields() as $field ) {
4041
$value = $source_post->{$field};
4142
$data[ $field ] = $value;
42-
$this->logger->log_success( 'post-field/added', [ $field => $value ] );
43+
$this->logger->log_success( 'post-field/added', array( $field => $value ) );
4344
}
4445

4546
if ( ! doing_action( 'wp_insert_post_data' ) ) {
46-
$postarr = array_merge( $data, [ 'ID' => $this->import_coordinates->dest_post_id ] );
47+
$postarr = array_merge( $data, array( 'ID' => $this->import_coordinates->dest_post_id ) );
4748
wp_insert_post( $postarr );
4849
}
4950

tests/phpunit/ContentImport/TestContentImporter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use lloc\Msls\ContentImport\Relations;
88
use lloc\Msls\MslsMain;
99
use lloc\MslsTests\MslsUnitTestCase;
10+
use Brain\Monkey\Actions;
1011

1112
class TestContentImporter extends MslsUnitTestCase {
1213

@@ -15,6 +16,7 @@ public function setUp(): void {
1516
parent::setUp();
1617

1718
$main = \Mockery::mock( MslsMain::class );
19+
$main->shouldReceive( 'verify_nonce' )->andReturnTrue();
1820

1921
$this->test = new ContentImporter( $main );
2022
}
@@ -30,4 +32,28 @@ public function test_relations(): void {
3032

3133
$this->assertInstanceOf( Relations::class, $this->test->get_relations() );
3234
}
35+
36+
public function test_handle_import() {
37+
$this->assertEquals( array(), $this->test->handle_import() );
38+
}
39+
40+
public function test_parse_sources_no_post() {
41+
$this->assertFalse( $this->test->parse_sources() );
42+
}
43+
44+
public function test_handle_false() {
45+
$this->expectNotToPerformAssertions();
46+
47+
Actions\expectAdded( 'msls_main_save' )->once();
48+
49+
$this->test->handle( false );
50+
}
51+
52+
public function test_handle_true() {
53+
$this->expectNotToPerformAssertions();
54+
55+
Actions\expectRemoved( 'msls_main_save' )->once();
56+
57+
$this->test->handle( true );
58+
}
3359
}

0 commit comments

Comments
 (0)