Completed
Pull Request — master (#22)
by Daryl
07:43
created

Location_Model::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
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 3
	function __construct( $args = array() ) {
33
34 3
		$args = wp_parse_args( $args, array(
35 3
			'location' => new Location( $args ),
36
		) );
37
38 3
		parent::__construct( $args );
39
40 3
	}
41
42
	/**
43
	 * @return bool
44
	 */
45 1
	function has_location() {
46
47 1
		return $this->_has( '_location' );
48
49
	}
50
51
    /**
52
     * @param  string $method_name
53
     * @return mixed
54
     */
55 4
    public function __call( $method_name, $args ) {
56 4
        $value = null;
57
58
	    do {
59
60 4
		    if ( property_exists( $this, "_{$method_name}" ) ) {
61 2
			    $property = "_{$method_name}";
62 2
			    $value = $this->{$property};
63 2
			    break;
64
		    }
65
66 2
		    if ( ! isset( $this->_location ) ) {
67 1
		    	break;
68
		    }
69
70 1
		    $value = call_user_func_array( array( $this->_location, $method_name ), $args );
71
72 1
	    } while ( false );
73
74 4
        return $value;
75
    }
76
77
}
78