Completed
Push — master ( 29d6e4...6080d3 )
by
unknown
8s
created

TreeExtension::validateConfigTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 5
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of Zenify
5
 * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
6
 */
7
8
namespace Zenify\DoctrineBehaviors\DI;
9
10
use Kdyby\Events\DI\EventsExtension;
11
use Knp\DoctrineBehaviors\Model\Tree\Node;
12
use Knp\DoctrineBehaviors\ORM\Tree\TreeSubscriber;
13
use Nette\Utils\AssertionException;
14
use Nette\Utils\Validators;
15
16
17 View Code Duplication
final class TreeExtension extends AbstractBehaviorExtension
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
20
	/**
21
	 * @var array
22
	 */
23
	private $default = [
24
		'isRecursive' => TRUE,
25
		'nodeTrait' => Node::class
26
	];
27
28
29
	/**
30
	 * {@inheritdoc}
31
	 */
32
	public function loadConfiguration()
33
	{
34
		$config = $this->getConfig($this->default);
35
		$this->validateConfigTypes($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->getConfig($this->default) on line 34 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...
36
		$builder = $this->getContainerBuilder();
37
38
		$builder->addDefinition($this->prefix('listener'))
39
			->setClass(TreeSubscriber::class, [
40
				'@' . $this->getClassAnalyzer()->getClass(),
41
				$config['isRecursive'],
42
				$config['nodeTrait']
43
			])
44
			->setAutowired(FALSE)
45
			->addTag(EventsExtension::TAG_SUBSCRIBER);
46
	}
47
48
49
	/**
50
	 * @throws AssertionException
51
	 */
52
	private function validateConfigTypes(array $config)
53
	{
54
		Validators::assertField($config, 'isRecursive', 'bool');
55
		Validators::assertField($config, 'nodeTrait', 'type');
56
	}
57
58
}
59