Completed
Push — master ( 77c306...84aad0 )
by Michael
02:51
created

ApplicationServiceProvider::register()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 113
Code Lines 66

Duplication

Lines 23
Ratio 20.35 %

Code Coverage

Tests 37
CRAP Score 1.1148

Importance

Changes 9
Bugs 2 Features 3
Metric Value
c 9
b 2
f 3
dl 23
loc 113
ccs 37
cts 72
cp 0.5139
rs 8.2857
cc 1
eloc 66
nc 1
nop 1
crap 1.1148

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Input;
11
use Stats\Application;
12
use Stats\Controllers\DisplayControllerGet;
13
use Stats\Controllers\SubmitControllerCreate;
14
use Stats\Controllers\SubmitControllerGet;
15
use Stats\Models\StatsModel;
16
use Stats\Router;
17
use Stats\Views\Stats\StatsJsonView;
18
19
/**
20
 * Application service provider
21
 *
22
 * @since  1.0
23
 */
24
class ApplicationServiceProvider implements ServiceProviderInterface
25
{
26
	/**
27
	 * Registers the service provider with a DI container.
28
	 *
29
	 * @param   Container  $container  The DI container.
30
	 *
31
	 * @return  void
32
	 *
33
	 * @since   1.0
34
	 */
35 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...
36
	{
37 1
		$container->alias(Application::class, JoomlaApplication\AbstractApplication::class)
38 1
			->alias(JoomlaApplication\AbstractWebApplication::class, JoomlaApplication\AbstractApplication::class)
39 1
			->share(
40 1
				JoomlaApplication\AbstractApplication::class,
41
				function (Container $container)
42
				{
43
					$application = new Application($container->get(Input::class), $container->get('config'));
44
45
					// Inject extra services
46
					$application->setLogger($container->get('monolog.logger.application'));
47
					$application->setRouter($container->get(Router::class));
48
49
					return $application;
50 1
				},
51
				true
52 1
			);
53
54 1
		$container->share(
55 1
			Input::class,
56
			function ()
57
			{
58
				return new Input($_REQUEST);
59 1
			},
60
			true
61 1
		);
62
63 1
		$container->share(
64 1
			Router::class,
65
			function (Container $container)
66
			{
67
				$router = (new Router($container->get(Input::class)))
68
					->setContainer($container)
69
					->setControllerPrefix('Stats\\Controllers\\')
70
					->setDefaultController('DisplayController')
71
					->addMap('/submit', 'SubmitController')
72
					->addMap('/:source', 'DisplayController');
73
74
				return $router;
75 1
			},
76
			true
77 1
		);
78
79 1
		$container->share(
80 1
			DisplayControllerGet::class,
81 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...
82
			{
83
				$controller = new DisplayControllerGet(
84
					$container->get(StatsJsonView::class),
85
					$container->get(Cache::class)
86
				);
87
88
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
89
				$controller->setInput($container->get(Input::class));
90
91
				return $controller;
92 1
			},
93
			true
94 1
		);
95
96 1
		$container->share(
97 1
			SubmitControllerCreate::class,
98 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...
99
			{
100
				$controller = new SubmitControllerCreate(
101
					$container->get(StatsModel::class)
102
				);
103
104
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
105
				$controller->setInput($container->get(Input::class));
106
107
				return $controller;
108 1
			},
109
			true
110 1
		);
111
112 1
		$container->share(
113 1
			SubmitControllerGet::class,
114
			function (Container $container)
115
			{
116
				$controller = new SubmitControllerGet;
117
118
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
119
				$controller->setInput($container->get(Input::class));
120
121
				return $controller;
122 1
			},
123
			true
124 1
		);
125
126 1
		$container->share(
127 1
			StatsModel::class,
128
			function (Container $container)
129
			{
130
				return new StatsModel(
131
					$container->get(DatabaseDriver::class)
132
				);
133 1
			},
134
			true
135 1
		);
136
137 1
		$container->share(
138 1
			StatsJsonView::class,
139 1
			function (Container $container)
140
			{
141
				return new StatsJsonView(
142
					$container->get(StatsModel::class)
143
				);
144 1
			},
145
			true
146 1
		);
147 1
	}
148
}
149