LoggableExtension::loadConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Zenify
7
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
8
 */
9
10
namespace Zenify\DoctrineBehaviors\DI;
11
12
use Kdyby\Events\DI\EventsExtension;
13
use Knp\DoctrineBehaviors\ORM\Loggable\LoggableSubscriber;
14
use Nette\Utils\AssertionException;
15
use Nette\Utils\Validators;
16
use Zenify\DoctrineBehaviors\Loggable\LoggerCallable;
17
18
19
final class LoggableExtension extends AbstractBehaviorExtension
20
{
21
22
	/**
23
	 * @var array
24
	 */
25
	private $default = [
26
		'isRecursive' => TRUE,
27
		'loggerCallable' => LoggerCallable::class
28
	];
29
30
31 1
	public function loadConfiguration()
32
	{
33 1
		$config = $this->getConfig($this->default);
34 1
		$this->validateConfigTypes($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->getConfig($this->default) on line 33 can also be of type string; however, Zenify\DoctrineBehaviors...::validateConfigTypes() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35 1
		$builder = $this->getContainerBuilder();
36
37 1
		$loggerCallable = $this->buildDefinitionFromCallable($config['loggerCallable']);
38
39 1
		$builder->addDefinition($this->prefix('listener'))
40 1
			->setClass(LoggableSubscriber::class, [
41 1
				'@' . $this->getClassAnalyzer()->getClass(),
42 1
				$config['isRecursive'],
43 1
				'@' . $loggerCallable->getClass()
44
			])
45 1
			->setAutowired(FALSE)
46 1
			->addTag(EventsExtension::TAG_SUBSCRIBER);
47 1
	}
48
49
50
	/**
51
	 * @throws AssertionException
52
	 */
53 1
	private function validateConfigTypes(array $config)
54
	{
55 1
		Validators::assertField($config, 'isRecursive', 'bool');
56 1
		Validators::assertField($config, 'loggerCallable', 'type');
57 1
	}
58
59
}
60