Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/phpstan upgrade #431

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MultisiteLanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ function msls_get_post( int $id ): \lloc\Msls\MslsOptionsPost {
* - is_tax
*
* @param int $id
* @return \lloc\Msls\MslsOptionsTax
* @return \lloc\Msls\OptionsTaxInterface
*/
function msls_get_tax( int $id ): \lloc\Msls\MslsOptionsTax {
function msls_get_tax( int $id ): \lloc\Msls\OptionsTaxInterface {
return \lloc\Msls\MslsOptionsTax::create( $id );
}

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"name": "lloc/multisite-language-switcher",
"description": "Multisite Language Switcher",
Expand All @@ -12,15 +12,15 @@
"require-dev": {
"phpunit/phpunit": "^10",
"brain/monkey": "^2.6",
"phpstan/phpstan": "^1.11",
"szepeviktor/phpstan-wordpress": "^1.3",
"phpstan/phpstan": "^2.1.0",
"szepeviktor/phpstan-wordpress": "^2.0.0",
"phpstan/extension-installer": "^1.3",
"antecedent/patchwork": "^2.1",
"squizlabs/php_codesniffer": "^3.9",
"phpcompatibility/php-compatibility": "^9.3",
"wp-coding-standards/wpcs": "^3.0",
"smeghead/php-class-diagram": "^1.3",
"phpstan/phpstan-mockery": "^1.1"
"phpstan/phpstan-mockery": "^2.0.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion includes/Component/Icon/IconLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace lloc\Msls\Component\Icon;

use lloc\Msls\Component\Icon;
use lloc\Msls\MslsPlugin;

/**
* Class IconLabel
Expand Down
2 changes: 1 addition & 1 deletion includes/ContentImport/AttachmentPathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function filter_attachment_url( $url, $attachment_id ) {
protected function get_source_post( $attachment_id, $msls_imported ) {
$source_post = get_blog_post( $msls_imported['blog'], $msls_imported['post'] );

if ( empty( $source_post ) || ! $source_post instanceof \WP_Post ) {
if ( ! $source_post instanceof \WP_Post ) {
delete_post_meta( $attachment_id, self::LINKED );

return false;
Expand Down
4 changes: 2 additions & 2 deletions includes/ContentImport/ContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
return false;
}

if ( ! isset( $_POST['msls_import'] ) ) {

Check warning on line 150 in includes/ContentImport/ContentImporter.php

View workflow job for this annotation

GitHub Actions / test

Processing form data without nonce verification.
return false;
}

Expand Down Expand Up @@ -285,7 +285,7 @@
* Returning a non `null` value here will override the creation of the importers map completely
* and use the one returned in the filter.
*
* @param null $importers
* @param ?array $importers
* @param ImportCoordinates $import_coordinates
*/
$importers = apply_filters( 'msls_content_import_importers', null, $import_coordinates );
Expand All @@ -297,7 +297,7 @@
$this->logger = $this->logger ?: new ImportLogger( $import_coordinates );
$this->relations = $this->relations ?: new Relations( $import_coordinates );

if ( ! empty( $importers ) && is_array( $importers ) ) {
if ( ! empty( $importers ) ) {
$source_post_id = $import_coordinates->source_post_id;
$dest_lang = $import_coordinates->dest_lang;
$dest_post_id = $import_coordinates->dest_post_id;
Expand Down Expand Up @@ -385,7 +385,7 @@
return $empty;
}

if ( ! isset( $_POST['msls_import'] ) ) {

Check warning on line 388 in includes/ContentImport/ContentImporter.php

View workflow job for this annotation

GitHub Actions / test

Processing form data without nonce verification.
return $empty;
}

Expand Down
4 changes: 3 additions & 1 deletion includes/ContentImport/ImportCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ImportCoordinates {
public $dest_post_id;

/**
* @var \WP_Post
* @var ?\WP_Post
*/
public $source_post;

Expand Down Expand Up @@ -55,9 +55,11 @@ public function validate() {
if ( ! get_blog_post( $this->source_blog_id, $this->source_post_id ) ) {
return false;
}

if ( ! get_blog_post( $this->dest_blog_id, $this->dest_post_id ) ) {
return false;
}

if ( ! $this->source_post instanceof \WP_Post ) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/ContentImport/Importers/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Map extends MslsRegistryInstance {
public function make( ImportCoordinates $import_coordinates ) {
$importers = array_map(
function ( $factory ) use ( $import_coordinates ) {
/** @var ImportersFactory $factory */
/** @var ImportersBaseFactory $factory */
return $factory->make( $import_coordinates );
},
$this->factories()
Expand Down
3 changes: 1 addition & 2 deletions includes/MslsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@
*/
public function __call( $method, $args ) {
$parts = explode( '_', $method, 2 );

if ( is_array( $parts ) && 'rewrite' === $parts[0] ) {
if ( count( $parts ) > 0 && 'rewrite' === $parts[0] ) {
$this->render_rewrite( $parts[1] );
return;
}
Expand Down Expand Up @@ -203,7 +202,7 @@
* Register the form-elements
*/
public function register(): void {
register_setting( 'msls', 'msls', array( $this, 'validate' ) );

Check warning on line 205 in includes/MslsAdmin.php

View workflow job for this annotation

GitHub Actions / test

Dynamic argument passed in third parameter of register_setting(). Please ensure proper sanitization.

$sections = array(
'language_section' => __( 'Language Settings', 'multisite-language-switcher' ),
Expand Down Expand Up @@ -386,7 +385,7 @@
);

// phpcs:ignore WordPress.Security.EscapeOutput
trigger_error( sprintf( esc_html( $format ), strval( self::MAX_REFERENCE_USERS ) ) );

Check warning on line 388 in includes/MslsAdmin.php

View workflow job for this annotation

GitHub Actions / test

trigger_error() found. Debug code should not normally be used in production.
}

// phpcs:ignore WordPress.Security.EscapeOutput
Expand Down
10 changes: 2 additions & 8 deletions includes/MslsBlogCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,8 @@ public function get_blogs_of_reference_user( MslsOptions $options ) {
);

$blogs = get_blogs_of_user( $reference_user );

/**
* @todo Check if this is still useful
*/
if ( is_array( $blogs ) ) {
foreach ( $blogs as $key => $blog ) {
$blogs[ $key ]->blog_id = $blog->userblog_id;
}
foreach ( $blogs as $key => $blog ) {
$blogs[ $key ]->blog_id = $blog->userblog_id;
}

return $blogs;
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public function get_available_languages(): array {
/**
* The 'blog'-slug-problem :/
*
* @param string $url
* @param mixed $url
* @param MslsOptions $options
*
* @return string
Expand Down
26 changes: 18 additions & 8 deletions includes/MslsOptionsTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MslsOptionsTax extends MslsOptions implements OptionsTaxInterface {
/**
* @param int $id
*
* @return MslsOptionsTax
* @return OptionsTaxInterface
*/
public static function create( $id = 0 ): OptionsTaxInterface {
$id = ! empty( $id ) ? (int) $id : get_queried_object_id();
Expand All @@ -33,23 +33,33 @@ public static function create( $id = 0 ): OptionsTaxInterface {
switch ( $req ) {
case 'category':
$options = new MslsOptionsTaxTermCategory( $id );
add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 );
break;
case 'post_tag':
$options = new MslsOptionsTaxTerm( $id );
add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 );
break;
default:
$options = new MslsOptionsTax( $id );
global $wp_rewrite;

$options = new MslsOptionsTax( $id );
$options->with_front = ! empty( $wp_rewrite->extra_permastructs[ $options->get_tax_query() ]['with_front'] );
}

if ( method_exists( $options, 'check_base' ) ) {
add_filter( 'msls_get_postlink', array( $options, 'check_base' ), 9, 2 );
} else {
global $wp_rewrite;
return $options;
}

$options->with_front = ! empty( $wp_rewrite->extra_permastructs[ $options->get_tax_query() ]['with_front'] );
/**
* @param int $id
*
* @return string
*/
public function get_content_type( int $id ): string {
if ( is_admin() ) {
return msls_content_types()->acl_request();
}

return $options;
return ( is_category() ? 'category' : is_tag( $id ) ) ? 'post_tag' : '';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsOptionsTaxTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MslsOptionsTaxTerm extends MslsOptionsTax implements OptionsTaxInterface {
/**
* Check and correct URL
*
* @param string $url
* @param mixed $url
* @param MslsOptionsTaxTerm $options
*
* @return string
Expand Down
10 changes: 5 additions & 5 deletions includes/MslsPostTagClassic.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ public function the_input( ?\WP_Term $tag, string $title_format, string $item_fo
/**
* Prints options inputs
*
* @param MslsBlog $blog
* @param string $type
* @param MslsOptionsTax $mydata
* @param string $item_format
* @param MslsBlog $blog
* @param string $type
* @param OptionsTaxInterface $mydata
* @param string $item_format
*/
public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $mydata, string $item_format ): void {
public function print_option( MslsBlog $blog, string $type, OptionsTaxInterface $mydata, string $item_format ): void {
switch_to_blog( $blog->userblog_id );

$language = $blog->get_language();
Expand Down
17 changes: 11 additions & 6 deletions includes/MslsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,34 @@ public static function get_config( string $name ): array {
return $config;
}

public static function has_var( string $name, ?int $input_type = null ): bool {
public static function has_var( string $var_name, ?int $input_type = null ): bool {
if ( null === $input_type ) {
try {
list( $input_type, ) = self::get_config( $name );
list( $input_type, ) = self::get_config( $var_name );
} catch ( \InvalidArgumentException $e ) {
return false;
}
}

return filter_has_var( $input_type, $name );
return filter_has_var( $input_type, $var_name );
}

/**
* @return mixed
*/
public static function get_var( string $name, ?int $input_type = null ) {
public static function get_var( string $var_name, ?int $input_type = null ) {
try {
list($type, $filter) = self::get_config( $name );
list($type, $filter) = self::get_config( $var_name );
} catch ( \InvalidArgumentException $e ) {
return null;
}

return filter_input( $input_type ?? $type, $name, $filter );
$type = $input_type ?? $type;
if ( in_array( $type, array( INPUT_POST, INPUT_GET, INPUT_COOKIE, INPUT_ENV, INPUT_SERVER ), true ) ) {
return filter_input( $type, $var_name, $filter );
}

return null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/MslsSqlCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/91e5fe9ada922a82a32b83eaabad1e2a2ee50338/MslsSqlCacher.php
*
* @method mixed get_var( string $sql )
* @method array get_results( string $sql )
* @method mixed[] get_results( string $sql )
* @method string prepare( string $sql, mixed $a, $b = '', $c = '' )
* @method mixed query( string $sql )
* @property string $posts
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
level: 6
inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: false
paths:
- MultisiteLanguageSwitcher.php
- includes
Expand Down
Loading