Conditions | 12 |
Paths | 448 |
Total Lines | 60 |
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 |
||
22 | public function __construct() { |
||
23 | // WordAds setting => default. |
||
24 | $settings = array( |
||
25 | 'wordads_approved' => false, |
||
26 | 'wordads_active' => false, |
||
27 | 'wordads_house' => true, |
||
28 | 'wordads_unsafe' => false, |
||
29 | 'enable_header_ad' => true, |
||
30 | 'wordads_second_belowpost' => true, |
||
31 | 'wordads_display_front_page' => true, |
||
32 | 'wordads_display_post' => true, |
||
33 | 'wordads_display_page' => true, |
||
34 | 'wordads_display_archive' => true, |
||
35 | 'wordads_custom_adstxt' => '', |
||
36 | 'wordads_custom_adstxt_enabled' => false, |
||
37 | 'wordads_ccpa_enabled' => false, |
||
38 | 'wordads_ccpa_privacy_policy_url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', |
||
39 | ); |
||
40 | |||
41 | // grab settings, or set as default if it doesn't exist. |
||
42 | $this->options = array(); |
||
|
|||
43 | foreach ( $settings as $setting => $default ) { |
||
44 | $option = get_option( $setting, null ); |
||
45 | |||
46 | if ( is_null( $option ) ) { |
||
47 | |||
48 | // Handle retroactively setting wordads_custom_adstxt_enabled to true if custom ads.txt content is already entered. |
||
49 | if ( 'wordads_custom_adstxt_enabled' === $setting ) { |
||
50 | $default = get_option( 'wordads_custom_adstxt' ) !== ''; |
||
51 | } |
||
52 | |||
53 | update_option( $setting, $default, true ); |
||
54 | $option = $default; |
||
55 | } |
||
56 | |||
57 | $this->options[ $setting ] = is_bool( $default ) ? (bool) $option : $option; |
||
58 | } |
||
59 | |||
60 | $host = 'localhost'; |
||
61 | if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
||
62 | $host = $_SERVER['HTTP_HOST']; |
||
63 | } |
||
64 | |||
65 | $this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $_SERVER['REQUEST_URI']; |
||
66 | if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
67 | $this->url = substr( $this->url, 0, strpos( $this->url, '?' ) ); |
||
68 | } |
||
69 | |||
70 | $this->cloudflare = self::is_cloudflare(); |
||
71 | $this->blog_id = Jetpack::get_option( 'id', 0 ); |
||
72 | $this->mobile_device = jetpack_is_mobile( 'any', true ); |
||
73 | $this->targeting_tags = array( |
||
74 | 'WordAds' => 1, |
||
75 | 'BlogId' => ( new Status() )->is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ), |
||
76 | 'Domain' => esc_js( wp_parse_url( home_url(), PHP_URL_HOST ) ), |
||
77 | 'PageURL' => esc_js( $this->url ), |
||
78 | 'LangId' => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else? |
||
79 | 'AdSafe' => 1, // TODO. |
||
80 | ); |
||
81 | } |
||
82 | |||
258 |
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: