Skip to content

Commit

Permalink
Addressed issues raised by PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Sep 4, 2024
1 parent c08cb46 commit 7e4e7bc
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 122 deletions.
48 changes: 33 additions & 15 deletions includes/ContentImport/ImportLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

class ImportLogger {

protected $levels_delimiter = '/';
protected string $levels_delimiter = '/';

protected $data = array(
/**
* @var array<string, array>
*/
protected array $data = array(
'info' => array(),
'error' => array(),
'success' => array(),
Expand All @@ -29,7 +32,7 @@ public function __construct( ImportCoordinates $import_coordinates ) {
*
* @param ImportLogger|null $logger
*/
public function merge( ImportLogger $logger = null ) {
public function merge( ImportLogger $logger = null ): void {
if ( null === $logger ) {
return;
}
Expand All @@ -38,16 +41,16 @@ public function merge( ImportLogger $logger = null ) {
}

/**
* @return array
* @return array<string, array>
*/
public function get_data() {
public function get_data(): array {
return $this->data;
}

/**
* Saves the log or prints it some place.
*/
public function save() {
public function save(): void {
$log_writer = $default_log_writer = AdminNoticeLogger::instance();
$log_writer->set_import_coordinates( $this->import_coordinates );

Expand Down Expand Up @@ -82,7 +85,7 @@ public function save() {
* @param string $where A location string using `/` as level format.
* @param mixed $what What should be stored in the log.
*/
public function log_error( $where, $what ) {
public function log_error( $where, $what ): void {
$this->log( $where, $what, 'error' );
}

Expand All @@ -93,7 +96,7 @@ public function log_error( $where, $what ) {
* @param mixed $what What should be stored in the log.
* @param string $root Where to log the information.
*/
protected function log( $where, $what, $root = 'info' ) {
protected function log( $where, $what, $root = 'info' ): void {
if ( ! isset( $this->data[ $root ] ) ) {
$this->data[ $root ] = array();
}
Expand All @@ -103,7 +106,7 @@ protected function log( $where, $what, $root = 'info' ) {
$this->data[ $root ] = array_merge_recursive( $this->data[ $root ], $data );
}

protected function build_nested_array( $path, $what = '' ) {
protected function build_nested_array( $path, $what = '' ): array {
$json = '{"'
. implode( '":{"', $path )
. '":' . wp_json_encode( $what )
Expand All @@ -125,7 +128,7 @@ protected function build_nested_array( $path, $what = '' ) {
*
* @return array
*/
protected function build_path( $where ) {
protected function build_path( $where ): array {
$where_path = explode( $this->levels_delimiter, $where );

return $where_path;
Expand All @@ -136,7 +139,7 @@ protected function build_path( $where ) {
*
* @return string
*/
public function get_levels_delimiter() {
public function get_levels_delimiter(): string {
return $this->levels_delimiter;
}

Expand All @@ -145,7 +148,7 @@ public function get_levels_delimiter() {
*
* @param string $levels_delimiter
*/
public function set_levels_delimiter( $levels_delimiter ) {
public function set_levels_delimiter( $levels_delimiter ): void {
$this->levels_delimiter = $levels_delimiter;
}

Expand All @@ -155,7 +158,7 @@ public function set_levels_delimiter( $levels_delimiter ) {
* @param string $where A location string using `/` as level format.
* @param mixed $what What should be stored in the log.
*/
public function log_success( $where, $what ) {
public function log_success( $where, $what ): void {
$this->log( $where, $what, 'success' );
}

Expand All @@ -164,16 +167,21 @@ public function log_success( $where, $what ) {
*
* @param string $message
*/
public function log_information( $key, $message ) {
public function log_information( $key, $message ): void {
$this->data['info'][ $key ] = $message;
}

/**
* @param string $where
*
* @return mixed
*/
public function get_error( $where ) {
return $this->get_nested_value( 'error' . $this->levels_delimiter . $where );
}

/**
* @param $where
* @param string $where
*
* @return mixed
*/
Expand All @@ -189,10 +197,20 @@ protected function get_nested_value( $where ) {
return $data;
}

/**
* @param string $where
*
* @return mixed
*/
public function get_success( $where ) {
return $this->get_nested_value( 'success' . $this->levels_delimiter . $where );
}

/**
* @param string $key
*
* @return mixed
*/
public function get_information( $key ) {
return isset( $this->data['info'][ $key ] ) ? $this->data['info'][ $key ] : '';
}
Expand Down
13 changes: 8 additions & 5 deletions includes/ContentImport/Importers/AttachmentsImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ class AttachmentsImporters extends ImportersBaseFactory {

const TYPE = 'attachments';

protected $importers_map = [
/**
* @var array<string, string>
*/
protected array $importers_map = array(
Linking::TYPE => Linking::class,
];
);

/**
* Returns the factory details.
*
* @return string
* @return \stdClass
*/
public function details() {
return (object) [
return (object) array(
'slug' => static::TYPE,
'name' => __( 'Image Attachments', 'multisite-language-switcher' ),
'importers' => $this->importers_info(),
'selected' => $this->selected(),
];
);
}
}
25 changes: 12 additions & 13 deletions includes/ContentImport/Importers/ImportersBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ abstract class ImportersBaseFactory extends MslsRegistryInstance implements Impo
const TYPE = 'none';

/**
* @var array An array defining the slug and Importer class relationships in
* @var array<string, string> An array defining the slug and Importer class relationships in
* the shape [ <slug> => <importer-class> ]
*/
protected $importers_map = [];
protected array $importers_map = array();

/**
* @return Importer
Expand Down Expand Up @@ -49,7 +49,6 @@ public function make( ImportCoordinates $import_coordinates ) {
* @param ImportCoordinates $import_coordinates
*
* @since TBD
*
*/
$map = apply_filters( "msls_content_import_{$type}_importers_map", $this->importers_map, $import_coordinates );

Expand All @@ -70,10 +69,10 @@ public function make( ImportCoordinates $import_coordinates ) {
* @return \stdClass
*/
public function details() {
return (object) [
return (object) array(
'name' => 'Base Factory',
'importers' => [],
];
'importers' => array(),
);
}

/**
Expand All @@ -90,9 +89,6 @@ public function selected() {
*
* @param string $selected The selected importer slug.
* @param ImportersFactory $this
*
* @since TBD
*
*/
$selected = apply_filters( "msls_content_import_{$slug}_selected", $selected, $this );

Expand All @@ -102,9 +98,12 @@ public function selected() {
protected function importers_info() {
return array_combine(
array_keys( $this->importers_map ),
array_map( function ( $importer_class ) {
return $importer_class::info();
}, $this->importers_map )
array_map(
function ( $importer_class ) {
return $importer_class::info();
},
$this->importers_map
)
);
}
}
}
4 changes: 2 additions & 2 deletions includes/ContentImport/Importers/ImportersFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function make( ImportCoordinates $import_coordinates );
/**
* Returns the factory details.
*
* @return string
* @return \stdClass
*/
public function details();

Expand All @@ -28,4 +28,4 @@ public function details();
* @return string
*/
public function selected();
}
}
13 changes: 8 additions & 5 deletions includes/ContentImport/Importers/PostFieldsImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ class PostFieldsImporters extends ImportersBaseFactory {

const TYPE = 'post-fields';

protected $importers_map = [
/**
* @var array<string, string>
*/
protected array $importers_map = array(
Duplicating::TYPE => Duplicating::class,
];
);

/**
* Returns the factory details.
*
* @return string
* @return \stdClass
*/
public function details() {
return (object) [
return (object) array(
'slug' => static::TYPE,
'name' => __( 'Post Fields', 'multisite-language-switcher' ),
'importers' => $this->importers_info(),
'selected' => $this->selected(),
];
);
}
}
26 changes: 18 additions & 8 deletions includes/ContentImport/Importers/PostMeta/Duplicating.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lloc\Msls\ContentImport\Importers\PostMeta;

use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\ContentImport\Importers\BaseImporter;

class Duplicating extends BaseImporter {
Expand All @@ -14,11 +15,11 @@ class Duplicating extends BaseImporter {
* @return \stdClass
*/
public static function info() {
return (object) [
'slug' => static::TYPE,
'name' => __( 'Duplicating', 'multisite-language-switcher' ),
'description' => __( 'Copies the source post meta to the destination.', 'multisite-language-switcher' )
];
return (object) array(
'slug' => static::TYPE,
'name' => __( 'Duplicating', 'multisite-language-switcher' ),
'description' => __( 'Copies the source post meta to the destination.', 'multisite-language-switcher' ),
);
}

public function import( array $data ) {
Expand Down Expand Up @@ -49,6 +50,13 @@ public function import( array $data ) {
return $data;
}

/**
* Filters the post meta that should not be imported.
*
* @param array $meta
*
* @return array
*/
public function filter_post_meta( array $meta ) {
$blacklist = array( '_edit_last', '_thumbnail_id', '_edit_lock' );

Expand All @@ -59,11 +67,13 @@ public function filter_post_meta( array $meta ) {
* @param array $meta
* @param ImportCoordinates $import_coordinates
*/
$blacklist = apply_filters( 'msls_content_import_post_meta_blacklist',
$blacklist = apply_filters(
'msls_content_import_post_meta_blacklist',
$blacklist,
$meta,
$this->import_coordinates );
$this->import_coordinates
);

return array_diff_key( $meta, array_combine( $blacklist, $blacklist ) );
}
}
}
13 changes: 8 additions & 5 deletions includes/ContentImport/Importers/PostMetaImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ class PostMetaImporters extends ImportersBaseFactory {

const TYPE = 'post-meta';

protected $importers_map = [
/**
* @var array<string, string>
*/
protected array $importers_map = array(
Duplicating::TYPE => Duplicating::class,
];
);

/**
* Returns the factory details.
*
* @return string
* @return \stdClass
*/
public function details() {
return (object) [
return (object) array(
'slug' => static::TYPE,
'name' => __( 'Meta Fields', 'multisite-language-switcher' ),
'importers' => $this->importers_info(),
'selected' => $this->selected(),
];
);
}
}
Loading

0 comments on commit 7e4e7bc

Please sign in to comment.