ConflictChecker   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 16 4
A check() 0 20 5
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link https://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Utilities;
16
17
use Maslosoft\Addendum\Collections\AnnotationsCollection;
18
use Maslosoft\Addendum\Exceptions\ConflictException;
19
use Maslosoft\Addendum\Interfaces\AnnotationInterface;
20
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedClass;
21
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedMethod;
22
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedProperty;
23
24
/**
25
 * ConflictChecker
26
 *
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class ConflictChecker
30
{
31
32
	private static $_conflicts = [];
33
34
	/**
35
	 * Register annotation for later check
36
	 * @param AnnotationInterface $annotation Annotation
37
	 * @return void
38
	 */
39 55
	public static function register(AnnotationInterface $annotation)
40
	{
41 55
		$name = AnnotationName::createName($annotation);
42
43 55
		$reflection = new ReflectionAnnotatedClass($annotation);
44 55
		if ($reflection->hasAnnotation('Conflicts'))
45
		{
46 1
			$value = $reflection->getAnnotation('Conflicts')->value;
47 1
			$values = is_array($value) ? $value : [$value];
48 1
			foreach($values as $secondName)
49
			{
50 1
				self::$_conflicts[$name] = $secondName;
51 1
				self::$_conflicts[$secondName] = $name;
52
			}
53
		}
54 55
	}
55
56
	/**
57
	 * Check target constraints
58
	 * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $target Target entity
59
	 * @param AnnotationsCollection $annotations
60
	 * @return void
61
	 * @throws ConflictException
62
	 */
63 58
	public static function check($target, AnnotationsCollection $annotations)
64
	{
65 58
		if (!self::$_conflicts)
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$_conflicts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
		{
67 2
			return;
68
		}
69 57
		foreach ($annotations->getAllAnnotations() as $annotation)
70
		{
71 53
			$name = AnnotationName::createName($annotation);
72 53
			if (!isset(self::$_conflicts[$name]))
73
			{
74 32
				continue;
75
			}
76 29
			$second = self::$_conflicts[$name];
77 29
			if ($annotations->hasAnnotation($second))
78
			{
79 29
				throw new ConflictException(sprintf('Annotation `%s` cannot be used together with `%s` in `%s`', $name, $second, ReflectionName::createName($target)));
80
			}
81
		}
82 57
	}
83
84
}
85