Test Failed
Push — master ( 7a3064...2e6e31 )
by Roberto
02:45
created

Component::loadExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
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 22 and the first side effect is on line 15.

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\Component;
10
11
use Joomla\Registry\Registry;
12
use Phproberto\Joomla\Traits;
13
use Phproberto\Joomla\Helper\ComponentHelper;
14
15
defined('JPATH_PLATFORM') || die;
16
17
/**
18
 * Table finder.
19
 *
20
 * @since  __DEPLOY_VERSION__
21
 */
22
class Component
23
{
24
	use Traits\HasExtension, Traits\HasParams;
25
26
	/**
27
	 * Component option. Example: com_content
28
	 *
29
	 * @var  string
30
	 */
31
	protected $option;
32
33
	/**
34
	 * Component prefix for classes, etc.
35
	 *
36
	 * @var  string
37
	 */
38
	protected $prefix;
39
40
	/**
41
	 * Cached instances.
42
	 *
43
	 * @var  array
44
	 */
45
	protected static $instances = array();
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param   string  $option  Component option
51
	 */
52
	public function __construct($option)
53
	{
54
		$option = trim(strtolower($option));
55
56
		if (empty($option))
57
		{
58
			throw new \InvalidArgumentException(__CLASS__ . ': Empty component option.');
59
		}
60
61
		$this->option = $option;
62
	}
63
64
	/**
65
	 * Clear a singleton instance.
66
	 *
67
	 * @param   string  $option  Component option
68
	 *
69
	 * @return  void
70
	 */
71
	public static function clearInstance($option)
72
	{
73
		unset(static::$instances[get_called_class()][$option]);
74
	}
75
76
	/**
77
	 * Get the active component. Mainly for testing purposes.
78
	 *
79
	 * @return  string
80
	 */
81
	protected static function getActiveComponent()
82
	{
83
		return \JApplicationHelper::getComponentName();
84
	}
85
86
	/**
87
	 * Get a singleton instance.
88
	 *
89
	 * @param   string  $option  Component option
90
	 *
91
	 * @return  $this
92
	 */
93
	public static function getInstance($option = null)
94
	{
95
		$option = trim(strtolower($option));
96
		$option = $option ?: static::getActiveComponent();
97
98
		$class = get_called_class();
99
100 View Code Duplication
		if (empty(static::$instances[$class][$option]))
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...
101
		{
102
			static::$instances[$class][$option] = new static($option);
103
		}
104
105
		return static::$instances[$class][$option];
106
	}
107
108
	/**
109
	 * Get the component prefix.
110
	 *
111
	 * @return  string
112
	 */
113
	public function getPrefix()
114
	{
115
		if (null === $this->prefix)
116
		{
117
			$parts = array_map(
118
				function ($part)
119
				{
120
					return ucfirst(strtolower($part));
121
				},
122
				explode('_', substr($this->option, 4))
123
			);
124
125
			$this->prefix = implode('_', $parts);
126
		}
127
128
		return $this->prefix;
129
	}
130
131
	/**
132
	 * Get a component table.
133
	 *
134
	 * @param   string   $name     Name of the table to load. Example: Article
135
	 * @param   array    $config   Optional array of configuration for the table
136
	 * @param   boolean  $backend  Search in backend folder?
137
	 *
138
	 * @return  \JTable
139
	 *
140
	 * @throws  \InvalidArgumentException  If table not found
141
	 */
142
	public function getTable($name, array $config = array(), $backend = true)
143
	{
144
		$prefix = $this->getPrefix() . 'Table';
145
146
		$baseFolder = $backend ? JPATH_ADMINISTRATOR : JPATH_SITE;
147
148
		\JTable::addIncludePath($baseFolder . '/components/' . $this->option . '/tables');
149
150
		$table = \JTable::getInstance($name, $prefix, $config);
151
152
		if (!$table instanceof \JTable)
0 ignored issues
show
Bug introduced by
The class JTable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
153
		{
154
			throw new \InvalidArgumentException(
155
				sprintf('Cannot find the table %s in component %s.', $prefix . $name, $this->option)
156
			);
157
		}
158
159
		return $table;
160
	}
161
162
	/**
163
	 * Load extension from DB.
164
	 *
165
	 * @return  \stdClass
166
	 */
167
	protected function loadExtension()
168
	{
169
		$db = \JFactory::getDbo();
170
		$query = $db->getQuery(true)
171
			->select('*')
172
			->from('#__extensions')
173
			->where('type = ' . $db->quote('component'))
174
			->where('element = ' . $db->q($this->option));
175
176
		$db->setQuery($query);
177
178
		return $db->loadObject() ?: new \stdClass;
179
	}
180
181
	/**
182
	 * Load parameters from database.
183
	 *
184
	 * @return  Registry
185
	 */
186
	protected function loadParams()
187
	{
188
		return new Registry($this->getExtensionProperty('params', array()));
189
	}
190
}
191