Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

includes/MslsWidget.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * MslsWidget
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * The standard widget of the Multisite Language Switcher
12
 * @package Msls
13
 */
14
class MslsWidget extends \WP_Widget {
15
16
	/**
17
	 * Constructor
18
	 */
19
	public function __construct() {
20
		parent::__construct(
21
			false,
22
			$name = __( 'Multisite Language Switcher', 'multisite-language-switcher' )
23
		);
24
	}
25
26
	/**
27
	 * Output of the widget in the frontend
28
	 *
29
	 * @param array $args
30
	 * @param array $instance
31
	 *
32
	 * @user MslsOutput
33
	 */
34
	public function widget( $args, $instance ) {
35
		$args = wp_parse_args(
36
			$args,
37
			[
38
				'before_widget' => '',
39
				'after_widget'  => '',
40
				'before_title'  => '',
41
				'after_title'   => '',
42
			]
43
		);
44
45
		/** This filter is documented in wp-includes/default-widgets.php */
46
		$title = apply_filters(
47
			'widget_title',
48
			( isset( $instance['title'] ) ? $instance['title'] : '' ),
49
			$instance,
50
			$this->id_base
51
		);
52
		if ( $title ) {
53
			$title = $args['before_title'] . esc_attr( $title ) . $args['after_title'];
54
		}
55
56
		$content = MslsOutput::init()->__toString();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class lloc\Msls\MslsMain as the method __toString() does only exist in the following sub-classes of lloc\Msls\MslsMain: lloc\Msls\MslsOutput. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
57
		if ( '' == $content ) {
58
			$content = __( 'No available translations found', 'multisite-language-switcher' );
59
		}
60
61
		echo $args['before_widget'], $title, $content, $args['after_widget']; // xss ok
62
	}
63
64
	/**
65
	 * Update widget in the backend
66
	 *
67
	 * @param array $new_instance
68
	 * @param array $old_instance
69
	 *
70
	 * @return array
71
	 */
72
	public function update( $new_instance, $old_instance ) {
73
		$instance = $old_instance;
74
		if ( isset( $new_instance['title'] ) ) {
75
			$instance['title'] = strip_tags( $new_instance['title'] );
76
		}
77
78
		return $instance;
79
	}
80
81
	/**
82
	 * Display an input-form in the backend
83
	 *
84
	 * @param array $instance
85
	 *
86
	 * @codeCoverageIgnore
87
	 */
88
	public function form( $instance ) {
89
		printf(
90
			'<p><label for="%1$s">%2$s:</label> <input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>',
91
			$this->get_field_id( 'title' ),
92
			__( 'Title', 'multisite-language-switcher' ),
93
			$this->get_field_name( 'title' ),
94
			( isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '' )
95
		);
96
	}
97
98
}
99