Skip to content
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
70 changes: 16 additions & 54 deletions includes/MslsAdminIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,17 @@
*/
class MslsAdminIcon {

/**
* @var string
*/
protected $icon_type = 'action';

/**
* @var string
*/
protected $language;

/**
* @var string
*/
public $origin_language;

/**
* @var string
*/
protected $src;

/**
* @var string
*/
protected $href;

/**
* @var int
*/
protected $blog_id;

/**
* @var string
*/
protected $type;

/**
* @var string
*/
protected $path = 'post-new.php';

/**
* The current object ID
*
* @var int
*/
protected $id;

const TYPE_FLAG = 'flag';

protected string $icon_type = 'action';
protected string $language;
public string $origin_language;
protected string $src;
protected string $href;
protected int $blog_id;
protected string $type;
protected string $path = 'post-new.php';
protected int $id;
Comment on lines +16 to +24
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The newly added typed properties are not initialized with default values. In PHP 7.4+, accessing an uninitialized typed property before it has been assigned a value will throw a TypeError. Since these properties (like $language, $src, $href, and $id) are not initialized in the constructor but are accessed in methods such as get_img(), get_a(), and get_icon(), this will lead to runtime crashes if the setter methods are not called first. Initializing them with safe defaults (e.g., '' or 0) ensures the object is always in a valid state.

	protected string $icon_type = 'action';
	protected string $language = '';
	public string $origin_language = '';
	protected string $src = '';
	protected string $href = '';
	protected int $blog_id = 0;
	protected string $type = '';
	protected string $path = 'post-new.php';
	protected int $id = 0;


const TYPE_FLAG = 'flag';
const TYPE_LABEL = 'label';

/**
Expand All @@ -70,7 +32,7 @@ class MslsAdminIcon {
* @param string $type
*/
public function __construct( ?string $type = null ) {
$this->type = $type;
$this->type = $type ?? '';

$this->set_path();
}
Expand Down Expand Up @@ -158,7 +120,7 @@ public function set_src( string $src ): MslsAdminIcon {
* @return MslsAdminIcon
*/
public function set_href( int $id ): MslsAdminIcon {
$this->href = get_edit_post_link( $id );
$this->href = get_edit_post_link( $id ) ?? '';

return $this;
}
Expand Down Expand Up @@ -227,8 +189,8 @@ public function get_a(): string {
* @return bool
*/
protected function should_quick_create(): bool {
return null !== $this->id
&& null !== $this->origin_language
return 0 !== $this->id
&& '' !== $this->origin_language
&& msls_options()->activate_quick_create;
}

Expand Down Expand Up @@ -298,7 +260,7 @@ public function get_icon(): string {
public function get_edit_new(): string {
$path = $this->path;

if ( null !== $this->id && null !== $this->origin_language ) {
if ( 0 !== $this->id && '' !== $this->origin_language ) {
$path = add_query_arg(
array(
'msls_id' => $this->id,
Expand Down
13 changes: 4 additions & 9 deletions includes/MslsAdminIconTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
*/
class MslsAdminIconTaxonomy extends MslsAdminIcon {

/**
* Path
*
* @var string
*/
protected $path = 'edit-tags.php';
protected string $path = 'edit-tags.php';

/**
* Set href
Expand All @@ -27,7 +22,7 @@ class MslsAdminIconTaxonomy extends MslsAdminIcon {
public function set_href( int $id ): MslsAdminIcon {
$object_type = MslsTaxonomy::instance()->get_post_type();

$this->href = get_edit_term_link( $id, $this->type, $object_type );
$this->href = get_edit_term_link( $id, $this->type, $object_type ) ?? '';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The get_edit_term_link function can return a WP_Error object. Since $this->href is now strictly typed as a string, assigning a WP_Error object to it will cause a TypeError at runtime. The current null-coalescing operator (??) only handles null values, not WP_Error. You should verify that the returned value is not an error before assignment.

		$link = get_edit_term_link( $id, $this->type, $object_type );
		$this->href = ! is_wp_error( $link ) ? (string) $link : '';


return $this;
}
Expand All @@ -38,9 +33,9 @@ public function set_href( int $id ): MslsAdminIcon {
* @return MslsAdminIconTaxonomy
*/
public function set_path(): MslsAdminIcon {
$args = array( 'taxonomy' => $this->type );
$post_type = MslsTaxonomy::instance()->get_post_type();
$args = array( 'taxonomy' => $this->type );

$post_type = MslsTaxonomy::instance()->get_post_type();
if ( '' !== $post_type ) {
$args['post_type'] = $post_type;
}
Expand Down
Loading