Completed
Push — Parse-Tags ( be9c93 )
by Michael
03:38
created

ApplicationServiceProvider   C

Complexity

Total Complexity 2

Size/Duplication

Total Lines 237
Duplicated Lines 24.05 %

Coupling/Cohesion

Components 0
Dependencies 19

Test Coverage

Coverage 49.26%

Importance

Changes 14
Bugs 2 Features 6
Metric Value
wmc 2
c 14
b 2
f 6
lcom 0
cbo 19
dl 57
loc 237
ccs 67
cts 136
cp 0.4926
rs 6.875

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 57 225 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\JoomlaTagsCommand;
15
use Stats\Commands\SnapshotCommand;
16
use Stats\Console;
17
use Stats\Controllers\DisplayControllerGet;
18
use Stats\Controllers\SubmitControllerCreate;
19
use Stats\Controllers\SubmitControllerGet;
20
use Stats\GitHub\GitHub;
21
use Stats\Models\StatsModel;
22
use Stats\Router;
23
use Stats\Views\Stats\StatsJsonView;
24
use Stats\WebApplication;
25
26
/**
27
 * Application service provider
28
 *
29
 * @since  1.0
30
 */
31
class ApplicationServiceProvider implements ServiceProviderInterface
32
{
33
	/**
34
	 * Registers the service provider with a DI container.
35
	 *
36
	 * @param   Container  $container  The DI container.
37
	 *
38
	 * @return  void
39
	 *
40
	 * @since   1.0
41
	 */
42 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...
43
	{
44 1
		$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class)
45 1
			->share(
46 1
				JoomlaApplication\AbstractCliApplication::class,
47
				function (Container $container)
48
				{
49
					$application = new CliApplication(
50
						$container->get(Cli::class),
51
						$container->get('config'),
52
						$container->get(JoomlaApplication\Cli\CliOutput::class),
53
						$container->get(Console::class)
54
					);
55
56
					// Inject extra services
57
					$application->setContainer($container);
58
					$application->setLogger($container->get('monolog.logger.cli'));
59
60
					return $application;
61 1
				},
62
				true
63 1
			);
64
65 1
		$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class)
66 1
			->share(
67 1
				JoomlaApplication\AbstractWebApplication::class,
68
				function (Container $container)
69
				{
70
					$application = new WebApplication($container->get(Input::class), $container->get('config'));
71
72
					// Inject extra services
73
					$application->setLogger($container->get('monolog.logger.application'));
74
					$application->setRouter($container->get(Router::class));
75
76
					return $application;
77 1
				},
78
				true
79 1
			);
80
81 1
		$container->share(
82 1
			Input::class,
83
			function ()
84
			{
85
				return new Input($_REQUEST);
86 1
			},
87
			true
88 1
		);
89
90 1
		$container->share(
91 1
			Cli::class,
92
			function ()
93
			{
94
				return new Cli;
95 1
			},
96
			true
97 1
		);
98
99 1
		$container->share(
100 1
			Console::class,
101
			function (Container $container)
102
			{
103
				$console = new Console;
104
				$console->setContainer($container);
105
106
				return $console;
107
			}
108 1
		);
109
110 1
		$container->share(
111 1
			JoomlaApplication\Cli\Output\Processor\ColorProcessor::class,
112
			function (Container $container)
113
			{
114
				$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor;
115
116
				/** @var Input $input */
117
				$input = $container->get(Cli::class);
118
119
				if ($input->get('nocolors'))
120
				{
121
					$processor->noColors = true;
122
				}
123
124
				// Setup app colors (also required in "nocolors" mode - to strip them).
125
				$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold']));
126
127
				return $processor;
128
			}
129 1
		);
130
131 1
		$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class)
132 1
			->share(
133 1
				JoomlaApplication\Cli\Output\Stdout::class,
134
				function (Container $container)
135
				{
136
					return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class));
137
				}
138 1
			);
139
140 1
		$container->share(
141 1
			Router::class,
142
			function (Container $container)
143
			{
144
				$router = (new Router($container->get(Input::class)))
145
					->setContainer($container)
146
					->setControllerPrefix('Stats\\Controllers\\')
147
					->setDefaultController('DisplayController')
148
					->addMap('/submit', 'SubmitController')
149
					->addMap('/:source', 'DisplayController');
150
151
				return $router;
152 1
			},
153
			true
154 1
		);
155
156 1
		$container->share(
157 1
			HelpCommand::class,
158 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...
159
			{
160
				$command = new HelpCommand;
161
162
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
163
				$command->setInput($container->get(Input::class));
164
165
				return $command;
166 1
			},
167
			true
168 1
		);
169
170 1
		$container->share(
171 1
			JoomlaTagsCommand::class,
172 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...
173
			{
174
				$command = new JoomlaTagsCommand($container->get(GitHub::class));
175
176
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
177
				$command->setInput($container->get(Input::class));
178
179
				return $command;
180 1
			},
181
			true
182 1
		);
183
184 1
		$container->share(
185 1
			SnapshotCommand::class,
186 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...
187
			{
188
				$command = new SnapshotCommand($container->get(StatsJsonView::class));
189
190
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
191
				$command->setInput($container->get(Input::class));
192
193
				return $command;
194 1
			},
195
			true
196 1
		);
197
198 1
		$container->share(
199 1
			DisplayControllerGet::class,
200 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...
201
			{
202
				$controller = new DisplayControllerGet(
203
					$container->get(StatsJsonView::class),
204
					$container->get(Cache::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
			SubmitControllerCreate::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 SubmitControllerCreate(
220
					$container->get(StatsModel::class)
221
				);
222
223
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
224
				$controller->setInput($container->get(Input::class));
225
226
				return $controller;
227 1
			},
228
			true
229 1
		);
230
231 1
		$container->share(
232 1
			SubmitControllerGet::class,
233 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...
234
			{
235
				$controller = new SubmitControllerGet;
236
237
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
238
				$controller->setInput($container->get(Input::class));
239
240
				return $controller;
241 1
			},
242
			true
243 1
		);
244
245 1
		$container->share(
246 1
			StatsModel::class,
247
			function (Container $container)
248
			{
249
				return new StatsModel(
250
					$container->get(DatabaseDriver::class)
251
				);
252 1
			},
253
			true
254 1
		);
255
256 1
		$container->share(
257 1
			StatsJsonView::class,
258 1
			function (Container $container)
259
			{
260
				return new StatsJsonView(
261
					$container->get(StatsModel::class)
262
				);
263 1
			},
264
			true
265 1
		);
266 1
	}
267
}
268