Completed
Pull Request — master (#65)
by
unknown
02:55
created

HomeFinderWidgets   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 147
Duplicated Lines 26.53 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 39
loc 147
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hf_enqueue() 0 6 1
A homefinder_id() 0 7 2
A homefinder_class() 0 9 2
A get_homes_for_sale_widget() 13 13 1
A get_open_house_widget() 13 13 1
A get_foreclosure_homes_widget() 13 13 1
A get_affiliates_widget() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * HomeFinder Widgets Class
4
 *
5
 * @package RE-PRO
6
 */
7
8
// Exit if accessed directly.
9
if ( ! defined( 'ABSPATH' ) ) { exit; }
10
11
if ( ! class_exists( 'HomeFinderWidgets' ) ) {
12
	/**
13
	 * HomeFinderWidgets class.
14
	 */
15
	class HomeFinderWidgets {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
17
		/**
18
		 * Widget data to be sent to JS.
19
		 *
20
		 * @var [Array]
21
		 */
22
		static private $hf_data;
23
24
		/**
25
		 * __construct function.
26
		 *
27
		 * @access public
28
		 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
		 */
30
		public function __construct() {
31
			add_action( 'wp_footer', array( $this, 'hf_enqueue' ) );
32
33
		}
34
35
		/**
36
		 * Enqueue JS on footer and handle multiple widgets.
37
		 */
38
		public function hf_enqueue() {
39
			wp_enqueue_script( 'hf-widget-loader', 'http://www.homefinder.com/widgets/js/widgetLoader.js', array( 'jquery' ), null, true );
40
			wp_enqueue_script( 'hf-widgets-js', plugins_url( 'homefinder-widgets.js', __FILE__ ), array( 'jquery' ), null, true );
41
			wp_localize_script( 'hf-widgets-js', 'hf_data', static::$hf_data );
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
42
43
		}
44
45
		/**
46
		 * HomeFinder ID Names.
47
		 *
48
		 * @access public
49
		 * @param string $widget_id (default: '').
50
		 * @return string $widget_id.
51
		 */
52
		public function homefinder_id( $widget_id = '' ) {
53
54
			if ( '' !== $widget_id  ) {
55
				return sanitize_html_class( $widget_id );
56
			}
57
58
		}
59
60
		/**
61
		 * HomeFinder div Class Names.
62
		 *
63
		 * @access public
64
		 * @param string $widget_name (default: '').
65
		 * @return string class name.
66
		 */
67
		public function homefinder_class( $widget_name = '' ) {
68
69
			if ( '' !== $widget_name ) {
70
				return 'homefinder homefinder-widget homefinder-' . sanitize_html_class( $widget_name ) . '-widget';
71
			} else {
72
				return 'homefinder homefinder-widget';
73
			}
74
75
		}
76
77
		/* HomeFinder WIDGETS. */
78
79
		/**
80
		 * Get Homes For Sale Widget.
81
		 *
82
		 * @access public
83
		 * @return void
84
		 */
85 View Code Duplication
		public function get_homes_for_sale_widget() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
87
			$widget_data = array(
88
				'type' => 'homeSearch',
89
				'container' => 'homeSearchWidget',
90
			);
91
			static::$hf_data[] = $widget_data;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
92
93
			$index = count( static::$hf_data ) - 1;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
94
95
			echo '<div id="homeSearchWidget-'. $index .'" class="'. $this->homefinder_class( 'homes-for-sale' ) .'"></div>';
96
97
		}
98
99
		/**
100
		 * Get Open House Widget.
101
		 *
102
		 * @access public
103
		 * @return void
104
		 */
105 View Code Duplication
		public function get_open_house_widget() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
107
			$widget_data = array(
108
				'type' => 'openHouseSearch',
109
				'container' => 'openHouseSearchWidget',
110
			);
111
			static::$hf_data[] = $widget_data;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
112
113
			$index = count( static::$hf_data ) - 1;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
114
115
			echo '<div id="openHouseSearchWidget-'. $index .'" class="'. $this->homefinder_class( 'open-house' ) .'"></div>';
116
117
		}
118
119
		/**
120
		 * Get Foreclosure Homes Widget.
121
		 *
122
		 * @access public
123
		 * @return void
124
		 */
125 View Code Duplication
		public function get_foreclosure_homes_widget() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
127
			$widget_data = array(
128
				'type' => 'foreclosureSearch',
129
				'container' => 'foreclosureSearchWidget',
130
			);
131
			static::$hf_data[] = $widget_data;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
132
133
			$index = count( static::$hf_data ) - 1;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
134
135
			echo '<div id="foreclosureSearchWidget-'. $index .'" class="'. $this->homefinder_class( 'foreclosure-homes' ) .'"></div>';
136
137
		}
138
139
		/**
140
		 * Get HomeFinder Widget.
141
		 *
142
		 * @access public
143
		 * @param string  $type Widget Type.
144
		 * @param  [Mixed] $widget_data : Array of widget data to send to js.
0 ignored issues
show
Documentation introduced by
The doc-type [Mixed] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
145
		 * @return void
146
		 */
147
		public function get_affiliates_widget( $type, $widget_data ) {
148
149
			$widget_data['type'] = $type;
150
			static::$hf_data[] = $widget_data;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
151
152
			$index = count( static::$hf_data ) - 1;
0 ignored issues
show
Bug introduced by
Since $hf_data is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hf_data to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
153
154
			if ( 'search' === $type ) {
155
				echo '<div id="searchPreview-' . $index . '" class="'. $this->homefinder_class( 'affiliate-search' ) .'"><div>';
156
			} else if ( 'directory' === $type ) {
157
				echo '<div id="directoryPreview-' . $index . '" class="'. $this->homefinder_class( 'adveritser-directory' ) .'"></div>';
158
			}
159
160
		}
161
	}
162
}
163