TranslatableExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 19 1
A validateConfigTypes() 0 8 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;
13
use Kdyby\Events\DI\EventsExtension;
14
use Knp\DoctrineBehaviors\Model\Translatable\Translation;
15
use Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber;
16
use Nette\Utils\AssertionException;
17
use Nette\Utils\Validators;
18
use Zenify\DoctrineBehaviors\Entities\Attributes\Translatable;
19
20
21
final class TranslatableExtension extends AbstractBehaviorExtension
22
{
23
24
	/**
25
	 * @var array
26
	 */
27
	private $default = [
28
		'currentLocaleCallable' => NULL,
29
		'defaultLocaleCallable' => NULL,
30
		'translatableTrait' => Translatable::class,
31
		'translationTrait' => Translation::class,
32
		'translatableFetchMode' => 'LAZY',
33
		'translationFetchMode' => 'LAZY',
34
	];
35
36
37 1
	public function loadConfiguration()
38
	{
39 1
		$config = $this->getConfig($this->default);
40 1
		$this->validateConfigTypes($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->getConfig($this->default) on line 39 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...
41 1
		$builder = $this->getContainerBuilder();
42
43 1
		$builder->addDefinition($this->prefix('listener'))
44 1
			->setClass(TranslatableSubscriber::class, [
45 1
				'@' . $this->getClassAnalyzer()->getClass(),
46 1
				$config['currentLocaleCallable'],
47 1
				$config['defaultLocaleCallable'],
48 1
				$config['translatableTrait'],
49 1
				$config['translationTrait'],
50 1
				$config['translatableFetchMode'],
51 1
				$config['translationFetchMode']
52
			])
53 1
			->setAutowired(FALSE)
54 1
			->addTag(EventsExtension::TAG_SUBSCRIBER);
55 1
	}
56
57
58
	/**
59
	 * @throws AssertionException
60
	 */
61 1
	private function validateConfigTypes(array $config)
62
	{
63 1
		Validators::assertField($config, 'currentLocaleCallable', 'null|array');
64 1
		Validators::assertField($config, 'translatableTrait', 'type');
65 1
		Validators::assertField($config, 'translationTrait', 'type');
66 1
		Validators::assertField($config, 'translatableFetchMode', 'string');
67 1
		Validators::assertField($config, 'translationFetchMode', 'string');
68 1
	}
69
70
}
71