Conditions | 10 |
Paths | 160 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
10 | public function __construct() { |
||
11 | // WordAds setting => default |
||
12 | $settings = array( |
||
13 | 'wordads_approved' => false, |
||
14 | 'wordads_active' => false, |
||
15 | 'wordads_house' => true, |
||
16 | 'wordads_unsafe' => false, |
||
17 | 'enable_header_ad' => true, |
||
18 | 'wordads_second_belowpost' => true, |
||
19 | 'wordads_display_front_page' => true, |
||
20 | 'wordads_display_post' => true, |
||
21 | 'wordads_display_page' => true, |
||
22 | 'wordads_display_archive' => true, |
||
23 | 'wordads_custom_adstxt' => '', |
||
24 | ); |
||
25 | |||
26 | // grab settings, or set as default if it doesn't exist |
||
27 | $this->options = array(); |
||
|
|||
28 | foreach ( $settings as $setting => $default ) { |
||
29 | $option = get_option( $setting, null ); |
||
30 | |||
31 | if ( is_null( $option ) ) { |
||
32 | update_option( $setting, $default, true ); |
||
33 | $option = $default; |
||
34 | } |
||
35 | |||
36 | $this->options[ $setting ] = 'wordads_custom_adstxt' !== $setting ? (bool) $option : $option; |
||
37 | } |
||
38 | |||
39 | $host = 'localhost'; |
||
40 | if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
||
41 | $host = $_SERVER['HTTP_HOST']; |
||
42 | } |
||
43 | |||
44 | $this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $_SERVER['REQUEST_URI']; |
||
45 | if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) { |
||
46 | $this->url = substr( $this->url, 0, strpos( $this->url, '?' ) ); |
||
47 | } |
||
48 | |||
49 | $this->cloudflare = self::is_cloudflare(); |
||
50 | $this->blog_id = Jetpack::get_option( 'id', 0 ); |
||
51 | $this->mobile_device = jetpack_is_mobile( 'any', true ); |
||
52 | $this->targeting_tags = array( |
||
53 | 'WordAds' => 1, |
||
54 | 'BlogId' => Jetpack::is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ), |
||
55 | 'Domain' => esc_js( parse_url( home_url(), PHP_URL_HOST ) ), |
||
56 | 'PageURL' => esc_js( $this->url ), |
||
57 | 'LangId' => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else? |
||
58 | 'AdSafe' => 1, // TODO |
||
59 | ); |
||
60 | } |
||
61 | |||
227 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: