TreeExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 39
loc 39
c 0
b 0
f 0
wmc 2
lcom 1
cbo 4
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 15 15 1
A validateConfigTypes() 5 5 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
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\Model\Tree\Node;
14
use Knp\DoctrineBehaviors\ORM\Tree\TreeSubscriber;
15
use Nette\Utils\AssertionException;
16
use Nette\Utils\Validators;
17
18
19 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...
20
{
21
22
	/**
23
	 * @var array
24
	 */
25
	private $default = [
26
		'isRecursive' => TRUE,
27
		'nodeTrait' => Node::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
		$builder->addDefinition($this->prefix('listener'))
38 1
			->setClass(TreeSubscriber::class, [
39 1
				'@' . $this->getClassAnalyzer()->getClass(),
40 1
				$config['isRecursive'],
41 1
				$config['nodeTrait']
42
			])
43 1
			->setAutowired(FALSE)
44 1
			->addTag(EventsExtension::TAG_SUBSCRIBER);
45 1
	}
46
47
48
	/**
49
	 * @throws AssertionException
50
	 */
51 1
	private function validateConfigTypes(array $config)
52
	{
53 1
		Validators::assertField($config, 'isRecursive', 'bool');
54 1
		Validators::assertField($config, 'nodeTrait', 'type');
55 1
	}
56
57
}
58