absences_DynamicRight   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 178
Duplicated Lines 19.1 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 34
loc 178
rs 10
ccs 42
cts 72
cp 0.5833
wmc 15
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getById() 0 7 1
A getRow() 0 16 3
A getQuantity() 0 4 1
A setAgentRight() 0 5 1
A getAgentRight() 0 12 2
A setEntry() 0 5 1
A getEntry() 10 10 2
B save() 24 62 4

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
2
/************************************************************************
3
 * OVIDENTIA http://www.ovidentia.org                                   *
4
 ************************************************************************
5
 * Copyright (c) 2003 by CANTICO ( http://www.cantico.fr )              *
6
 *                                                                      *
7
 * This file is part of Ovidentia.                                      *
8
 *                                                                      *
9
 * Ovidentia is free software; you can redistribute it and/or modify    *
10
 * it under the terms of the GNU General Public License as published by *
11
 * the Free Software Foundation; either version 2, or (at your option)  *
12
 * any later version.													*
13
 *																		*
14
 * This program is distributed in the hope that it will be useful, but  *
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of			*
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.					*
17
 * See the  GNU General Public License for more details.				*
18
 *																		*
19
 * You should have received a copy of the GNU General Public License	*
20
 * along with this program; if not, write to the Free Software			*
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,*
22
 * USA.																	*
23
************************************************************************/
24
25
26
27
28
29 1
require_once dirname(__FILE__).'/record.class.php';
30
31
/**
32
 * @property int	$id_user_right
33
 * @property float 	$quantity
34
 * @property int	$id_entry
35
 * @property string $createdOn
36
 *
37
 */
