absences_Organization   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 176
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 24
loc 176
rs 10
ccs 0
cts 66
cp 0
wmc 14
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getById() 0 7 1
A getByName() 0 19 2
A getRow() 0 23 3
A save() 0 22 2
A delete() 0 16 2
A getAgentInterator() 0 12 2
B createFromDirectory() 24 24 2

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
require_once dirname(__FILE__).'/record.class.php';
26
require_once dirname(__FILE__).'/collection.class.php';
27
require_once dirname(__FILE__).'/vacincl.php';
28
29
/**
30
 * Organization
31
 * 
32
 * @property	string	$name
33
 * @property	string	$description
34
 */
35
class absences_Organization extends absences_Record 
36
{
37
	
38
39
    /**
40
     * @return absences_Organization
41
     */
42
    public static function getById($id)
43
    {
44
        $organization = new absences_Organization;
45
        $organization->id = $id;
46
    
47
        return $organization;
48
    }
49
    
50
    /**
51
     * get by name or return null
52
     * @return absences_Organization
53
     */
54
    public static function getByName($name)
55
    {
56
        global $babDB;
57
        
58
        $query = "SELECT * FROM absences_organization WHERE name LIKE '".$babDB->db_escape_like($name)."'";
59
        
60
        $res = $babDB->db_query($query);
61
        $row = $babDB->db_fetch_assoc($res);
62
			
63
		if (!$row)
64
		{
65
			return null;
66
		}
67
        
68
		$organization = new absences_Organization();
69
		$organization->setRow($row);
70
		
71
        return $organization;
72
    }
73
	
74
	
75
	/**
76
	 * Table row as an array
77
	 * @return array
78
	 */
79
	public function getRow()
80
	{
81
		if (null === $this->row)
82
		{
83
			global $babDB;
84
			
85
			$query = 'SELECT * FROM absences_organization WHERE id='.$babDB->quote($this->id);
86
87
			$res = $babDB->db_query($query);
88
			$row = $babDB->db_fetch_assoc($res);
89
			
90
			if (!$row)
91
			{
92
				throw new Exception('This organization does not exists id='.$this->id);
93
			}
94
			
95
			$this->setRow($row);
96
			
97
			return $this->row;
98
		}
99
		
100
		return $this->row;
101
	}
102
	
103
	
104
	
105
	
106
	
107
	/**
108
	 * Save organization
109
	 * @return bool
110
	 */
111
	public function save()
112
	{
113
	    global $babDB;
114
	
115
	    if (empty($this->id))
116
	    {
117
	        $babDB->db_query('
118
				INSERT INTO absences_organization (name
119
				) VALUES (
120
					'.$babDB->quote($this->name).'
121
				)
122
			');
123
	    } else {
124
	        $babDB->db_query('UPDATE absences_organization
125
				SET
126
					name='.$babDB->quote($this->name).' 
127
				WHERE
128
					id='.$babDB->quote($this->id));
129
	    }
130
	    
131
	    return true;
132
	}
133
	
134
	
135
	/**
136
	 * Remove references and delete
137
	 * @return bool
138
	 */
139
	public function delete()
140
	{
141
	    global $babDB;
142
	    
143
	    if (!$this->id) {
144
	        throw new Exception('Missing ID');
145
	    }
146
	    
147
	    $babDB->db_query("UPDATE absences_personnel SET id_organization='0' 
148
	        WHERE id_organization=".$babDB->quote($this->id));
149
	    
150
	    $babDB->db_query("DELETE FROM absences_organization 
151
	        WHERE id=".$babDB->quote($this->id));
152
	    
153
	    return (1 == $babDB->db_affected_rows());
154
	}
155
	
156
	
157
	
158
	/**
159
	 * Get agents
160
	 * @return absences_AgentIterator | false
161
	 */
162
	public function getAgentInterator()
163
	{
164
	    if (!$this->id) {
165
	        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by absences_Organization::getAgentInterator of type absences_AgentIterator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
166
	    }
167
	    
168
	    require_once dirname(__FILE__).'/agent.class.php';
169
	    $agents = new absences_AgentIterator();
170
	    $agents->setOrganization($this);
171
	    
172
	    return $agents;
173
	}
174
	
175
	
176
	
177
	
178
	
179
	
180
	/**
181
	 * Create organizations from the organization directory field "organisationname"
182
	 * this method must not modify the organizations ID
183
	 * 
184
	 */
185 View Code Duplication
	public static function createFromDirectory()
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...
186
	{
187
	    global $babDB;
188
	    
189
	    $res = $babDB->db_query("
190
	        SELECT 
191
	           e.organisationname name, 
192
	           o.id 
193
	        
194
	        FROM bab_dbdir_entries e
195
	               LEFT JOIN absences_organization o ON o.name LIKE e.organisationname, 
196
	           absences_personnel p 
197
	        WHERE e.id_user=p.id_user 
198
	           AND e.organisationname<>'' 
199
	        
200
	        GROUP BY organisationname
201
	    ");
202
	    
203
	    while ($arr = $babDB->db_fetch_assoc($res)) {
204
	        $org = new absences_Organization();
205
	        $org->setRow($arr);
206
	        $org->save();
207
	    }
208
	}
209
	
210
}
211
212
213
214
215
216 View Code Duplication
class absences_OrganizationIterator extends absences_Iterator
0 ignored issues
show
Duplication introduced by
This class 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...
217
{
218
	public function getObject($data)
219
	{
220
		$org = new absences_Organization;
221
		$org->setRow($data);
222
223
224
		return $org;
225
	}
226
227
	public function executeQuery()
228
	{
229
		if(is_null($this->_oResult))
230
		{
231
			global $babDB;
232
	
233
			$query = '
234
				SELECT
235
					o.*
236
				FROM 
237
					absences_organization o 
238
			';			
239
			
240
			$this->setMySqlResult($this->getDataBaseAdapter()->db_query($query));
241
		}
242
	}
243
244
}