BlameableExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 40.43 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 19 19 1
A validateConfigTypes() 0 7 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\Blameable\Blameable;
14
use Knp\DoctrineBehaviors\ORM\Blameable\BlameableSubscriber;
15
use Nette\Utils\AssertionException;
16
use Nette\Utils\Validators;
17
use Zenify\DoctrineBehaviors\Blameable\UserCallable;
18
19
20
final class BlameableExtension extends AbstractBehaviorExtension
21
{
22
23
	/**
24
	 * @var array
25
	 */
26
	private $default = [
27
		'isRecursive' => TRUE,
28
		'trait' => Blameable::class,
29
		'userCallable' => UserCallable::class,
30
		'userEntity' => NULL
31
	];
32
33
34 1 View Code Duplication
	public function loadConfiguration()
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...
35
	{
36 1
		$config = $this->getConfig($this->default);
37 1
		$this->validateConfigTypes($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->getConfig($this->default) on line 36 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...
38 1
		$builder = $this->getContainerBuilder();
39
40 1
		$userCallable = $this->buildDefinitionFromCallable($config['userCallable']);
41
42 1
		$builder->addDefinition($this->prefix('listener'))
43 1
			->setClass(BlameableSubscriber::class, [
44 1
				'@' . $this->getClassAnalyzer()->getClass(),
45 1
				$config['isRecursive'],
46 1
				$config['trait'],
47 1
				'@' . $userCallable->getClass(),
48 1
				$config['userEntity']
49
			])
50 1
			->setAutowired(FALSE)
51 1
			->addTag(EventsExtension::TAG_SUBSCRIBER);
52 1
	}
53
54
55
	/**
56
	 * @throws AssertionException
57
	 */
58 1
	private function validateConfigTypes(array $config)
59
	{
60 1
		Validators::assertField($config, 'isRecursive', 'bool');
61 1
		Validators::assertField($config, 'trait', 'type');
62 1
		Validators::assertField($config, 'userCallable', 'string');
63 1
		Validators::assertField($config, 'userEntity', 'null|string');
64 1
	}
65
66
}
67