38
class absences_DynamicRight extends absences_Record
39
{
40
	
41
	
42
	/**
43
	 *
44
	 * @var absences_Entry
45
	 */
46
	private $entry;
47
	
48
	
49
	/**
50
	 * @return absences_DynamicRight
51
	 */
52 2
	public static function getById($id)
53
	{
54 2
		$dright = new absences_DynamicRight;
55 2
		$dright->id = $id;
56
	
57 2
		return $dright;
58
	}
59
	
60
	
61
	/**
62
	 * (non-PHPdoc)
63
	 * @see absences_Record::getRow()
64
	 */
65 3
	public function getRow()
66
	{
67 3
		if (null === $this->row)
68 3
		{
69
			if (!isset($this->id))
70
			{
71
				throw new Exception('Failed to load dynamic right, missing id');
72
			}
73
	
74
			global $babDB;
75
			$res = $babDB->db_query('SELECT * FROM absences_dynamic_rights WHERE id='.$babDB->quote($this->id));
76
			$this->setRow($babDB->db_fetch_assoc($res));
77
		}
78
	
79 3
		return $this->row;
80
	}
81
	
82
	/**
83
	 * Get quantity
84
	 * @return number
85
	 */
86 2
	public function getQuantity()
87
	{
88 2
		return (float) $this->quantity;
89
	}
90
	
91
	
92
	/**
93
	 *
94
	 * @param absences_AgentRight $agentright
95
	 * @return absences_DynamicRight
96
	 */
97 2
	public function setAgentRight(absences_AgentRight $agentright)
98
	{
99 2
		$this->agentright = $agentright;
0 ignored issues
show
Documentation introduced by
The property agentright does not exist on object<absences_DynamicRight>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100 2
		return $this;
101
	}
102
	
103
	
104
	/**
105
	 * @return absences_AgentRight
106
	 */
107 3
	public function getAgentRight()
108
	{
109 3
		if (!isset($this->agentright))
110 3
		{
111 3
			require_once dirname(__FILE__).'/agent_right.class.php';
112
	
113 3
			$row = $this->getRow();
114 3
			$this->agentright = absences_AgentRight::getById($row['id_user_right']);
0 ignored issues
show
Documentation introduced by
The property agentright does not exist on object<absences_DynamicRight>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115 3
		}
116
	
117 3
		return $this->agentright;
0 ignored issues
show
Documentation introduced by
The property agentright does not exist on object<absences_DynamicRight>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
	}
119
	
120
	
121
	/**
122
	 *
123
	 * @param absences_Entry $entry
124
	 * @return absences_EntryElem
125
	 */
126
	public function setEntry(absences_Entry $entry)
127
	{
128
		$this->entry = $entry;
129
		return $this;
130
	}
131
	
132
	
133
	/**
134
	 * @return absences_Entry
135
	 */
136 View Code Duplication
	public function getEntry()
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...
137
	{
138
		if (!isset($this->entry))
139
		{
140
			$row = $this->getRow();
141
			$this->entry = absences_Entry::getById($row['id_entry']);
142
		}
143
	
144
		return $this->entry;
145
	}
146
	
147
	
148
	
149
	
150
	/**
151
	 * Save element (insert or update or delete)
152
	 */
153 3
	public function save()
154
	{
155 3
		global $babDB;
156
	
157 3
		if (isset($this->id))
158 3
		{
159
			$quantity = (int) round(100 * $this->quantity);
160
	
161
			if (0 === $quantity)
162
			{
163
				// if quantity has been set to 0, the element must be deleted
164
	
165
				$babDB->db_query("DELETE FROM absences_dynamic_rights WHERE id=".$babDB->quote($this->id));
166
	
167
			} else {
168
	
169
	
170
				$babDB->db_query("
171
					UPDATE absences_dynamic_rights 
172
					SET
173
						id_entry=".$babDB->quote($this->id_entry).", 
174
						quantity=".$babDB->quote($this->quantity)." 
175
					WHERE
176
						id=".$babDB->quote($this->id)
177
				);
178
	
179
			}
180
	
181
	
182 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
183
	
184 3
			if (isset($this->id_entry))
185 3
			{
186 3
				$id_entry = $this->id_entry;
187 3
			} else {
188
				$entry = $this->getEntry();
189
				$id_entry = $entry->id;
190
			}
191
	
192 3
			$babDB->db_query("
193
				INSERT INTO absences_dynamic_rights 
194
					(id_entry, id_user_right, quantity, createdOn)
195
				VALUES
196
					(
197 3
					" .$babDB->quote($id_entry). ",
198 3
					" .$babDB->quote($this->id_user_right). ",
199 3
					" .$babDB->quote($this->quantity). ",
200 3
					" .$babDB->quote($this->createdOn). "
201
			)
202 3
					");
203
	
204 3
			$this->id = $babDB->db_insert_id();
205
		}
206
		
207
		
208 3
		$agentRight = $this->getAgentRight();
209 3
		$agent = $agentRight->getAgent();
210 3
		$right = $agentRight->getRight();
211
		
212 3
		$agentRight->addMovement(sprintf(absences_translate('The quantity for user %s on right %s has been changed because of the rule on consumed quantity : %s'), 
213 3
				$agent->getName(), $right->description, absences_quantity($this->quantity, $right->quantity_unit)));
214 3
	}
215
}
216
217
218
219
class absences_DynamicRightIterator extends absences_Iterator
220
{
221
	protected $agentRight;
222
	
223
	/**
224
	 * date time
225
	 * @var string
226
	 */
227
	public $createdOn;
228
	
229
	
230 39
	public function setAgentRight(absences_AgentRight $agentRight)
231
	{
232 39
		$this->agentRight = $agentRight;
233 39
	}
234
	
235
	
236 39
	public function executeQuery()
237
	{
238 39
		if(is_null($this->_oResult))
239 39
		{
240 39
			global $babDB;
241
			$req = "SELECT 
242
				dr.* 
243
			FROM
244
				absences_dynamic_rights dr 
245 39
			WHERE dr.id_user_right=".$babDB->quote($this->agentRight->id)." 
246 39
			";
247
			
248 39
			if (isset($this->createdOn)) {
249 14
			    $req .= ' AND dr.createdOn<='.$babDB->quote($this->createdOn);
250 14
			}
251
			
252 39
			$req .= ' ORDER BY dr.createdOn, dr.id ';
253
			
254
			// On right modification, the dynamic right rows are generated with the same createdOn date
255
	
256 39
			$this->setMySqlResult($this->getDataBaseAdapter()->db_query($req));
257 39
		}
258 39
	}
259
	
260
	
261 2
	public function getObject($data)
262
	{
263 2
		$dynRight = absences_DynamicRight::getById($data['id']);
264 2
		$dynRight->setRow($data);
265 2
		$dynRight->setAgentRight($this->agentRight);
266
	
267 2
		return $dynRight;
268
	}
269
	
270
	
271
}