Completed
Push — master ( cd1493...0fdab2 )
by Michael
02:22
created

ApplicationServiceProvider   B

Complexity

Total Complexity 2

Size/Duplication

Total Lines 223
Duplicated Lines 22.42 %

Coupling/Cohesion

Components 0
Dependencies 18

Test Coverage

Coverage 49.22%

Importance

Changes 13
Bugs 2 Features 5
Metric Value
wmc 2
c 13
b 2
f 5
lcom 0
cbo 18
dl 50
loc 223
ccs 63
cts 128
cp 0.4922
rs 7.3333

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 50 211 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
namespace Stats\Providers;
4
5
use Doctrine\Common\Cache\Cache;
6
use Joomla\Application as JoomlaApplication;
7
use Joomla\Database\DatabaseDriver;
8
use Joomla\DI\Container;
9
use Joomla\DI\ServiceProviderInterface;
10
use Joomla\Input\Cli;
11
use Joomla\Input\Input;
12
use Stats\CliApplication;
13
use Stats\Commands\HelpCommand;
14
use Stats\Commands\SnapshotCommand;
15
use Stats\Console;
16
use Stats\Controllers\DisplayControllerGet;
17
use Stats\Controllers\SubmitControllerCreate;
18
use Stats\Controllers\SubmitControllerGet;
19
use Stats\Models\StatsModel;
20
use Stats\Router;
21
use Stats\Views\Stats\StatsJsonView;
22
use Stats\WebApplication;
23
24
/**
25
 * Application service provider
26
 *
27
 * @since  1.0
28
 */
29
class ApplicationServiceProvider implements ServiceProviderInterface
30
{
31
	/**
32
	 * Registers the service provider with a DI container.
33
	 *
34
	 * @param   Container  $container  The DI container.
35
	 *
36
	 * @return  void
37
	 *
38
	 * @since   1.0
39
	 */
40 1
	public function register(Container $container)
0 ignored issues
show
Coding Style introduced by
register uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
	{
42 1
		$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class)
43 1
			->share(
44 1
				JoomlaApplication\AbstractCliApplication::class,
45
				function (Container $container)
46
				{
47
					$application = new CliApplication(
48
						$container->get(Cli::class),
49
						$container->get('config'),
50
						$container->get(JoomlaApplication\Cli\CliOutput::class),
51
						$container->get(Console::class)
52
					);
53
54
					// Inject extra services
55
					$application->setContainer($container);
56
					$application->setLogger($container->get('monolog.logger.cli'));
57
58
					return $application;
59 1
				},
60
				true
61 1
			);
62
63 1
		$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class)
64 1
			->share(
65 1
				JoomlaApplication\AbstractWebApplication::class,
66
				function (Container $container)
67
				{
68
					$application = new WebApplication($container->get(Input::class), $container->get('config'));
69
70
					// Inject extra services
71
					$application->setLogger($container->get('monolog.logger.application'));
72
					$application->setRouter($container->get(Router::class));
73
74
					return $application;
75 1
				},
76
				true
77 1
			);
78
79 1
		$container->share(
80 1
			Input::class,
81
			function ()
82
			{
83
				return new Input($_REQUEST);
84 1
			},
85
			true
86 1
		);
87
88 1
		$container->share(
89 1
			Cli::class,
90
			function ()
91
			{
92
				return new Cli;
93 1
			},
94
			true
95 1
		);
96
97 1
		$container->share(
98 1
			Console::class,
99
			function (Container $container)
100
			{
101
				$console = new Console;
102
				$console->setContainer($container);
103
104
				return $console;
105
			}
106 1
		);
107
108 1
		$container->share(
109 1
			JoomlaApplication\Cli\Output\Processor\ColorProcessor::class,
110
			function (Container $container)
111
			{
112
				$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor;
113
114
				/** @var Input $input */
115
				$input = $container->get(Cli::class);
116
117
				if ($input->get('nocolors'))
118
				{
119
					$processor->noColors = true;
120
				}
121
122
				// Setup app colors (also required in "nocolors" mode - to strip them).
123
				$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold']));
124
125
				return $processor;
126
			}
127 1
		);
128
129 1
		$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class)
130 1
			->share(
131 1
				JoomlaApplication\Cli\Output\Stdout::class,
132
				function (Container $container)
133
				{
134
					return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class));
135
				}
136 1
			);
137
138 1
		$container->share(
139 1
			Router::class,
140
			function (Container $container)
141
			{
142
				$router = (new Router($container->get(Input::class)))
143
					->setContainer($container)
144
					->setControllerPrefix('Stats\\Controllers\\')
145
					->setDefaultController('DisplayController')
146
					->addMap('/submit', 'SubmitController')
147
					->addMap('/:source', 'DisplayController');
148
149
				return $router;
150 1
			},
151
			true
152 1
		);
153
154 1
		$container->share(
155 1
			HelpCommand::class,
156 View Code Duplication
			function (Container $container)
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...
157
			{
158
				$command = new HelpCommand;
159
160
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
161
				$command->setInput($container->get(Input::class));
162
163
				return $command;
164 1
			},
165
			true
166 1
		);
167
168 1
		$container->share(
169 1
			SnapshotCommand::class,
170 View Code Duplication
			function (Container $container)
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...
171
			{
172
				$command = new SnapshotCommand($container->get(StatsJsonView::class));
173
174
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
175
				$command->setInput($container->get(Input::class));
176
177
				return $command;
178 1
			},
179
			true
180 1
		);
181
182 1
		$container->share(
183 1
			DisplayControllerGet::class,
184 View Code Duplication
			function (Container $container)
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...
185
			{
186
				$controller = new DisplayControllerGet(
187
					$container->get(StatsJsonView::class),
188
					$container->get(Cache::class)
189
				);
190
191
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
192
				$controller->setInput($container->get(Input::class));
193
194
				return $controller;
195 1
			},
196
			true
197 1
		);
198
199 1
		$container->share(
200 1
			SubmitControllerCreate::class,
201 View Code Duplication
			function (Container $container)
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...
202
			{
203
				$controller = new SubmitControllerCreate(
204
					$container->get(StatsModel::class)
205
				);
206
207
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
208
				$controller->setInput($container->get(Input::class));
209
210
				return $controller;
211 1
			},
212
			true
213 1
		);
214
215 1
		$container->share(
216 1
			SubmitControllerGet::class,
217 View Code Duplication
			function (Container $container)
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...
218
			{
219
				$controller = new SubmitControllerGet;
220
221
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
222
				$controller->setInput($container->get(Input::class));
223
224
				return $controller;
225 1
			},
226
			true
227 1
		);
228
229 1
		$container->share(
230 1
			StatsModel::class,
231
			function (Container $container)
232
			{
233
				return new StatsModel(
234
					$container->get(DatabaseDriver::class)
235
				);
236 1
			},
237
			true
238 1
		);
239
240 1
		$container->share(
241 1
			StatsJsonView::class,
242 1
			function (Container $container)
243
			{
244
				return new StatsJsonView(
245
					$container->get(StatsModel::class)
246
				);
247 1
			},
248
			true
249 1
		);
250 1
	}
251
}
252