Conditions | 11 |
Paths | 16 |
Total Lines | 52 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
17 | public function render() { |
||
|
|||
18 | $post = get_post(); |
||
19 | $mydata = new MslsOptionsPost( $post->ID ); |
||
20 | $languages = MslsOptionsPost::instance()->get_available_languages(); |
||
21 | $current = MslsBlogCollection::get_blog_language( get_current_blog_id() ); |
||
22 | $languages = array_diff_key( $languages, array( $current => $current ) ); |
||
23 | $input_lang = isset( $_GET['msls_lang'] ) ? $_GET['msls_lang'] : null; |
||
24 | $input_id = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null; |
||
25 | $has_input = null !== $input_lang && null !== $input_id; |
||
26 | $blogs = MslsBlogCollection::instance(); |
||
27 | $available = array_filter( array_map( function ( $lang ) use ( $mydata ) { |
||
28 | return $mydata->{$lang}; |
||
29 | }, array_keys( $languages ) ) ); |
||
30 | $has_translation = count( $available ) >= 1; |
||
31 | |||
32 | if ( $has_input || $has_translation ) { |
||
33 | add_thickbox(); |
||
34 | $label_template = __( 'Import content from %s', 'multisite-language-switcher' ); |
||
35 | $output = '<fieldset>'; |
||
36 | $output .= '<legend>' |
||
37 | . esc_html__( 'Warning! This will override and replace all the post content with the content from the source post!', |
||
38 | 'multisite-language-switcher' ) |
||
39 | . '</legend>'; |
||
40 | foreach ( $languages as $language => $label ) { |
||
41 | $id = $mydata->{$language}; |
||
42 | $blog = $blogs->get_blog_id( $language ); |
||
43 | $label = sprintf( $label_template, $label ); |
||
44 | if ( null === $id && $has_input && $input_lang === $language ) { |
||
45 | $id = $input_id; |
||
46 | $blog = $blogs->get_blog_id( $language ); |
||
47 | } |
||
48 | if ( null !== $id ) { |
||
49 | $this->data = [ |
||
50 | 'msls_import' => "{$blog}|{$id}", |
||
51 | ]; |
||
52 | $output .= sprintf( '<a class="button-primary thickbox" href="%s" title="%s">%s</a>', |
||
53 | $this->inline_thickbox_url( $this->data ), |
||
54 | $label, |
||
55 | $label |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 | $output .= '</fieldset>'; |
||
60 | } else { |
||
61 | $output = '<p>' . |
||
62 | esc_html__( 'No translated versions linked to this post: import content functionality is disabled.', |
||
63 | 'multisite-language-switcher' ) |
||
64 | . '</p>'; |
||
65 | } |
||
66 | |||
67 | echo $output; |
||
68 | } |
||
69 | |||
155 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: