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.

JFormFieldReleases   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 156
Duplicated Lines 43.59 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 68
loc 156
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
F getOptions() 68 141 21

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  models
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
use Joomla\Github\Github;
13
14
JFormHelper::loadFieldClass('list');
15
16
/**
17
 * Form Field Place class.
18
 *
19
 * @package     Extensions.Components
20
 * @subpackage  Localise
21
 *
22
 * @since       1.0
23
 */
24
class JFormFieldReleases extends JFormFieldList
25
{
26
	/**
27
	 * The field type.
28
	 *
29
	 * @var    string
30
	 */
31
	protected $type = 'Releases';
32
33
	/**
34
	 * Method to get the field input.
35
	 *
36
	 * @return  string    The field input.
37
	 */
38
	protected function getOptions()
39
	{
40
		require_once JPATH_ADMINISTRATOR . '/components/com_localise/vendor/autoload.php';
41
42
		$attributes    = '';
43
		$params        = JComponentHelper::getParams('com_localise');
44
		$versions_path = JPATH_ROOT
45
				. '/administrator/components/com_localise/customisedref/stable_joomla_releases.txt';
46
47
		// Empty txt file to make sure it contains only stable releases after save.
48
		if ($params->get('pre_stable', '0') == '0')
49
		{
50
			file_put_contents($versions_path, '');
51
		}
52
53
		$versions_file = file_get_contents($versions_path);
54
		$versions      = preg_split("/\\r\\n|\\r|\\n/", $versions_file);
55
56
		$gh_user       = 'joomla';
57
		$gh_project    = 'joomla-cms';
58
		$gh_token      = $params->get('gh_token', '');
59
60
		$options = new JRegistry;
61
62 View Code Duplication
		if (!empty($gh_token))
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...
63
		{
64
			$options->set('gh.token', $gh_token);
65
			$github = new Github($options);
66
		}
67
		else
68
		{
69
			// Without a token runs fatal.
70
			// $github = new JGithub;
71
72
			// Trying with a 'read only' public repositories token
73
			// But base 64 encoded to avoid Github alarms sharing it.
74
			$gh_token = base64_decode('MzY2NzYzM2ZkMzZmMWRkOGU5NmRiMTdjOGVjNTFiZTIyMzk4NzVmOA==');
75
			$options->set('gh.token', $gh_token);
76
			$github = new Github($options);
77
		}
78
79
		try
80
		{
81
			$releases = $github->repositories->get(
82
					$gh_user,
83
					$gh_project . '/releases'
84
					);
85
86 View Code Duplication
			foreach ($releases as $release)
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...
87
			{
88
				$tag_name = $release->tag_name;
89
				$tag_part = explode(".", $tag_name);
90
				$undoted  = str_replace('.', '', $tag_name);
91
				$excluded = 0;
92
93
				if (version_compare(JVERSION[0], '2', 'eq'))
94
				{
95
					$excluded = 1;
96
				}
97
				elseif (version_compare(JVERSION[0], '3', 'eq'))
98
				{
99
					if ($tag_part[0] != '3')
100
					{
101
						$excluded = 1;
102
					}
103
				}
104
				elseif (version_compare(JVERSION[0], '4', 'ge'))
105
				{
106
					if ($tag_part[0] == '4' || $tag_part[0] == '3')
107
					{
108
						$excluded = 0;
109
					}
110
					else
111
					{
112
						$excluded = 1;
113
					}
114
				}
115
116
				// Filtering by "is_numeric" disable betas or similar releases.
117
				if ($params->get('pre_stable', '0') == '0')
118
				{
119
					if (!in_array($tag_name, $versions) && is_numeric($undoted) && $excluded == 0)
120
					{
121
						$versions[] = $tag_name;
122
						JFactory::getApplication()->enqueueMessage(
123
							JText::sprintf('COM_LOCALISE_NOTICE_NEW_VERSION_DETECTED', $tag_name),
124
							'notice');
125
					}
126
				}
127
				else
128
				{
129
					if (!in_array($tag_name, $versions) && $excluded == 0)
130
					{
131
						$versions[] = $tag_name;
132
						JFactory::getApplication()->enqueueMessage(
133
							JText::sprintf('COM_LOCALISE_NOTICE_NEW_VERSION_DETECTED', $tag_name),
134
							'notice');
135
					}
136
				}
137
			}
138
		}
139
		catch (Exception $e)
140
		{
141
			JFactory::getApplication()->enqueueMessage(
142
				JText::_('COM_LOCALISE_ERROR_GITHUB_GETTING_RELEASES'),
143
				'warning');
144
		}
145
146
		arsort($versions);
147
148
		if ($v = (string) $this->element['onchange'])
149
		{
150
			$attributes .= ' onchange="' . $v . '"';
151
		}
152
153
		$attributes .= ' class="' . (string) $this->element['class'] . ' iconlist-16-' . $this->value . '"';
154
		$options = array();
155
156
		foreach ($this->element->children() as $option)
157
		{
158
			$options[] = JHtml::_('select.option', $option->attributes('value'), JText::_(trim($option)), array('option.attr' => 'attributes', 'attr' => ''));
159
		}
160
161
		$versions_file = '';
162
163
		foreach ($versions as $id => $version)
164
		{
165
			if (!empty($version))
166
			{
167
				$options[] = JHtml::_('select.option', $version, JText::sprintf('COM_LOCALISE_CUSTOMIZED_REFERENCE', $version),
168
							array('option.attr' => 'attributes', 'attr' => 'class="iconlist-16-release"')
169
							);
170
171
				$versions_file .= $version . "\n";
172
			}
173
		}
174
175
		JFile::write($versions_path, $versions_file);
176
177
		return $options;
178
	}
179
}
180