Skip to content

Commit

Permalink
refactor: use null-coalesce operator where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Mar 24, 2024
1 parent 032ad82 commit 59fc283
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Admin/Fields/CheckboxGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CheckboxGroup extends Field implements RenderElement {
public function render(): void {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

$options = isset( $this->option['options'] ) ? $this->option['options'] : [];
$options = $this->option['options'] ?? [];
if ( ! is_array( $options ) ) {
return;
}
Expand Down Expand Up @@ -52,6 +52,6 @@ public function render(): void {
protected function get_custom_value( string $key ) {
$options = Settings::get_option( "{$this->controllable_option_name}", $this->type );

return isset( $options[ $key ] ) ? $options[ $key ] : null;
return $options[ $key ] ?? null;
}
}
8 changes: 4 additions & 4 deletions src/Admin/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function get_name() {
* @return string Label of the field.
*/
public function get_label() {
$kses = isset( $this->option['label_kses'] ) ? $this->option['label_kses'] : [];
$label = isset( $this->option['label'] ) ? $this->option['label'] : '';
$kses = $this->option['label_kses'] ?? [];
$label = $this->option['label'] ?? '';
if ( ! $kses ) {
return esc_html( $label );
}
Expand All @@ -80,7 +80,7 @@ public function get_label() {
* @return string
*/
public function get_placeholder(): string {
return isset( $this->option['placeholder'] ) ? $this->option['placeholder'] : '';
return $this->option['placeholder'] ?? '';
}

/**
Expand All @@ -89,7 +89,7 @@ public function get_placeholder(): string {
* @return string Description of the field.
*/
public function get_description(): string {
return isset( $this->option['description'] ) ? $this->option['description'] : '';
return $this->option['description'] ?? '';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/Fields/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function get_class(): string {
'small' => 'small-text',
'regular' => 'regular-text',
];
$field_size = isset( $this->option['input_size'] ) ? $this->option['input_size'] : '';
$field_size = $this->option['input_size'] ?? '';

if ( isset( $classes[ $field_size ] ) ) {
return $classes[ $field_size ];
Expand All @@ -86,6 +86,6 @@ protected function get_class(): string {
* @return string
*/
protected function get_type(): string {
return isset( $this->option['input_type'] ) ? $this->option['input_type'] : 'text';
return $this->option['input_type'] ?? 'text';
}
}
2 changes: 1 addition & 1 deletion src/Admin/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function generate_fields( array $controllables ): void {
$options = $controllable::get_options();
if ( ! empty( $options ) ) {
foreach ( $options as $option ) {
$valid_for = isset( $option['valid_for'] ) ? $option['valid_for'] : null;
$valid_for = $option['valid_for'] ?? null;
if ( null !== $valid_for && $this->type !== $valid_for ) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/ComponentsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ComponentsHelper {
* @return array Filtered list.
*/
public static function filter( array $components, array $options ): array {
$reaction_type = isset( $options['reaction_type'] ) ? $options['reaction_type'] : null;
$only_active = isset( $options['only_active'] ) ? $options['only_active'] : false;
$reaction_type = $options['reaction_type'] ?? null;
$only_active = $options['only_active'] ?? false;

$filtered_components = [];
foreach ( $components as $component ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/ContentTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function get_type_name( string $item_type ): string {
// Todo: Write a doc how to add custom types.
$type_names = array_merge( apply_filters( 'antispam_bee_item_types', [] ), $type_names );

return isset( $type_names[ $item_type ] ) ? $type_names[ $item_type ] : $item_type;
return $type_names[ $item_type ] ?? $item_type;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/LinkbackFromMyself.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class LinkbackFromMyself extends Base implements SpamReason {
* @return int Numeric result.
*/
public static function verify( array $item ): int {
$url = isset( $item['comment_author_url'] ) ? $item['comment_author_url'] : null;
$target_post_id = isset( $item['comment_post_ID'] ) ? $item['comment_post_ID'] : null;
$url = $item['comment_author_url'] ?? null;
$target_post_id = $item['comment_post_ID'] ?? null;
if ( empty( $url ) || empty( $target_post_id ) ) {
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/LinkbackPostTitleIsBlogName.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class LinkbackPostTitleIsBlogName extends Base implements SpamReason {
* @return int Numeric result.
*/
public static function verify( array $item ): int {
$body = isset( $item['comment_content'] ) ? $item['comment_content'] : null;
$blog_name = isset( $item['comment_author'] ) ? $item['comment_author'] : null;
$body = $item['comment_content'] ?? null;
$blog_name = $item['comment_author'] ?? null;
preg_match( '/<strong>(.*)<\/strong>\\n\\n/', $body, $matches );
if ( ! isset( $matches[1] ) ) {
return 0;
Expand Down

0 comments on commit 59fc283

Please sign in to comment.