Passed
Push — master ( 0b4d79...cc8b35 )
by Roberto
03:16
created

HasObject::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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 20 and the first side effect is on line 13.

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
 * Joomla! common library.
4
 *
5
 * @copyright  Copyright (C) 2017 Roberto Segura López, Inc. All rights reserved.
6
 * @license    GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm
7
 */
8
9
namespace Phproberto\Joomla\Object\Traits;
10
11
use Phproberto\Joomla\Object\Object;
12
13 1
defined('JPATH_PLATFORM') || die;
14
15
/**
16
 * Classes that have an associated object.
17
 *
18
 * @since  __DEPLOY_VERSION__
19
 */
20
trait HasObject
21
{
22
	/**
23
	 * Associated object.
24
	 *
25
	 * @var  Entity
26
	 */
27
	protected $object;
28
29
	/**
30
	 * Bind data to the object.
31
	 *
32
	 * @param   array  $data  Data to bind
33
	 *
34
	 * @return  self
35
	 */
36 2
	public function bind(array $data)
37
	{
38 2
		if (null === $this->object)
39
		{
40 1
			$this->object = new Object;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Phproberto\Joomla\Object\Object() of type object<Phproberto\Joomla\Object\Object> is incompatible with the declared type object<Phproberto\Joomla\Object\Traits\Entity> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
		}
42
43 2
		$this->object->bind($data);
44
45 2
		return $this;
46
	}
47
48
	/**
49
	 * Retrieve all the values of the object.
50
	 *
51
	 * @return  array
52
	 */
53 1
	public function data()
54
	{
55 1
		return $this->object()->data();
56
	}
57
58
	/**
59
	 * Get the associated object.
60
	 *
61
	 * @return  Entity
62
	 */
63 4
	public function object()
64
	{
65 4
		if (null === $this->object)
66
		{
67 4
			$this->loadObject();
68
		}
69
70 4
		return clone $this->object;
71
	}
72
73
	/**
74
	 * Get property of the object
75
	 *
76
	 * @param   string  $property  Name of the property to retrieve
77
	 * @param   mixed   $default   Default value if property does not exist
78
	 *
79
	 * @return  mixed
80
	 */
81 1
	public function get($property, $default = null)
82
	{
83 1
		return $this->object()->get($property, $default);
84
	}
85
86
	/**
87
	 * Check if the object has a property.
88
	 *
89
	 * @param   string  $property  Name of the property to check for
90
	 *
91
	 * @return  boolean
92
	 */
93 1
	public function has($property)
94
	{
95 1
		return $this->object()->has($property);
96
	}
97
98
	/**
99
	 * Load object.
100
	 *
101
	 * @return  self
102
	 */
103 4
	public function loadObject()
104
	{
105 4
		$this->object = new Object($this->loadData());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Phproberto\Joomla\O...ject($this->loadData()) of type object<Phproberto\Joomla\Object\Object> is incompatible with the declared type object<Phproberto\Joomla\Object\Traits\Entity> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
107 4
		return $this;
108
	}
109
110
	/**
111
	 * Load object data.
112
	 *
113
	 * @return  array
114
	 */
115
	abstract protected function loadData();
116
117
	/**
118
	 * Set value of an object property.
119
	 *
120
	 * @param   string  $property  Name of the property to set
121
	 * @param   mixed   $value     Value to assign
122
	 *
123
	 * @return  self
124
	 */
125 1
	public function set($property, $value)
126
	{
127 1
		if (null === $this->object)
128
		{
129 1
			$this->object = new Object;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Phproberto\Joomla\Object\Object() of type object<Phproberto\Joomla\Object\Object> is incompatible with the declared type object<Phproberto\Joomla\Object\Traits\Entity> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
130
		}
131
132 1
		$this->object->set($property, $value);
133
134 1
		return $this;
135
	}
136
137
	/**
138
	 * Set the object.
139
	 *
140
	 * @param   Object  $object  Value to assign
141
	 *
142
	 * @return  self
143
	 */
144 1
	public function setObject(Object $object)
145
	{
146 1
		$this->object = $object;
0 ignored issues
show
Documentation Bug introduced by
It seems like $object of type object<Phproberto\Joomla\Object\Object> is incompatible with the declared type object<Phproberto\Joomla\Object\Traits\Entity> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
147
148 1
		return $this;
149
	}
150
}
151