GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LocaliseTableLocalise   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 229
Duplicated Lines 14.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 34
loc 229
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A _getAssetName() 0 6 1
A _getAssetTitle() 0 4 1
C _getAssetParentId() 34 85 14
A delete() 0 9 2
A deleteLanguageTranslations() 0 35 5

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
 * @package     Com_Localise
4
 * @subpackage  tables
5
 *
6
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
7
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8
 */
9
10
defined('_JEXEC') or die;
11
12
13
/**
14
 * Localise Table class for the Localise Component
15
 *
16
 * @package     Extensions.Components
17
 * @subpackage  Localise
18
 *
19
 * @since       1.0
20
 */
21
class LocaliseTableLocalise extends JTable
22
{
23
	/**
24
	 * Primary Key
25
	 *
26
	 * @var int
27
	 */
28
	public $id = null;
29
30
	/**
31
	 * The title to use for the asset table
32
	 *
33
	 * @var string
34
	 */
35
	public $path = null;
36
37
	/**
38
	 * Checked out status
39
	 *
40
	 * @var int
41
	 */
42
	public $checked_out = null;
43
44
	/**
45
	 * Checkout out time
46
	 *
47
	 * @var date
48
	 */
49
	public $checked_out_time = null;
50
51
	/**
52
	 * The asset ID
53
	 *
54
	 * @var asset_id
55
	 */
56
	public $asset_id = null;
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param   object  &$db  Database connector object
62
	 */
63
	public function __construct(&$db)
64
	{
65
		parent::__construct('#__localise', 'id', $db);
66
	}
67
68
	/**
69
	 * Method to compute the default name of the asset.
70
	 * The default name is in the form `table_name.id`
71
	 * where id is the value of the primary key of the table.
72
	 *
73
	 * @return  string
74
	 */
75
	protected function _getAssetName()
76
	{
77
		$k = $this->_tbl_key;
78
79
		return 'com_localise.' . (int) $this->$k;
80
	}
81
82
	/**
83
	 * Method to return the title to use for the asset table.
84
	 *
85
	 * @return  string
86
	 *
87
	 * @since   1.6
88
	 */
89
	protected function _getAssetTitle()
90
	{
91
		return basename($this->path);
92
	}
93
94
	/**
95
	 * Get the parent asset id for the record
96
	 *
97
	 * @param   JTable   $table  JTable Table object
98
	 * @param   Integer  $id     Primart key of table
99
	 *
100
	 * @return  Integer          Parent asset id for the record
101
	 */
102
	protected function _getAssetParentId(JTable $table = null, $id = null)
103
	{
104
		// Initialise variables.
105
		$db = $this->getDbo();
0 ignored issues
show
Unused Code introduced by
$db is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
107
		// Build the query to get the asset id for the parent category.
108
		$asset        = JTable::getInstance('asset');
109
		$name         = basename($this->path);
110
		$relativePath = substr($this->path, strlen(JPATH_ROOT));
111
112
		if (preg_match('/^([^.]*)\..*\.ini$/', $name, $matches) || preg_match('/^([^.]*)\.ini$/', $name, $matches))
113
		{
114
			$params              = JComponentHelper::getParams('com_localise');
115
			$installation_folder = $params->get('installation', 'installation');
116
			$tag                 = $matches[1];
117
118
			if (preg_match('#^/(administrator|plugins)#', $relativePath))
119
			{
120
				$id = LocaliseHelper::getFileId(JPATH_ROOT . "/administrator/language/$tag/$tag.xml");
121
			}
122
			elseif (preg_match('#^/' . $installation_folder . '#', $relativePath))
123
			{
124
				$id = LocaliseHelper::getFileId(LOCALISEPATH_INSTALLATION . "/language/$tag/$tag.xml");
125
			}
126
			else
127
			{
128
				$id = LocaliseHelper::getFileId(JPATH_ROOT . "/language/$tag/$tag.xml");
129
			}
130
131
			$assetName = "com_localise.$id";
132
133
			if (!$asset->loadByName($assetName))
134
			{
135
				$component = JTable::getInstance('asset');
136
137 View Code Duplication
				if (!$component->loadByName('com_localise'))
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...
138
				{
139
					$root = JTable::getInstance('asset');
140
					$root->rebuild();
141
					$root->loadByName('root.1');
142
					$component->name  = 'com_localise';
143
					$component->title = 'com_localise';
144
					$component->setLocation($root->id, 'last-child');
145
146
					if (!$component->check() || !$component->store())
147
					{
148
						$this->setError($component->getError());
149
150
						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 LocaliseTableLocalise::_getAssetParentId of type integer.

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...
151
					}
152
				}
153
154
				$asset->name  = "com_localise.$id";
155
				$asset->title = $name;
156
				$asset->setLocation($component->id, 'last-child');
157
158
				if (!$asset->check() || !$asset->store())
159
				{
160
					$this->setError($asset->getError());
161
162
					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 LocaliseTableLocalise::_getAssetParentId of type integer.

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...
163
				}
164
			}
165
		}
166 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...
167
		{
168
			if (!$asset->loadByName('com_localise'))
169
			{
170
				$root = JTable::getInstance('asset');
171
				$root->loadByName('root.1');
172
				$asset->name  = 'com_localise';
173
				$asset->title = 'com_localise';
174
				$asset->setLocation($root->id, 'last-child');
175
176
				if (!$asset->check() || !$asset->store())
177
				{
178
					$this->setError($asset->getError());
179
180
					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 LocaliseTableLocalise::_getAssetParentId of type integer.

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...
181
				}
182
			}
183
		}
184
185
		return $asset->id;
186
	}
187
188
	/**
189
	 * Method to delete a row from the database table by primary key value.
190
	 *
191
	 * @param   mixed  $pk  An optional primary key value to delete.  If not set the instance property value is used.
192
	 *
193
	 * @return  boolean  True on success.
194
	 *
195
	 * @link    http://docs.joomla.org/JTable/delete
196
	 * @since   11.1
197
	 * @throws  UnexpectedValueException
198
	 */
199
	public function delete($pk = null)
200
	{
201
		if (!$this->deleteLanguageTranslations())
202
		{
203
			return false;
204
		}
205
206
		return parent::delete($pk);
207
	}
208
209
	/**
210
	 * Delete language translations
211
	 *
212
	 * @return  boolean
213
	 */
214
	protected function deleteLanguageTranslations()
215
	{
216
		$fileName  = basename($this->path);
217
		$fileInfo  = pathinfo($fileName);
218
		$extension = isset($fileInfo['extension']) ? $fileInfo['extension'] : null;
219
		$langTag   = $fileInfo['filename'];
220
221
		// Make sure we are deleting a base language. Otherwise avoid to delete
222
		if ($extension != 'xml')
223
		{
224
			return true;
225
		}
226
227
		if ($langTag)
228
		{
229
			$db = $this->getDbo();
230
231
			$searchTag = $db->quote('%' . $db->escape($langTag, true) . '%');
232
233
			$query = $db->getQuery(true)
234
				->delete('#__localise')
235
				->where('path LIKE ' . $searchTag);
236
237
			$db->setQuery($query);
238
239
			if (!$db->execute())
240
			{
241
				$this->setError('COM_LOCALISE_ERROR_DELETING_DB_TRANSLATIONS');
242
243
				return false;
244
			}
245
		}
246
247
		return true;
248
	}
249
}
250