Completed
Pull Request — master (#22)
by Daryl
01:32
created

Location_Model::has_location()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Clubdeuce\WPLib\Components\GoogleMaps;
4
use Clubdeuce\WPGoogleMaps\Location;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Clubdeuce\WPLib\Components\GoogleMaps\Location.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
/**
7
 * Class Location_Model
8
 * @package Clubdeuce\WPLib\Components\GoogleMaps
9
 * @method  string address()
10
 * @method  string formatted_address()
11
 * @method  string state()
12
 * @method  string zip_code()
13
 * @method  float  latitude()
14
 * @method  string location_type()
15
 * @method  float  longitude()
16
 * @method  string place_id()
17
 * @method  array  type()
18
 * @method  array  viewport()
19
 */
20
class Location_Model extends Model_Base {
21
22
    /**
23
     * @var \Clubdeuce\WPGoogleMaps\Location
24
     */
25
    protected $_location;
26
27
	/**
28
	 * Location_Model constructor.
29
	 *
30
	 * @param array $args
31
	 */
32 4
	function __construct( $args = array() ) {
33
34 4
		$location = new Location( $args );
35
36 4
		$args = wp_parse_args( $args, array(
37 4
			'location' => $location,
38
		) );
39
40 4
		parent::__construct( $args );
41
42 4
	}
43
44
	/**
45
	 * @return bool
46
	 */
47 1
	function has_location() {
48
49 1
		return $this->_has( '_location' );
50
51
	}
52
53
    /**
54
     * @param  string $method_name
55
     * @return mixed
56
     */
57 4
    public function __call( $method_name, $args ) {
58 4
        $value = null;
59
60
	    do {
61
62 4
		    if ( property_exists( $this, "_{$method_name}" ) ) {
63 2
			    $property = "_{$method_name}";
64 2
			    $value = $this->{$property};
65 2
			    break;
66
		    }
67
68 2
		    if ( ! isset( $this->_location ) ) {
69 1
		    	break;
70
		    }
71
72 1
		    $value = call_user_func_array( array( $this->_location, $method_name ), $args );
73
74 1
	    } while ( false );
75
76 4
        return $value;
77
    }
78
79
}
80