Completed
Push — master ( d5e807...536ced )
by Michael
05:17 queued 45s
created

MonologServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 166
Duplicated Lines 11.45 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 6
dl 19
loc 166
ccs 44
cts 44
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 23 1
A getMonologProcessorPsr3Service() 0 4 1
A getMonologProcessorWebService() 0 4 1
A getMonologHandlerApplicationService() 9 9 1
A getMonologHandlerDatabaseService() 10 10 2
A getMonologLoggerApplicationService() 0 12 1
A getMonologLoggerCliService() 0 9 1
A getMonologLoggerDatabaseService() 0 13 1

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 Joomla\DI\Container;
6
use Joomla\DI\ServiceProviderInterface;
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Logger;
9
use Monolog\Processor\PsrLogMessageProcessor;
10
use Monolog\Processor\WebProcessor;
11
12
/**
13
 * Monolog service provider
14
 *
15
 * @since  1.0
16
 */
17
class MonologServiceProvider implements ServiceProviderInterface
18
{
19
	/**
20
	 * Registers the service provider with a DI container.
21
	 *
22
	 * @param   Container  $container  The DI container.
23
	 *
24
	 * @return  void
25
	 *
26
	 * @since   1.0
27
	 */
28 1
	public function register(Container $container)
29
	{
30
		// Register the PSR-3 processor
31 1
		$container->share('monolog.processor.psr3', [$this, 'getMonologProcessorPsr3Service'], true);
32
33
		// Register the web processor
34 1
		$container->share('monolog.processor.web', [$this, 'getMonologProcessorWebService'], true);
35
36
		// Register the web application handler
37 1
		$container->share('monolog.handler.application', [$this, 'getMonologHandlerApplicationService'], true);
38
39
		// Register the database handler
40 1
		$container->share('monolog.handler.database', [$this, 'getMonologHandlerDatabaseService'], true);
41
42
		// Register the web application Logger
43 1
		$container->share('monolog.logger.application', [$this, 'getMonologLoggerApplicationService'], true);
44
45
		// Register the CLI application Logger
46 1
		$container->share('monolog.logger.cli', [$this, 'getMonologLoggerCliService'], true);
47
48
		// Register the database Logger
49 1
		$container->share('monolog.logger.database', [$this, 'getMonologLoggerDatabaseService'], true);
50 1
	}
51
52
	/**
53
	 * Get the `monolog.processor.psr3` service
54
	 *
55
	 * @param   Container  $container  The DI container.
56
	 *
57
	 * @return  PsrLogMessageProcessor
58
	 *
59
	 * @since   1.0
60
	 */
61 1
	public function getMonologProcessorPsr3Service(Container $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
	{
63 1
		return new PsrLogMessageProcessor;
64
	}
65
66
	/**
67
	 * Get the `monolog.processor.web` service
68
	 *
69
	 * @param   Container  $container  The DI container.
70
	 *
71
	 * @return  WebProcessor
72
	 *
73
	 * @since   1.0
74
	 */
75 1
	public function getMonologProcessorWebService(Container $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
	{
77 1
		return new WebProcessor;
78
	}
79
80
	/**
81
	 * Get the `monolog.handler.application` service
82
	 *
83
	 * @param   Container  $container  The DI container.
84
	 *
85
	 * @return  StreamHandler
86
	 *
87
	 * @since   1.0
88
	 */
89 1 View Code Duplication
	public function getMonologHandlerApplicationService(Container $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
	{
91
		/** @var \Joomla\Registry\Registry $config */
92 1
		$config = $container->get('config');
93
94 1
		$level = strtoupper($config->get('log.application', $config->get('log.level', 'error')));
95
96 1
		return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level));
97
	}
98
99
	/**
100
	 * Get the `monolog.handler.database` service
101
	 *
102
	 * @param   Container  $container  The DI container.
103
	 *
104
	 * @return  StreamHandler
105
	 *
106
	 * @since   1.0
107
	 */
108 1 View Code Duplication
	public function getMonologHandlerDatabaseService(Container $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
109
	{
110
		/** @var \Joomla\Registry\Registry $config */
111 1
		$config = $container->get('config');
112
113
		// If database debugging is enabled then force the logger's error level to DEBUG, otherwise use the level defined in the app config
114 1
		$level = $config->get('database.debug', false) ? 'DEBUG' : strtoupper($config->get('log.database', $config->get('log.level', 'error')));
115
116 1
		return new StreamHandler(APPROOT . '/logs/stats.log', constant('\\Monolog\\Logger::' . $level));
117
	}
118
119
	/**
120
	 * Get the `monolog.logger.application` service
121
	 *
122
	 * @param   Container  $container  The DI container.
123
	 *
124
	 * @return  Logger
125
	 *
126
	 * @since   1.0
127
	 */
128 1
	public function getMonologLoggerApplicationService(Container $container)
129
	{
130 1
		return new Logger(
131 1
			'Application',
132
			[
133 1
				$container->get('monolog.handler.application')
134 1
			],
135
			[
0 ignored issues
show
Documentation introduced by
array($container->get('monolog.processor.web')) is of type array<integer,*,{"0":"*"}>, but the function expects a array<integer,callable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136 1
				$container->get('monolog.processor.web')
137 1
			]
138 1
		);
139
	}
140
141
	/**
142
	 * Get the `monolog.logger.cli` service
143
	 *
144
	 * @param   Container  $container  The DI container.
145
	 *
146
	 * @return  Logger
147
	 *
148
	 * @since   1.0
149
	 */
150 1
	public function getMonologLoggerCliService(Container $container)
151
	{
152 1
		return new Logger(
153 1
			'Application',
154
			[
155 1
				$container->get('monolog.handler.application')
156 1
			]
157 1
		);
158
	}
159
160
	/**
161
	 * Get the `monolog.logger.database` service
162
	 *
163
	 * @param   Container  $container  The DI container.
164
	 *
165
	 * @return  Logger
166
	 *
167
	 * @since   1.0
168
	 */
169 1
	public function getMonologLoggerDatabaseService(Container $container)
170
	{
171 1
		return new Logger(
172 1
			'Application',
173
			[
174 1
				$container->get('monolog.handler.database')
175 1
			],
176
			[
0 ignored issues
show
Documentation introduced by
array($container->get('m...onolog.processor.web')) is of type array<integer,*,{"0":"*","1":"*"}>, but the function expects a array<integer,callable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177 1
				$container->get('monolog.processor.psr3'),
178 1
				$container->get('monolog.processor.web')
179 1
			]
180 1
		);
181
	}
182
}
183